/*
 *
 * init commun
 *
 */
var jQueryObject = jQuery.noConflict();
jQueryObject(document).ready(function(){

	initNavPrincipale();
	jQueryObject(document).pngFix();

});

/*
 *
 * function common
 *
 */
function initNavPrincipale(){
	jQueryObject(".nav ul").autoWidth();
	jQueryObject(".nav").lockTarget();
}

function removePreloader( callBack )
{
	var nTotalImg = jQueryObject('.str_content img').length;
	var nImgLoaded;
	var intervalTimer;
	var sCallBack = callBack;
	countImgLoaded();
	
	function countImgLoaded()
	{
		nImgLoaded = 0;
		jQueryObject('.str_content img').each(function()
		{
			if(jQueryObject(this).height() > 0) nImgLoaded ++;
		});
		
		if(nImgLoaded == nTotalImg ) 
		{
			jQueryObject('span.obj_loader').remove();
			clearInterval(intervalTimer);
			if( sCallBack == "home" ) fnHome();
		}
		else 
		{
			intervalTimer = setInterval(countImgLoaded, 2000);
		}
	}
}
/*
 * Open popin
 */
function openPopin( idPopin, content ){

	
	//--
	jQueryObject('<div id="overlay"></div>').prependTo('body');
	jQueryObject("#overlay").css({opacity:"0"});
	jQueryObject("#overlay").animate({opacity:"0.5"},{queue:false, duration:250, easing:"circEaseOut", complete: callBack});

	function callBack(){
		jQueryObject( idPopin ).css({display:"block"});
		jQueryObject(idPopin + " #contentLoader").load( "../../" + content );
		jQueryObject("#overlay").css({height: jQueryObject("#overlay").parent().outerHeight(true) });
	}

	jQueryObject('#overlay').click( fermeturePopin );

	jQueryObject(idPopin + ' .b_close').click( fermeturePopin );

	function fermeturePopin(){
		jQueryObject( idPopin ).css({display:"none"});
		jQueryObject( "#overlay" ).remove();
		jQueryObject(idPopin + " #contentLoader").children().remove();
	}

}

/*
 *
 * function par page
 *
 */
function fnHome()
{
	oCH = new CarrouselHome('oCH','.carrousel-home');
	jQueryObject('.obj_interstitiel').delay(2000).animate(
		{opacity:"0"},
		{
			queue:true, 
			duration:500, 
			easing:"circEaseIn",
			complete: function()
			{
				jQueryObject('.obj_interstitiel').css({display:"none"});
				oCH.play();
			}
		});
	// Cufon
	Cufon.replace(
		'.ctn_texte h1', 
		{ fontFamily: 'helvetica' });
	Cufon.replace(
		'.ctn_texte h2', 
		{ fontFamily: 'helvetica', lineHeight: "28px" });
}

function fnProduits()
{
				
	jQueryObject('.visuel').mouseover(function()
	{
		var margLeft = jQueryObject(this).next().outerWidth(true) / 2;
		var posLeft = jQueryObject(this).position().left + jQueryObject(this).width() / 2;
		jQueryObject(this).next().css({display:'block', left: posLeft + 'px', marginLeft: "-" + margLeft + "px"})
	}).mouseout(function()
	{
		jQueryObject(this).next().removeAttr('style');
	});
	
	jQueryObject('.product .b_bloc').mouseover(function()
	{
		var margLeft = jQueryObject(this).outerWidth(true) / 2;
		var posLeft = jQueryObject(this).prev().position().left + jQueryObject(this).prev().width() / 2;
		jQueryObject(this).css({display:'block', left: posLeft + 'px', marginLeft: "-" + margLeft + "px"})
	}).mouseout(function()
	{
		jQueryObject(this).removeAttr('style');	
	});
	
	Cufon.replace(
		'.ctn_info h3, .ctn_info h4, h1, .gamme h2, .product h4', 
		{ fontFamily: 'helvetica' });
}

function fnProduct()
{
	Cufon.replace(
		'.ctn_info h3, .ctn_info h4, h1, .ctn_info h2, .ctn_info h6', 
		{ fontFamily: 'helvetica' });
}

function fnPromotion()
{
	Cufon.replace(
		'h1, h2', 
		{ fontFamily: 'helvetica' });
}

function fnConcours()
{
	Cufon.replace(
		'h1', 
		{ fontFamily: 'helvetica' });
	Cufon.replace(
		'h2, h2 + p', 
		{ fontFamily: 'helveticaNeue' });
}

function fnStorelocator()
{
	jQueryObject('.jQueryScroll').jScrollPane({showArrows:false,scrollbarWidth:11,scrollbarMargin:10,arrowSize:15});
	
	jQueryObject(".obj_liste-1 li").mouseover(function(){
		jQueryObject(this).addClass('over');
	});

	jQueryObject(".obj_liste-1 li").mouseout(function(){
		jQueryObject(this).removeClass('over');
	});
	
	Cufon.replace(
		'h1', 
		{ fontFamily: 'helvetica' });
}
 
/*
 *
 * class CarrouselProduct
 *
 */
function CarrouselProduct( occurence, idCarrousel )
{
	
	var sOcurrence;
	var sIdCarrousel;
	var nCarrouselWidth;
	var nCurrentPosition;
	var nNextPosition;
	var nPosMax;

		/*
		 * Constructeur
		 */
			function constructeur()
			{
				sOccurence = occurence;
				sIdCarrousel = idCarrousel;
				nCarrouselWidth = 560;
				nCurrentPosition = 0;
				nPosMax = (jQueryObject(sIdCarrousel + ' .gamme').length - 1) * -nCarrouselWidth;
				controlPosition();
			}
			constructeur();
		/*
		 * public function
		 */
		 	this.moveItems = function( classDirection )
			{
				moveItems( classDirection );
			}
			this.moveToGamme = function( indexGamme )
			{
				moveToGamme( indexGamme );
			}
			
		/*
		 * private function
		 */
		 	function moveItems( classDirection )
			{
				if( classDirection == ".b_prev" ) nNextPosition = nCurrentPosition + nCarrouselWidth; 
				else nNextPosition = nCurrentPosition - nCarrouselWidth;
				
				nCurrentPosition = nNextPosition;
				controlPosition();
				
				jQueryObject(sIdCarrousel + ' .items').animate(
					{left: nNextPosition + "px" },
					{
						queue:false,
						duration:500,
						easing:"circEaseInOut"
					});
			}
			
			function moveToGamme( indexGamme )
			{
				
				var nDuration = indexGamme * 500;
				nNextPosition = nCarrouselWidth * indexGamme * -1;
				nCurrentPosition = nNextPosition;
				controlPosition();
				
				jQueryObject(sIdCarrousel + ' .items').css({left: nNextPosition + "px" });
			}
			
			function disabledButton( classDirection )
			{
				jQueryObject( sIdCarrousel + ' ' + classDirection).attr('href','javascript:void(0);');
				jQueryObject( sIdCarrousel + ' ' + classDirection).addClass('b_disabled');	
			}
			
			function enabledButton( classDirection )
			{
				jQueryObject( sIdCarrousel + ' ' + classDirection).attr('href','javascript:'+ sOccurence +'.moveItems("'+ classDirection +'")');
				jQueryObject( sIdCarrousel + ' ' + classDirection).removeClass('b_disabled');	
			}
			
			function controlPosition()
			{
				if( nCurrentPosition == 0 )
				{
					disabledButton('.b_prev');
					enabledButton('.b_next');	
				}
				else if( nCurrentPosition == nPosMax)
				{
					disabledButton('.b_next');
					enabledButton('.b_prev');
				}
				else
				{
					enabledButton('.b_next');
					enabledButton('.b_prev');
				}
				
			}
			
			
}
/*
 *
 * class CarrouselHome
 *
 */
function CarrouselHome(occurence,idCarrousel)
{
		var sIdCarrousel;
		var nCompteur;
		var totalItem;
		var intervalTimer;
		var sOccurence;
		
		/*
		 * Constructeur
		 */
			function constructeur()
			{
				sIdCarrousel = idCarrousel;
				totalItem = jQueryObject(sIdCarrousel + ' .item').length;
				nCompteur = 0;
				sOccurence = occurence;
				
				jQueryObject(sIdCarrousel + ' .item').css({display:"none"});
			}
			constructeur();
		/*
		 * public function
		 */
		 	this.play = function()
			{
				initItem();
			}
		 
			this.changeItem = function( userTargetItem )
			{
				enabledButton(nCompteur);
				nCompteur = userTargetItem;
				initItem();
			}
			
		/*
		 * private function
		 */
		 
			function initItem()
			{
				jQueryObject(sIdCarrousel + ' .item').css({display:"none"});
				jQueryObject(sIdCarrousel + ' .item .ctn_texte').css({opacity:"0"});
				jQueryObject(sIdCarrousel + ' .item img').css({opacity:"0"});
				showItem();
			}
			 
			function showItem()
			{
				disabledButton( nCompteur );
				jQueryObject(sIdCarrousel + ' .item:eq('+ nCompteur +')').css({display:"block"});
				jQueryObject(sIdCarrousel + ' .item:eq('+ nCompteur +') .ctn_texte').stop().animate(
					{opacity:"1"},
					{
						queue:false,
						duration:1500,
						easing:"circEaseOut"
					});
				jQueryObject(sIdCarrousel + ' .item:eq('+ nCompteur +') img').stop().animate(
					{opacity:"1"},
					{
						queue:false,
						duration:1500,
						easing:"circEaseOut",
						complete: function()
						{
							restartSetInterval();	
						}
					});
			}
			
			function hideItem()
			{
				stopIntervalTimer();
				
				jQueryObject(sIdCarrousel + ' .item:eq('+ nCompteur +') .ctn_texte').animate(
					{opacity:"0"},
					{
						queue:false,
						duration:1500,
						easing:"circEaseIn"
					});
				jQueryObject(sIdCarrousel + ' .item:eq('+ nCompteur +') img').animate(
					{opacity:"0"},
					{
						queue:false,
						duration:1500,
						easing:"circEaseIn",
						complete: function()
						{
							enabledButton( nCompteur );
							incrementeCompteur();
							initItem();
						}
					});
					
			}
			
			function incrementeCompteur()
			{
				nCompteur += 1;
				if( nCompteur == totalItem ) nCompteur = 0;
			}
			
			function restartSetInterval(){
				clearInterval(intervalTimer);
				intervalTimer = setInterval(hideItem, 5000);
			}
			
			function stopIntervalTimer()
			{
				clearInterval(intervalTimer);
			}
			
			function disabledButton( target )
			{
				jQueryObject(sIdCarrousel + ' .nav-carrousel li:eq(' + target + ') a').removeAttr('href');	
				jQueryObject(sIdCarrousel + ' .nav-carrousel li:eq(' + target + ') a').addClass('b_disabled');
			}
			
			function enabledButton( target )
			{
				jQueryObject(sIdCarrousel + ' .nav-carrousel li:eq(' + target + ') a').attr('href', 'javascript:' + sOccurence + '.changeItem(' + nCompteur + ')');
				jQueryObject(sIdCarrousel + ' .nav-carrousel li:eq(' + target + ') a').removeClass('b_disabled');
			}
					
}
