var slideshow = {
	
	nbSlide : 0,
	nbCurrent : 1,
	elemCurrent : null,
	elem : null,
	timer : null,
	
	
	//Fonction d'initialisation
	init : function(elem){
		this.nbSlide = elem.find(".slide").length;
		
		//Création de la pagination
		elem.append('<div class="navigation"></div>');
		for(var i=1;i<=this.nbSlide;i++){
			elem.find(".navigation").append("<div id="+i+"></div>");
		}
		
		
		elem.find(".navigation div").click(function(){ slideshow.gotoSlide($(this).attr("id")); });
		
		//init
		
		this.elem = elem;
		elem.find(".slide").hide();		
		this.elemCurrent = elem.find(".slide:first");
		this.elemCurrent.show();
		this.elem.find(".navigation div:first").addClass("active");
		
		elem.mouseover(slideshow.stop);
		elem.mouseout(slideshow.play);
		
		
		slideshow.play();

	},
	

	
	gotoSlide : function(num){
				
		
		if(num==this.nbCurrent){
			return false; 
		}		
		
		this.nbCurrent = num;
		
		this.elemCurrent.fadeOut();
		
		this.elemCurrent=this.elem.find("#slide"+this.nbCurrent);

		this.elemCurrent.fadeIn();
		
		this.elem.find(".navigation div").removeClass("active");
		this.elem.find(".navigation div:eq("+(this.nbCurrent-1)+")").addClass("active");
				
		
	},
	
	next : function(){
		var num = parseInt(this.nbCurrent)+1;
		
		if(num > this.nbSlide){
			num=1;		
		}
		
		this.gotoSlide(num);	
		
	},
	
	stop : function(){
		window.clearInterval(slideshow.timer);
		slideshow.elem.find("#pause").show();
		
	},
	
	play : function(){
		window.clearInterval(slideshow.timer);
		slideshow.timer=window.setInterval("slideshow.next()",5000);
		slideshow.elem.find("#pause").hide();
	},
	
}

$(function(){

	slideshow.init($("#slideshow"));

})
