SlideShow = Class.create({
	initialize: function(liste,opt){
		//Prise en compte des paramètres
		this.nom = opt.nom;
		this.random = (opt.random)?opt['random']:false;
		this.indicators = (opt.indicators)?opt['indicators']:false;
		this.time = (opt.time)?opt['time']:5;
		this.images = liste.childElements();
		this.i=-1;
		
		liste.setStyle({position:'relative'});
		//préparation des images
		this.images.each(function(elem){
			elem.absolutize();
			
			var offsetleft = (elem.up('div').getWidth()-elem.down('img').getWidth())/2;
			//console.log('H : up->'+elem.up('div').getWidth()+' ; down->'+elem.down('img').getWidth()+' =>'+offsetleft);
			var offsettop = (elem.up('div').getHeight()-elem.down('img').getHeight())/2;
			//console.log('V : up->'+elem.up('div').getHeight()+' ; down->'+elem.down('img').getHeight()+' =>'+offsettop);
			//console.log('image width:'+elem.down('img').getWidth());
			elem.setStyle({'top':offsettop+'px',left:offsetleft+'px',width:elem.down('img').getWidth()+'px',textAlign:'center'});
			elem.hide();
		});
		
		if(this.indicators){
			for(var j = 0; j < this.images.length; j++){
				var posSpan = (this.images[j].getWidth() - 19*this.images.length)/2;
				//console.log('down->'+this.images[j].getWidth()+' ; size->'+(19*this.images.length)+' =>'+posSpan);
				var indicSpan = new Element('div',{style:'position:absolute;bottom:5px;left:'+posSpan+'px;'});
				for(var i = 0; i < this.images.length; i++){
					var nomImg = (i==j)?'on':'off';
					var indicLink = new Element('a', {href:"#",onclick:'javascript:'+this.nom+'.go('+i+');return false;'});
					indicLink.insert({bottom:new Element('img',{src:'/img/helpers/'+nomImg+'.png',style:'margin-left:4px;margin-right:4px;'})});
					indicSpan.insert(indicLink);
					//console.log(this.images[j]);
					delete nomImg;
					delete indicLink;
				}
				this.images[j].insert(new Element('br'));
				this.images[j].insert(indicSpan);
				delete IndicSpan;
				delete posSpan;
			}
		}
		
		//lancement du carroussel
		this.rotate();
	},
	rotate:function(no){
		if(this.i!=-1){
			this.images[this.i].fade({ from: 1, to: 0, duration: 0.5 });
		}
		var old_i=this.i;
		if(no==undefined){
			if(this.random){
				//console.log('random old_i : '+this.nom+' , '+this.i);
				this.i = Math.floor(Math.random() * (this.images.length-1 + 1));
				//console.log('random new_i : '+this.nom+' , '+this.i);
				if (this.i==old_i){
					this.i = (this.i+1)%this.images.length;
				}
			}else{
				this.i=(this.i+1)%this.images.length;
			}
		}else{
			this.i = no;
		}
		eval(this.nom+"sto1=setTimeout(this.nom+'.images['+this.nom+'.i].appear({ from: 0, to: 1, duration: 0.5 })',500)");
		eval(this.nom+"sto2=setTimeout(this.nom+'.rotate()',"+(this.time*1000)+")");
	},
	go:function(no){
		eval("clearTimeout("+this.nom+"sto1)");
		eval("clearTimeout("+this.nom+"sto2)");
		this.rotate(no);
	}
});
