SliderManager = Class.create();
Object.extend(SliderManager.prototype,{
	slideList: null,
	contener : null,
	contenerSecondaire : null,
	boutonContener : null,
	slideLocked : false,
	currentNB : 0,
	autoPlay : null,
	autoPlayTimeout : null,
	initialize: function(slideContener,boutonContener,options) {
		this.contener = $(slideContener);
		this.boutonContener = $(boutonContener);
	  //currentNode = this.contener.select(".slideElement")[0];
	  totalWidth = 0;
	  this.slideList= new Array();
	  this.contenerSecondaire = new Element("DIV");
	  this.contener.appendChild(this.contenerSecondaire);
	  currentNb = 0;
	  maxHeight = 0;
	  maxWidth = 0;
	  this.contener.select(".slideElement").each(
	    function(node){
		    //slideElements.push(node);
		    node.setStyle({opacity:0});
	    	//maxHeight = maxHeight < node.getDimensions().height ? node.getDimensions().height : maxHeight;
	    	//maxWidth = maxWidth < node.getDimensions().width ? node.getDimensions().width : maxWidth;
	    	node.setStyle({display:"block",position:"absolute"});
		    this.contenerSecondaire.appendChild(node)
		    totalWidth += this.contener.getWidth();
		    boutton = null;
		    slide = null;
		    if(this.boutonContener.select(".slideButton").length > currentNb)
		    	boutton = this.boutonContener.select(".slideButton")[currentNb];
		    slide = new Slide(node,boutton,currentNb);
		    if(boutton != null)
		    	boutton.observe("click",this.playSlide.bind(this,slide));
		    
		    this.slideList.push(slide);
		    //node.hide();
		    currentNb++;
	    }.bind(this));
	  //this.contener.setStyle({width:maxWidth+"px",height:maxHeight+"px"});
	  this.contenerSecondaire.setStyle({width:totalWidth+"px"});
	  this.slideList[this.currentNB].boutton.addClassName("currentSlideButton");
	  if(options)
		  this.autoPlay = options.autoPlay ? options.autoPlay : null;
			  
	 /*if(this.autoPlay != null)
		 this.autoPlayTimeout = setTimeout(this.nextSlide.bind(this),this.autoPlay*1000);*/
	 this.currentNB = this.slideList.length - 1;
	 this.playSlide(this.slideList[0]);
	},

nextSlide : function (){
  if(this.slideLocked)
    return null;
  //nextNode = this.currentSlide.slideElement.next(".slideElement");
  numToPlay = this.currentNB+1 >= this.slideList.length ? 0 : this.currentNB+1;
  this.playSlide(this.slideList[numToPlay]);
},

	playSlide : function (slide)
	{
	  if(this.slideLocked || slide==this.slideList[this.currentNB])
	    return null;
	  clearTimeout(this.autoPlayTimeout);
	  this.slideLocked = true;
	  currentSlide = this.slideList[this.currentNB];
	  //currentSlide.slideElement.insert({after:slide.slideElement});
	  this.contenerSecondaire.insert({bottom:slide.slideElement});
	    if(currentSlide.boutton)
	    	currentSlide.boutton.removeClassName("currentSlideButton");
	    if(slide.boutton)	    
	    	slide.boutton.addClassName("currentSlideButton");
	  //new Effect.Appear(slide.slideElement, { from:0.0 , to:1.0 , duration: 0.7 });
	  //new Effect.Move(slide.slideElement, { x: -(this.contener.getWidth()), y:0, duration: 1 , afterFinishInternal:function(){
	    new Effect.Appear(currentSlide.slideElement, { from:1.0 , to:0.0 , duration: 0.7 });
	    new Effect.Appear(slide.slideElement, { from:0.0 , to:1.0 , duration: 0.7 , afterFinishInternal:function(){
		currentSlide = this.slideList[this.currentNB]; 
	    //this.contenerSecondaire.insert({bottom:currentSlide.slideElement});
	    //slide.slideElement.setStyle({left:0});
	    this.currentNB = slide.position;
	       
	    this.slideLocked = false;

		 if(this.autoPlay != null)
			 this.autoPlayTimeout = setTimeout(this.nextSlide.bind(this),this.autoPlay*1000);
	  }.bind(this)
	  });
	}
});

Slide = Class.create();
Object.extend(Slide.prototype,{
	slideElement:null,
	boutton:null,
	position:null,
	onClickFunction:null,
	initialize: function(slideElement,boutton,position) {
		this.slideElement = $(slideElement);
		this.boutton = $(boutton);
		this.position = position;
		/*if(onClickFn != null)
			this.onClickFunction = onClickFn;*/
	}
});