//the $$ is required to check for element existance on each transition

var MooSlideshow = new Class({
	initialize: function(container, elements, options){
		this.setOptions({
			"wait": 6000,
			"fxDuration": 4000,
			"fxTransition": Fx.Transitions.Quad.easeOut,
			"onChange": Class.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.elements).each(function(el, index){
			
			el.setStyles({
				"position":"absolute"
			});
			if(index > 0) el.effect("opacity").set(0);
		});
		
		this.effectObj = { 
			"duration": this.options.fxDuration, 
			"transition": this.options.fxTransition, 
			"wait": false,
			"onComplete": (function(){ this.busy = false; }).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].effect("opacity", this.effectObj).start(1,0);
		this.slides[this.currentIndex].effect("opacity", this.effectObj).start(0,1);

		if(this.options.onChange){
			this.options.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].effect("opacity").set(0);
		this.slides[this.currentIndex].effect("opacity").set(1);
		
		if(this.options.onChange){
			this.options.onChange(this.currentIndex, this.slides[this.currentIndex], this);
		}
	}
});

MooSlideshow.implement(new Options);