/*
    CARROUSEL JS
*/


var carrousel = {

    nbSlide : 0,
    nbCurrent : 1,
    elemCurrent : null,
    elem : null,
    timer : null,

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

        // ajoute les fleches de navigation
        elem.append('<div class="nav_suiv"></div>');
        $(".nav_suiv").click(function(){ carrousel.next(); })

        elem.append('<div class="nav_prec"></div>');
        elem.find(".nav_prec").click(function(){ carrousel.prev(); })

        // Initialisation du carrousel
		$(".title").css("opacity",0.6); 	//-- rend le sous titre un peu transparent
		$("#fond_caroussel").css("opacity",0.5); 	//-- rend le fond transparent
        this.elem=elem;
        elem.find(".slide").hide();
        elem.find(".slide:first").show();
        this.elemCurrent = elem.find(".slide:first");
		$("#carrousel").show();				//-- rend le caroussel visible (pour eviter effet de superposition des elements)
		
        // 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; }

        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);

        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.nbCurrent = num;
        this.elemCurrent = this.elem.find("#slide"+num);
    },

    next : function(){
		sens=1;
        var num  = this.nbCurrent+1;
        if(num  >this.nbSlide){
            num  = 1;
        }
        this.gotoSlide(num);
    },
    prev : function(){
		sens=-1;
        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()",4000);
    }

}

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


