/*
 
 CARROUSEL JAVASCRIPT
 
*/



var carrousel = {
    
    NbSlide : 0,
    NbCurrent : 1,
    elemCurrent : null,
    elem : null,
    timer: null,
    
    
    init : function(elem){
        
       this.NbSlide = elem.find(".slide").length;
       
       //Créer la pagination
       elem.append('<div class="navigation"></div>');
       for(var i=1;i<=this.NbSlide;i++){
            elem.find('.navigation').append('<span>'+i+'</span>');
       }
       elem.find('.navigation span').click(function(){ carrousel.gotoSlide($(this).text());})
       
       
       
       //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 span:first").addClass('active');
       
       //On Crée le 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;}
        this.elemCurrent.fadeOut();
        this.elem.find("#slide"+num).fadeIn();
        this.elem.find(".navigation span").removeClass('active');
        this.elem.find('.navigation span: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);
        
    },
	
	stop:function(){
		window.clearInterval(carrousel.timer);
	},
	
	play:function(){
		carrousel.timer=window.setInterval("carrousel.next()",8000);
	},
    
    
    
    
}

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