var swgallery=Class.create({
	elements:[],
	current:0,
	timeline:false,
	options:{
		style:[
			{opacity:0},
			{opacity:1},
			{opacity:0}
		],
		loop:true,
		transition2next:'quadInOut',
		transition2prev:'quadInOut',
		duration2next:500,
		duration2prev:500
	},
	initialize:function(elements,options){
		this.elements=elements;
		Object.extend(this.options, options);

		this.elements[0].setStyle(this.options.style[1]);
		for (var a=1; a < this.elements.length; a++)
			this.elements[a].setStyle(this.options.style[0]);
		this.timeline=new Timeline({autostop:false});
		this.timeline.start();
	},
	next:function(){
		var e=this.get_current_elements(1);
		if (!e) return;
		this.change(e,2,this.options.transition2next,this.options.duration2next);
	},
	prev:function(){
		var e=this.get_current_elements(-1);
		if (!e) return;
		this.change(e,0,this.options.transition2prev,this.options.duration2prev);
	},
	change:function(e,o,t,d){
		this.timeline.addTweenings([
			[e[1],this.options.style[1],{duration:d,transition:t,oldstyles:this.options.style[(o ? 0 : 2)]}],
			[e[0],this.options.style[o],{duration:d,transition:t,oldstyles:this.options.style[1]}]
		]);
	},
	get_current_elements:function(to){
		to+=this.current;
		if (to < 0) { if (this.options.loop) to=this.elements.length-1; else return false; }
		if (to >= this.elements.length) { if (this.options.loop) to=0; else return false; }

		var r=[
			this.elements[this.current],
			this.elements[to]
		];

		this.current=to;

		return r;
	}
});

