/*

CARROUSEL ACCUEIL HW

*/

var home_carrousel = {
	dureeanim : 500,
	intervanim : 3000,
	nbSlide : 0,
	nbCurrent : 1,
	elem : null,
	timer : null,
	
	init : function(elem){
		this.nbSlide = elem.find(".slide").length;
		this.elem = elem;
		
		//naviguation
		var cssNav = {'width':22*this.nbSlide };
		this.elem.find(".naviguation").css(cssNav);
		for (var i=1; i<=this.nbSlide; i++){
			this.elem.find(".naviguation").append('<span>'+i+'</span>');
			
		}
		elem.find(".naviguation span").click(function(){home_carrousel.gotoSlide($(this).text());});
		this.elem.find('.naviguation span:first').addClass('active');
		
		//init infos (on les affiches et on rgle la largeur)
		this.elem.find('.info').each(function (i) {
			$(this).show();
			var sujet_width = $(this).find(".sujet").outerWidth();
			var client_width = $(this).find(".client").outerWidth();
			var content_left = $(this).find(".sujet").position().left;
			
			if (client_width > sujet_width) content_left += client_width;
			else content_left += sujet_width;
			
			var info_width = content_left + $(this).find(".contenu").outerWidth()+1; //+1 pour le border-left dans firefox
			
			$(this).find(".contenu").css('left', content_left);
			$(this).css('width', info_width);
			
		});
		
		//init slide
		var cssCarContent = {'width':900*this.nbSlide };
		this.elem.find(".car_content").css(cssCarContent);
		
		// creation du timer
		home_carrousel.play();
		
		// stop timer on rollover
		this.elem.mouseover(home_carrousel.stop);
		this.elem.mouseout(home_carrousel.play);
		
		//stop timer quand fentre inactive / play timer quand fentre active
		$(window).focus(home_carrousel.play); 
		$(window).blur(home_carrousel.stop); 
	},
	
	gotoSlide : function(num){
		if (num == this.nbCurrent){return false;}
		
		this.elem.find('.naviguation span').removeClass('active');
		this.elem.find('.naviguation span:eq('+(num-1)+')').addClass('active');
		
		//anim en slide
		var sens = -1;
		if (num < parseInt(this.nbCurrent)) {sens = 1;}

		var cssCarFin = {"left": -900*(num-1) };
		this.elem.find(".car_content").animate(cssCarFin,this.dureeanim);
				
		this.nbCurrent = num;
		
	},
	
	next : function(){
		var num = parseInt(this.nbCurrent)+1;
		if (num > this.nbSlide){
			//alert ("num="+num+" nbslide="+this.nbSlide);
			num=1;
		}
		this.gotoSlide(num);
	},
	
	prev : function(){
		var num = parseInt(this.nbCurrent)-1;
		if (num == 0){
			num=this.nbSlide;
		}
		this.gotoSlide(num);
	},
	
	play : function(){
		home_carrousel.stop();
		home_carrousel.timer = window.setInterval("home_carrousel.next()", home_carrousel.intervanim);
	},
	
	stop : function(){
		window.clearInterval(home_carrousel.timer);
	}

}

$(function(){
	//alert ("JS OK");
	home_carrousel.init($("#carrouselhome"));

});

