var SlideShowSimple = new Class({
	mootools:1.2,
	options: {
		slidefx:{transition:Fx.Transitions.Quint.easeOut,duration:200},
		classes:{cell:'cell'},
		container:{width:'450', height:'300',direction:'vertical'},
		timer:3000,
		autostart:true
	},
	initialize: function( mask, container, options){
		if( $defined( options ) ) {this.setOptions(options);}

		this.container = $(container);
		this.mask=$(mask);

		this.ls = $$('#'+mask + ' .'+this.options.classes.cell).length;
		if(this.options.container.direction=='vertical') {
			this.container.setStyles({
				left:'0px',top:'0px',width:this.options.container.width+'px',
				height:(this.options.container.height*this.ls)+'px'
			});
		} else {
			this.container.setStyles( {
				left:'0px',top:'0px',width:(this.options.container.width*this.ls)+'px',
				height:this.options.container.height+'px'
			});
		}
		
		this.mask.addEvent( 'mouseenter', function() {this.doStop();}.bind(this));
		this.mask.addEvent( 'mouseleave', function() {this.doStart();}.bind(this));
		this.addEvent( 'endwait', function(val) {this.doAction('endwait', val);});

		this.container.fx = new Fx.Morph(this.container,{
			duration: this.options.slidefx.duration,
			transition: this.options.slidefx.transition,
			onComplete: function() {this.doAction('endslide');}.bind(this)
		});

		this.stop=false;
		this.eventid=-1;
		this.ci = 0;
		this.step='attente';
		this.doAction('starttimer');
	},
	doAction: function(action, index) {
		switch(this.step) {
			case 'attente' :
				switch(action) {
					case 'endwait' :
						this.doAction('move', this.ci+1>=this.ls?0:this.ci+1);
						break;

					case 'move' :
						if(this.ci==index) {break;}
						
						this.step='slide';
						this.ci=index;
						if(this.options.container.direction=='vertical') {
							this.container.fx.start({top:-(this.options.container.height*index)+'px'});
						} else {
							this.container.fx.start({left:-(this.options.container.width*index)+'px'});
						}
						break;

					case 'starttimer' :
						if((this.ls<=1)||(this.stop==true)||(this.options.autostart==false)) {break;}
						this.eventid = (function(){this.fireEvent('endwait', {}, 0);}.bind(this)).delay(
							this.options.timer>0?this.options.timer:$random(1000, 10000));
						break;
					default :
				}
				break;

			case 'slide' :
				switch(action) {
					case 'endslide' :
						this.step='attente';
						this.doAction('starttimer');
						break;
				}
				break;
			
			default :
		}
	},
	bpNext:function() {
		this.stop=true;
		this.doAction('move', this.ci+1>=this.ls?0:this.ci+1);
	},
	bpPrev:function() {
		this.stop=true;
		this.doAction('move', this.ci-1<0?this.ls-1:this.ci-1);
	},
	switchTo:function(index) {
		if(index<0){return;}
		if(index>=this.ls){return;}
		this.stop=true;
		this.doAction('move',index);
	},
	doStop:function() {
		this.stop=true;
		try {$clear(this.eventid);}catch(e) {}
		this.eventid=-1;
	},
	doStart:function() {
		if(this.stop==false) {return;}
		this.stop=false;
		if(this.eventid==-1) {this.doAction('starttimer');}
	}
});

SlideShowSimple.implement(new Events);
SlideShowSimple.implement(new Options);

