/**
 * myCarrousel
*/


var myCarrousel = {
    nbSlide : 0,
    nbCurrent :1,
    elemCurrent : null,
    elem : null,
    timer : null,
    titleTimer : null,
    
    init : function (elem){
        this.nbSlide = elem.find('.slide').length;
        
       /* Creation pagination*/
       elem.append('<div id="carrousel_nav"></div>');
       for(var i=1;i<=this.nbSlide;i++){
            $('#carrousel_nav').append('<span>'+i+'</span>');
       }
        $('#carrousel_nav span').click(function(){myCarrousel.gotoSlide($(this).text())});
    
       
       // initialisation carrousel
       this.elem=elem;
       elem.find('.slide').hide();
       elem.find('.slide:first').show();
       this.elemCurrent=elem.find('.slide:first');
       this.elem.find('#carrousel_nav span:first').addClass('active');
        
       // Démarrage du mode automatique
       myCarrousel.play();
       
        
        // stop au survol de souris       
        //$("#cadrediapo").mouseover(myCarrousel.stop);
        //$("#cadrediapo").mouseout(myCarrousel.play);
        this.elem.mouseover(myCarrousel.stop);
        this.elem.mouseout(myCarrousel.play);
    },

    gotoSlide : function (num){
        if(num==this.nbCurrent) {return false;}
        var slideToGo = this.elem.find('#slide'+num);
        /* Animation de base (Fade)
        this.elemCurrent.fadeOut();
        this.elem.find('#slide'+num).fadeIn();
       */
        
        /* Animation par Glissement */
        var sens = 1;
        if(num<this.nbCurrent){sens = -1};
        var slideWidth=sens*(slideToGo.width()); 
        var cssDep = { "left" : slideWidth};
        var cssFin = { "left" : - slideWidth};
        slideToGo.show().css(cssDep);
        
        var animationSpeed = 1200;
        slideToGo.animate({"top":0 , "left":0},animationSpeed);
        this.elemCurrent.animate(cssFin,animationSpeed);
        //this.titleTimer = window.setTimeout(this.elemCurrent.find('.titre').fadeOut(animationSpeed*0.5),animationSpeed*0.5);
        this.elemCurrent.find('.titre').fadeOut(animationSpeed*0.95); /*  en test*/
        
        /* Animation du titre */
        slideToGo.find('.titre').hide();
        this.titleTimer = window.setTimeout(function(){myCarrousel.titleAnimate(num)},animationSpeed*0.7);
        
        /* FIN - Animation par Glissement */
        
        /* Gestion de la Nav */
        this.nbCurrent=num;
        this.elemCurrent=slideToGo;
        this.elem.find('#carrousel_nav span').removeClass('active');
        this.elem.find('#carrousel_nav span:eq('+(num-1)+')').addClass('active');
    },
    
    titleAnimate : function (num){
        var animDuration = 350;
        var titleHeight = this.elemCurrent.find(' .titre').height();
        
        this.elem.find('#slide'+num+'  .titre').show();
        this.elem.find('#slide'+num+'  .titre').css({"bottom": - titleHeight}).animate({"bottom":0},animDuration*2);
        
    },
    
     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);        
    },
    
    play : function (){
        window.clearInterval(myCarrousel.timer);
        myCarrousel.timer = window.setInterval('myCarrousel.next()',5000);
    },
    
    stop : function(){
        window.clearInterval(myCarrousel.timer);
    }
    
};

$(function(){
    myCarrousel.init($('#carrousel'));
});

