var carrousel = {
	nbSlide:0, // nb de slide quil ya 
	nbCurrent:1,  //element courant
	elemCurrent:null,  //element html actuellement en fichier (pour gagner du temps)
	elem:null, //dans quelle div initialiser mon carrousel
	timer:null,

	init : function(elem){
		this.nbSlide=elem.find(".slide").length;  //nb de slide

		//créer la pagination
		elem.append('<div class="navigation"></div>');
		if(this.nbSlide>0){
		elem.find(".navigation").append('<img class="prev" src="/images/prev.png"/>');
		}
		elem.find(".navigation .prev").click(function(){carrousel.prev();})
		
		for (var i=1;i<=this.nbSlide;i++){
			elem.find(".navigation").append('<span class="num">'+i+'</span>');
		}
		elem.find(".navigation .num").click(function(){carrousel.gotoSlide($(this).text());})

		
		if(this.nbSlide>0){
		elem.find(".navigation").append('<img class="next" src="/images/next.png"/>');
		}
		elem.find(".navigation .next").click(function(){carrousel.next();})
		
		//initialisation du carrousel
		this.elem=elem;
		elem.find(".slide").hide();
		elem.find(".slide:first").show();
		this.elemCurrent=elem.find(".slide:first"); 
		this.elem.find(".navigation .num:first").addClass("active");
		//timer
		carrousel.play();
		//stop quand on passe dessus
		elem.mouseover(carrousel.stop);
		elem.mouseout(carrousel.play);
	},
	
	
	gotoSlide:function(num){
	if(num==this.nbCurrent){return false;}
		
				
		/*Animation slide
		var sens=1;
		if(num<this.nbCurrent){sens=-1;}
		var cssDeb={"left":sens*this.elem.width()};
		var cssFin={"left":-sens*this.elem.width()};
		this.elem.find("#slide"+num).show().css(cssDeb);
		this.elem.find("#slide"+num).animate({"top":0,"left":0},500);
		this.elemCurrent.animate(cssFin,500);
		
		*/	
		
		this.elemCurrent.find(".visu" ).fadeOut();
		this.elem.find("#slide"+num).show();
		this.elem.find("#slide"+num+" .visu" ).hide().fadeIn();

		var titleHeight=this.elemCurrent.find(".title" ).height();
		this.elemCurrent.find(".title" ).animate({"bottom":-titleHeight},500);
		this.elem.find("#slide"+num+" .title" ).css("bottom",-titleHeight).animate({"bottom":0},500);
	
		this.elem.find(".navigation .num").removeClass("active");
		this.elem.find(".navigation .num:eq("+(num-1)+")").addClass("active");
		this.nbCurrent=num;
		this.elemCurrent=this.elem.find("#slide"+num);
	},
	
	next:function(){
		var num = (this.nbCurrent)+1;
		if(num >this.nbSlide){
			num=1;
		}
		this.gotoSlide(num);
	},
	prev:function(){
		var num = (this.nbCurrent)-1;
		if(num<1){
			num=this.nbSlide;
		}
		this.gotoSlide(num);
	},
	stop:function(){
	window.clearInterval(carrousel.timer);
	},
	play:function(){
	carrousel.timer=window.setInterval("carrousel.next()",10000);
	}
}

$(function(){
	carrousel.init($("#carrousel"));
});
