//the $$ is required to check for element existance on each transition

var MooSlideshow = new Class({
	Implements: [Options,Events],
	initialize: function(container, elements, options){
		this.setOptions({
			"wait": 6000,
			"fxDuration": 4000,
			"fxTransition": Fx.Transitions.Quad.easeOut,
			"onChange": $empty
		}, options);
		
		this.container = $(container);
		this.container.setStyles({
			"position":"relative",
			"overflow":"hidden"
		});
		
		this.elements = elements;
		this.totalElements = $$(this.elements).length;
		
		if(this.totalElements <= 1) return;

		this.effectObj = { 
			"duration": this.options.fxDuration, 
			"transition": this.options.fxTransition, 
			"link": "cancel",
			"onComplete": (function(){ this.busy = false; }).bind(this)
		};
		
		$$(this.elements).each(function(el, index){
			el.setStyles({
				"position":"absolute"
			});
			el.set("tween",this.effectObj)
			if(index > 0) el.setStyle("opacity",0);
		}.bind(this));

		this.currentIndex = 0;
		this.previousIndex = 0;
		
		this.timer = 0;
		this.busy = false;
		this.timer = this.next.periodical(this.options.wait, this);
	},
	
	stop: function(){
		$clear(this.timer);
		this.busy = false;
	},
	
	next: function(){
		this.slides = $$(this.elements);
		
		if(this.slides.length <= 1) {
			$clear(this.timer);
			return;
		}
	
		this.previousIndex = this.currentIndex;
		this.currentIndex++;
		if(this.currentIndex >= this.totalElements) this.currentIndex = 0;
		
		this.busy = true;
		
		this.slides[this.previousIndex].get("tween").start("opacity", 1, 0);
		this.slides[this.currentIndex].get("tween").start("opacity", 0, 1);

		this.fireEvent("onChange", [this.currentIndex, this.slides[this.currentIndex], this]);
	},
	
	jumpTo: function(index){
		if(this.busy) return;
		this.slides = $$(this.elements);
	
		if(index < 0 || index >= this.totalElements) index = 0;
		
		this.previousIndex = this.currentIndex;
		this.currentIndex = index;

		$clear(this.timer);
		this.timer = this.next.periodical(this.options.wait, this);

		this.slides[this.previousIndex].get("tween").start("opacity", 0);
		this.slides[this.currentIndex].get("tween").start("opacity", 1);
		
		this.fireEvent("onChange", [this.currentIndex, this.slides[this.currentIndex], this]);
	}
});