/*

    CARROUSEL JS
    
*/

var carrousel = {

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

    init : function(elem){
        this.nbSlide = elem.find(".slide").length;
        
        // pagination
        elem.append('<div class="slidenav"></div>');
        for(var i=1;i<=this.nbSlide;i++){
            elem.find(".slidenav").append("<span>"+i+"</span>");
        }
        elem.find(".slidenav span").click(function(){ carrousel.gotoSlide($(this).text())});
        //Initialisation
        this.elem=elem;
        elem.find(".slide").hide();
        elem.find(".slide:first").show();
        this.elemCurrent = elem.find(".slide:first");
        this.elem.find(".slidenav span:first").addClass("active");
        

        //Timer
        carrousel.play();
        
        //stop on over and play on out
        elem.mouseover(carrousel.stop);
        elem.mouseout(carrousel.play);

    },
    
    gotoSlide : function(num){
        if(num==this.nbCurrent){ return false; }
        
        /* Animation fadeIn and fadeOut */
        this.elemCurrent.fadeOut('slow');
        this.elem.find(".slide:eq("+(num-1)+")").fadeIn('slow');
        
        /* Animation à debuger
        this.elemCurrent.find(".visuel").fadeOut();
        this.elem.find(".slide:eq("+num+")").show();
        this.elem.find(".slide:eq("+num+") .visuel").hide().fadeIn();
        
        var descriptionHeight = this.elemCurrent.find(".description").height();
        
        this.elemCurrent.find(".description").animate({"bottom": -descriptionHeight},500);
        this.elem.find(".slide:eq("+num+") .description").css("bottom",-descriptionHeight).animate({"bottom":0},500);
        */        
        
        this.elem.find(".slidenav span").removeClass("active");
        this.elem.find(".slidenav span:eq("+(num-1)+")").addClass("active");
        this.nbCurrent = num;
        this.elemCurrent = this.elem.find(".slide:eq("+(num-1)+")");

    },
    
    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<this.nbSlide){
            num = this.nbSlide;
        }
        this.gotoSlide(num);
    },
    
    stop : function(){
        window.clearInterval(carrousel.timer);
    },
    
    play : function(){
        window.clearInterval(carrousel.timer);
        carrousel.timer = window.setInterval("carrousel.next()",8000);
    }

}


$(function(){

	// Initialisation du carrousel
	carrousel.init($("#carrousel"));

	// Initialisation du menu catalogue
	$("ul#catalogue").simpletreeview();

	// setup ul.tabs to work as tabs for each div directly under div.panes
	$("ul.tabs").tabs("div.panes > div");

	// Fixed Sidebar	
	var msie6 = $.browser == 'msie' && $.browser.version < 7;
  
	if (!msie6) {
		var top = $('#sidebar').offset().top - parseFloat($('#sidebar').css('margin-top').replace(/auto/, 0));
		$(window).scroll(function (event) {
			// what the y position of the scroll is
			var y = $(this).scrollTop();

			// whether that's below the form
			if (y >= top) {
				// if so, change the top position
				var positionTop = y - top + 1;
				$('#sidebar').stop().animate({'top' : positionTop+'px'}, 500);
			} else {
				// otherwise leave it null
				$('#sidebar').stop().animate({'top' : '0'}, 500);
			}
		});
	};

});

