/*
    CARROUSEL JS
*/


var carrousel = {
    
    nbSlide : 0,
    nbCurrent : 1,
    elemCurrent : null,
    elem : null,
    timer : null,
    strNavHTML : null,
    
    init : function(elem){
        //alert ('lancement carrousel');
        this.nbSlide = elem.find(".slide").length;

        // Créer la pagination
        this.strNavHTML = '<div class="navigation">'; 
        this.strNavHTML += '<div class="bouton"><span class="pause"></span></div>';
        this.strNavHTML += '</div>';        
        elem.append(this.strNavHTML);
	
	//au moment du clique sur un bouton
	elem.find('.navigation .bouton').toggle(
	    function(){
		//alert('pause');
		$(this).children('span').removeClass('pause');
		$(this).children('span').addClass('play');
		carrousel.stop();
	    },
	    function(){
		//alert('stop');
		$(this).children('span').removeClass('play');
		$(this).children('span').addClass('pause');
		carrousel.play();
	    }
	);
        
        // Initialisation du carrousel
        this.elem=elem;
        elem.find(".slide").hide();
        elem.find(".slide:first").show();
        this.elemCurrent = elem.find(".slide:first");
        
        // On cré le timer
        carrousel.play();
    },
    
    gotoSlide : function(num){
        if(num==this.nbCurrent){ return false; }
	
        this.elemCurrent.find(".visu").fadeOut();
        this.elem.find("#slide"+num).show();
        this.elem.find("#slide"+num+" .visu").hide().fadeIn('slow');
	
	var titleHeight = this.elemCurrent.find(".titre").height();
	//alert (titleHeight);
	//this.elemCurrent.find(".titre").animate({"bottom": -titleHeight},500);
	this.elemCurrent.find(".titre").fadeOut('slow');
	this.elem.find("#slide"+num+" .titre").fadeIn('slow');
	//this.elem.find("#slide"+num+" .titre").css("bottom", -titleHeight).animate({"bottom": 0},500);
		
        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);
    }
}

