var slider = {
    
    nbSlide : 0,
    nbCurrent : 1,
    elemCurrent : null,
    elem : null,
    timer : null,
    
    init : function(elem){
        this.nbSlide = elem.find(".slide").length;
        
        //Fleches navigation
        elem.append('<span class="controles" id="left"></span><span class="controles" id="right"></span>');
        
        //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(){
            slider.gotoSlide($(this).text());
        });*/
        elem.find("#left").click(function(){
            slider.prev($(this).text());
        });
	elem.find("#right").click(function(){
            slider.next($(this).text()); })
        
        //Initialisation
        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");
        
        //Timer
        slider.play();
        
        //Stop timer mouseover
        elem.mouseover(slider.stop);
        elem.mouseout(slider.play);
    },
    
    gotoSlide : function(num){
        if(num == this.nbCurrent){
            return false;
        }
        /* Fade In / Fade Out
        this.elemCurrent.fadeOut();
        this.elem.find("#slide"+num).fadeIn();
        */
        
        // Slide
        var sens = 1;
        if(num < this.nbCurrent){ sens = -1;}
        var cssDeb = { "left" : sens*this.elem.find(".slide").width() };
        var cssFin = { "left" : -sens*this.elem.find(".slide").width() };
        this.elem.find("#slide"+num).show().css(cssDeb);
        
        this.elem.find("#slide"+num).animate({"left":0},500);
        this.elemCurrent.animate(cssFin,500);
        
        //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 = Number(this.nbCurrent)+1;
        if(num > this.nbSlide){
            num = 1;
        }
        this.gotoSlide(num);
    },
    
        prev : function(){
        var num = Number(this.nbCurrent)-1;
        if(num < 1){
            num = this.nbSlide;
        }
        this.gotoSlide(num);
    },
    
    stop : function(){
        window.clearInterval(slider.timer);
    },
    
    play : function(){
        window.clearInterval(slider.timer);
        slider.timer = window.setInterval("slider.next()",8000);
    }
    
}

var slider2 = {
    
    nbslide2 : 0,
    nbCurrent : 1,
    elemCurrent : null,
    elem : null,
    timer : null,
    
    init2 : function(elem){
        this.nbslide2 = elem.find(".slide2").length;
        
        //Fleches navigation
        elem.append('<span class="controles" id="left2"></span><span class="controles" id="right2"></span>');
        
        //Pagination
        /*elem.append('<div class="navigation"></div>');
        for(var i=1;i<=this.nbslide2;i++){
            elem.find(".navigation").append("<span>"+i+"</span>");
        }
        elem.find(".navigation span").click(function(){
            slider2.gotoslide2($(this).text());
        });*/
        elem.find("#left2").click(function(){
            slider2.prev($(this).text());
        });
	elem.find("#right2").click(function(){
            slider2.next($(this).text()); })
        
        //Initialisation
        this.elem = elem;
        elem.find(".slide2").hide();
        elem.find(".slide2:first").show();
        this.elemCurrent = elem.find(".slide2:first");
        this.elem.find(".navigation span:first").addClass("active");
        
        //Timer
        slider2.play();
        
        //Stop timer mouseover
        elem.mouseover(slider2.stop);
        elem.mouseout(slider2.play);
    },
    
    gotoslide2 : function(num){
        if(num == this.nbCurrent){
            return false;
        }
        /* Fade In / Fade Out
        this.elemCurrent.fadeOut();
        this.elem.find("#slide2"+num).fadeIn();
        */
        
        // slide2
        var sens = 1;
        if(num < this.nbCurrent){ sens = -1;}
        var cssDeb = { "left" : sens*this.elem.find(".slide2").width() };
        var cssFin = { "left" : -sens*this.elem.find(".slide2").width() };
        this.elem.find("#slide2"+num).show().css(cssDeb);
        
        this.elem.find("#slide2"+num).animate({"left":0},500);
        this.elemCurrent.animate(cssFin,500);
        
        //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("#slide2"+num);
    },
    
    next : function(){
        var num = Number(this.nbCurrent)+1;
        if(num > this.nbslide2){
            num = 1;
        }
        this.gotoslide2(num);
    },
    
        prev : function(){
        var num = Number(this.nbCurrent)-1;
        if(num < 1){
            num = this.nbslide2;
        }
        this.gotoslide2(num);
    },
    
    stop : function(){
        window.clearInterval(slider2.timer);
    },
    
    play : function(){
        window.clearInterval(slider2.timer);
        slider2.timer = window.setInterval("slider2.next()",8000);
    }
    
}


var slider3 = {
    
    nbslide3 : 0,
    nbCurrent : 1,
    elemCurrent : null,
    elem : null,
    timer : null,
    
    init3 : function(elem){
        this.nbslide3 = elem.find(".slide3").length;
        
        //Fleches navigation
        elem.append('<span class="controles" id="left3"></span><span class="controles" id="right3"></span>');
        
        //Pagination
        /*elem.append('<div class="navigation"></div>');
        for(var i=1;i<=this.nbslide3;i++){
            elem.find(".navigation").append("<span>"+i+"</span>");
        }
        elem.find(".navigation span").click(function(){
            slider3.gotoslide3($(this).text());
        });*/
        elem.find("#left3").click(function(){
            slider3.prev($(this).text());
        });
	elem.find("#right3").click(function(){
            slider3.next($(this).text()); })
        
        //Initialisation
        this.elem = elem;
        elem.find(".slide3").hide();
        elem.find(".slide3:first").show();
        this.elemCurrent = elem.find(".slide3:first");
        this.elem.find(".navigation span:first").addClass("active");
        
        //Timer
        slider3.play();
        
        //Stop timer mouseover
        elem.mouseover(slider3.stop);
        elem.mouseout(slider3.play);
    },
    
    gotoslide3 : function(num){
        if(num == this.nbCurrent){
            return false;
        }
        /* Fade In / Fade Out
        this.elemCurrent.fadeOut();
        this.elem.find("#slide3"+num).fadeIn();
        */
        
        // slide3
        var sens = 1;
        if(num < this.nbCurrent){ sens = -1;}
        var cssDeb = { "left" : sens*this.elem.find(".slide3").width() };
        var cssFin = { "left" : -sens*this.elem.find(".slide3").width() };
        this.elem.find("#slide3"+num).show().css(cssDeb);
        
        this.elem.find("#slide3"+num).animate({"left":0},500);
        this.elemCurrent.animate(cssFin,500);
        
        //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("#slide3"+num);
    },
    
    next : function(){
        var num = Number(this.nbCurrent)+1;
        if(num > this.nbslide3){
            num = 1;
        }
        this.gotoslide3(num);
    },
    
        prev : function(){
        var num = Number(this.nbCurrent)-1;
        if(num < 1){
            num = this.nbslide3;
        }
        this.gotoslide3(num);
    },
    
    stop : function(){
        window.clearInterval(slider3.timer);
    },
    
    play : function(){
        window.clearInterval(slider3.timer);
        slider3.timer = window.setInterval("slider3.next()",8000);
    }
    
}



$(function(){
    slider.init($("#slideConteneur"));
	slider2.init2($("#slideConteneur2"));
	slider3.init3($("#slideConteneur3"));
});
