/*
    CARROUSEL JS
*/

var carrousel = {
    
    nbSlide : 0,
    nbCurrent : 1,
    elemCurrent : null,
    elem : null,
    timer : null,
    topSlide : 0,
    leftSlide : 0,

    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("<span slide='"+i+"'></span>");
        }*/
        elem.find(".navigation a").click(function() { carrousel.gotoSlide($(this).attr("name")); return false; });

        // Initialisation du système 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 démarre le carrousel en automatique
        carrousel.play();

        // Stop quand on passe dessus et relance quand on sort du truc

        elem.mouseover(carrousel.stop);
        elem.mouseout(carrousel.play);
    },

    gotoSlide : function(num) {
        if(num==this.nbCurrent){ return false;}

        /* animation en Fade */
       /* this.elemCurrent.fadeOut();
        this.elem.find("#slide"+num).fadeIn();*/

        /* Animation en slide */
        var sens = 1;
        //if(num<this.nbCurrent) { sens = -1;};
        var cssDeb = { "left" : sens*this.elem.width()};
        var cssFin = { "left" : -sens*this.elem.width()};
                /*var cssDeb = { "left" : this.elem.width()};
                var cssFin = { "left" : -this.elem.width()};*/
        this.elem.find("#slide"+num).show().css(cssDeb);
        
        this.elem.find("#slide"+num).animate({"top":topSlide,"left":leftSlide},500);
        this.elemCurrent.animate(cssFin,500);
        
        /* animation du titre aussi
        this.elemCurrent.find(".visu").fadeOut();
        this.elem.find("#slide"+num).show();
        this.elem.find("#slide"+num+" .visu").hide().fadeIn();
        
        var titleHeight = this.elem.find("#slide"+num+" .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 a").removeClass("active active2 active3 active4");
        this.elem.find(".navigation a: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() {
        window.clearInterval(carrousel.timer);
        carrousel.timer = window.setInterval("carrousel.next()",5000);
    },

    changeTop : function(changeTop) {
        topSlide = changeTop;
    },

    changeLeft : function(changeLeft) {
        leftSlide = changeLeft;
    }
}

$(function() {
    carrousel.init($('#carrousel'));
    //alert(carrousel.nbSlide);
    carrousel.changeTop(0);
    carrousel.changeLeft(0);
});

