//--------------------------------------
// initialisation global ---------------
var jQueryObject = jQuery.noConflict();
jQueryObject(document).ready(function(){

	jQueryObject(document).pngFix();

	initNavPrincipale();

	Cufon.replace(
		'.str_header h2 span.ttr1, .obj_logo-dercos h1', 
		{ fontFamily: 'helvetica' });

	Cufon.replace(
		'.str_header h2 span.ttr2, .obj_logo-dercos h2, .obj_logo-dercos h3',
		{ fontFamily: 'helvetica-light' });

});
//--------------------------------------
// function nav-principale -------------
function initNavPrincipale(){
	jQueryObject(".nav_principale ul").autoWidth();

	positionStart(".nav_principale a.on");
	
	jQueryObject(".nav_principale a").bind('mouseover', function (){
		deplaceBckMenu(this);
	});

	jQueryObject(".nav_principale").bind('mouseleave', function(){
		deplaceBckMenu(".nav_principale a.on");
	});

	function deplaceBckMenu( target ){
		var bckPosition = jQueryObject( target ).parent().position().left + "px";
		var bckWidth = jQueryObject( target ).parent().width() + "px";
		jQueryObject(".nav_principale .bckMenu").stop().animate({left: bckPosition, width: bckWidth},{queue:false, duration:500, easing:"backEaseOut"});
	}

	function positionStart( target ){
		var bckPosition = jQueryObject( target ).parent().position().left + "px";
		var bckWidth = jQueryObject( target ).parent().width() + "px";
		jQueryObject(".nav_principale .bckMenu").css({left: bckPosition, width: bckWidth});
	}

}
//--------------------------------------
// function initInfoBox ----------------
function initInfoBox(){
	//init
	focusOnglet( jQueryObject(".obj_infoBox .nav_items a:first") );
	//
	jQueryObject( ".obj_infoBox .nav_items a" ).mouseover(function(){
		if(jQueryObject(this).attr("class") != "on"){
			jQueryObject(this).next().animate({left:"0px"},{queue:false, duration:250, easing:"circEaseOut"});
		}
	});

	jQueryObject( ".obj_infoBox .nav_items a" ).mouseout(function(){
		if(jQueryObject(this).attr("class") != "on"){
			jQueryObject(this).next().animate({left:"-330px"},{queue:false, duration:250, easing:"circEaseOut"});
		}
	});

	jQueryObject( ".obj_infoBox .nav_items a" ).click(function(){
		if(jQueryObject(this).attr("class") != "on"){
			focusOnglet( this );
		}
	});

	function focusOnglet( target ){
		jQueryObject( ".obj_infoBox .nav_items a.on" ).next().animate({left:"-330px"},{queue:false, duration:250, easing:"circEaseOut"});
		jQueryObject( ".obj_infoBox .nav_items a" ).removeClass('on');
		jQueryObject( target ).addClass('on');
		jQueryObject( target ).next().css({left:"0px"});
		switchOnglet( ".obj_infoBox #" + jQueryObject( target ).attr("name")); 
	}

	function switchOnglet( target ){
		jQueryObject( ".obj_infoBox .item" ).css({zIndex:"1"});
		jQueryObject( ".obj_infoBox .item" ).animate({left:"-100px"}, {queue:false, duration:250, easing:"circEaseOut"});
		jQueryObject( ".obj_infoBox .item" ).children().animate({opacity:"0"}, {queue:false, duration:250, easing:"circEaseOut"});

		jQueryObject( target ).css({zIndex:"100"});
		jQueryObject( target ).animate({left:"0px"}, {queue:false, duration:250, easing:"circEaseOut"});
		jQueryObject( target ).children().animate({opacity:"1"}, {queue:false, duration:250, easing:"circEaseOut"});
	}
}
//--------------------------------------
// function switchView --------------------
function switchView( handler, target, targetHeight, text, newText ){
	if(jQueryObject( handler ).hasClass("down")){
		jQueryObject( handler ).removeClass('down').addClass('up');
		jQueryObject( target ).animate({height: targetHeight + "px"},{queue:false, duration:500, easing:"circEaseOut"});
		jQueryObject( target ).children(".masque").css({display:"none"});
		jQueryObject( handler ).text(newText);

	}else{
		jQueryObject( handler ).removeClass('up').addClass('down');
		jQueryObject( target ).animate({height:"200px"},{queue:false, duration:500, easing:"circEaseOut"});
		jQueryObject( target ).children(".masque").css({display:"block"});
		jQueryObject( handler ).text(text);
	}
}
//--------------------------------------
// function openPopin ------------------
var sParamMailTo;
if (sParamMailTo == undefined)
{
	sParamMailTo = "";
}
function openPopin( idPopin, content ){

	jQueryObject( idPopin ).css({display:"block"});

	jQueryObject('<div id="overlay"></div>').prependTo('body');
	jQueryObject("#overlay").css({opacity:"0"});
	
	if( content != "mailto")
	{
		jQueryObject(idPopin + " #contentLoader").load( "site/pages/includeFiles/" + content, onBeforeLoad );
	}
	if( content == "mailto-diag")
	{
		var sParam = "";
		if (sParamMailTo != "")
		{
			sParam = "?diagappcode=" + sParamMailTo;
		}
		jQueryObject('<iframe src="../site/pages/mailto-diag.asp' + sParam + '" width="410" height="190" scrolling="no" frameborder="0"></iframe>').appendTo(jQueryObject(idPopin + " #contentLoader"));
		onBeforeLoad();	
	}
	else
	{
		jQueryObject('<iframe src="../site/pages/mailto.asp" width="410" height="190" scrolling="no" frameborder="0"></iframe>').appendTo(jQueryObject(idPopin + " #contentLoader"));
		onBeforeLoad();	
	}
	
	function onBeforeLoad()
	{
		jQueryObject("#overlay").animate({opacity:"0.5"},{queue:false, duration:250, easing:"circEaseOut", complete: callBack});
	}

	function callBack(){
		var nMarginLeft = jQueryObject(idPopin + " #contentLoader").children().width() / -2;
		var nMarginTop = jQueryObject(idPopin + " #contentLoader").children().height() / -2;
		jQueryObject( idPopin ).css({marginLeft: nMarginLeft + "px", marginTop: nMarginTop + "px",visibility:"visible"});
		jQueryObject("#overlay").css({height: jQueryObject("#overlay").parent().outerHeight(true) });
	}
	
	//-- fermeture popin
	jQueryObject('#overlay').click( fermeturePopin );
	jQueryObject(idPopin + ' .b_close').click( fermeturePopin );

	function fermeturePopin(){
		jQueryObject( idPopin ).css({display:"none",visibility:"hidden"});
		jQueryObject( "#overlay" ).remove();
		jQueryObject( "#contentLoader" ).children().remove();
	}

}
//--------------------------------------
// function callVideo ------------------
function callVideo(dest, flv, height, width){
	so = new SWFObject('../../include/swf/PlayerVideo.swf','flashID', width, height,'9', "#000000");
	so.addParam('wmode','transparent');
	so.setAttribute("redirectUrl","http://www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash");
	so.addParam('flashvars','sourceVideo=../../ressources/flv/' + flv + '&skinVideo=../../include/swf/skinDercos.swf');
	so.write(dest);
}

//--------------------------------------
// function pg_home --------------------
function fnPageHome(){
	//produit
	jQueryObject(".obj_produit-home span.bckProduit").css({opacity:"0.8"});

	jQueryObject(".obj_produit-home a").mouseover(function(){
		jQueryObject(".obj_produit-home span.bckProduit").animate({opacity:"1"},{queue:false, duration:500, easing:"circEaseOut"});
	});

	jQueryObject(".obj_produit-home a").mouseout(function(){
		jQueryObject(".obj_produit-home span.bckProduit").animate({opacity:"0.8"},{queue:false, duration:500, easing:"circEaseOut"});
	});
	
	//video
	if( jQueryObject.browser.webkit )
	{

	}
	else
	{
		addVideo();
	}

	//cufon
	Cufon.replace(
		'.obj_message h2 .ttr1', 
		{ fontFamily: 'helvetica' });

	Cufon.replace(
		'.obj_message p, .obj_message h2 .ttr2',
		{ fontFamily: 'helvetica-light' });

}
function addMasque()
{
	jQueryObject(".masque").css({display:"block"});
	jQueryObject(".masque").animate({opacity:"1"},{queue:false,duration:1000,easing:"circEaseOut",complete: addVideo});
}
function addVideo()
{
	jQueryObject("<div id='video-1'></div>").appendTo(".ctn_video");
	callVideo('video-1', 'Film_DercosAminexilPro_produit.flv', '382', '650');
}
function destroy()
{
	jQueryObject(".masque").css({display:"none"});
	jQueryObject("#video-1").remove();
}
//--------------------------------------
// function pg_diagnostic --------------
function fnPageDiagnostic(){

	jQueryObject(".obj_liProd a").bind('mouseover', function(){
		jQueryObject(this).children("img").stop().animate({opacity:"0.1"},{queue:false, duration:200, easing:"circEaseOut"});
		jQueryObject(this).children(".ctn_over").css({visibility:"visible"});
	});

	jQueryObject(".obj_liProd a").bind('mouseout', function(){
		jQueryObject(this).children("img").stop().animate({opacity:"1"},{queue:false, duration:200, easing:"circEaseOut"});
		jQueryObject(this).children(".ctn_over").css({visibility:"hidden"});
	});
	

	//carrousel diagnostic-2
	var nbEtapes = Math.ceil(jQueryObject("ul.obj_liProd li").size() / 3);
	var compteur = 0;

	jQueryObject(".carrousel-product .b_prev").css({visibility:"hidden"});

	if(nbEtapes <= 1){
		jQueryObject(".carrousel-product .b_next").css({visibility:"hidden"});
	}

	jQueryObject(".carrousel-product .b_next").click(function(){
		compteur ++;
		controlCount();

		var position = compteur * -600;
		jQueryObject(".carrousel-product .obj_liProd").animate({left: position + "px"},{queue:false, duration:250, easing:"circEaseOut"});
	});

	jQueryObject(".carrousel-product .b_prev").click(function(){
		compteur --;
		controlCount();

		var position = compteur * -600;
		jQueryObject(".carrousel-product .obj_liProd").animate({left: position + "px"},{queue:false, duration:250, easing:"circEaseOut"});
	});

	function controlCount(){
		jQueryObject(".carrousel-product .b_next, .carrousel-product .b_prev").css({visibility:"visible"});
		if( compteur == ( nbEtapes - 1)) jQueryObject(".carrousel-product .b_next").css({visibility:"hidden"});
		if( compteur == 0 ) jQueryObject(".carrousel-product .b_prev").css({visibility:"hidden"});
	}

	//cufon
	Cufon.replace(
		'.str_top h1, .str_top h2,.str_content .ctn_texte h3', 
		{ fontFamily: 'helvetica-light' });

}
//--------------------------------------
// function pg_produit -----------------
function fnPageProduit(){

	//cufon
	Cufon.replace(
		'.obj_efficacite h2', 
		{ fontFamily: 'helvetica' });

	//carrousel
	jQueryObject(".obj_carrousel .nav_items a").css({opacity:"0.5"});
	jQueryObject(".obj_carrousel .ctn_video").children("div").css({display:"none"});

	jQueryObject(".obj_carrousel .nav_items a").mouseover(function(){
		jQueryObject(this).css({zIndex:"100"});
		jQueryObject(this).stop().animate({width:"100px", height:"135px",top:"-25px", left:"-25px", opacity:"1"}, {queue:false, duration:200, easing:"circEaseOut"});

	});

	jQueryObject(".obj_carrousel .nav_items a").mouseout(function(){
		jQueryObject(this).css({zIndex:"1"});
		jQueryObject(this).stop().animate({width:"50px", height:"67px", top:"0px", left:"0px", opacity:"0.5"}, {queue:false, duration:200, easing:"circEaseOut"});

	});

	jQueryObject(".obj_carrousel .nav_items a").click(function(){
		jQueryObject(".obj_carrousel .ctn_video").children("div").remove();

		switch(jQueryObject(this).attr("name")){
			case "4":
				jQueryObject("<div id='video-1'></div>").appendTo(".obj_carrousel .video-1");
				callVideo('video-1', 'Film_DercosAminexilPro_produit_519x305.flv', '305', '519');
				break;
			default:
		}

		var position = 520 * jQueryObject(this).attr("name");
		jQueryObject(".obj_carrousel .items").animate({left:"-" + position + "px"}, {queue:false, duration:400, easing:"circEaseOut"});
	});

	//scroll pane
	jQueryObject(".jQueryScroll").jScrollPane({showArrows:false,scrollbarWidth:11,scrollbarMargin:10,arrowSize:15});
}
//--------------------------------------
// function pg_efficacite --------------
function fnPageEfficacite(){

	var targetHeight = jQueryObject(".obj_temoignages .ctn_temoignages").height();

	if( targetHeight > 200 ){
		jQueryObject(".obj_temoignages .ctn_temoignages").css({height:"200px"});
		jQueryObject(".b_fleche").click( function(){ switchView(this, ".obj_temoignages .ctn_temoignages", targetHeight, "Bekijk alle persoonlijke ervaringen", "Verberg de persoonlijke ervaringen") });
	}else{
		jQueryObject(".obj_temoignages .masque").remove();
		jQueryObject(".b_fleche").remove();
	}

	//cufon
	Cufon.replace(
		'.str_top .str_right h2, .str_top .str_right h3, .str_top .obj_efficacite h2, .str_top .obj_efficacite h3', 
		{ fontFamily: 'helvetica-light' });

	// carrousel
	jQueryObject(".str_top .nav_items a:eq(0)").addClass('on');

	jQueryObject(".str_top .nav_items a").click(function(){
		jQueryObject(".str_top .nav_items a").removeClass('on');
		jQueryObject(this).addClass('on');

		var attrName = jQueryObject(this).attr("name");
		var position = attrName * -600;
		jQueryObject("#car-2 .items").animate({left: position + "px" },{queue:false, duration:250, easing:"circEaseOut"});
	});

	//scroll pane
	jQueryObject(".jQueryScroll").jScrollPane({showArrows:true,scrollbarWidth:11,scrollbarMargin:10,arrowSize:7});
}
//--------------------------------------
// function pg_conseils ----------------
function fnPageConseils(){
	jQueryObject(".obj_question").each(function(){
		var targetHeight = jQueryObject(this).children(".ctn_question").height();
		var text = jQueryObject(this).children(".ctn_options").children(".b_fleche").text();

		if( targetHeight > 200 ){
			jQueryObject(this).children(".ctn_question").css({height:"200px"});
			jQueryObject(this).children(".ctn_options").children(".b_fleche").click( function(){ switchView(this, jQueryObject(this).parent().prev(), targetHeight, "Alle vragen bekijken", "De vragen verbergen") });
		}else{
			jQueryObject( this ).children().children( ".masque" ).remove();
			jQueryObject( this ).children(".ctn_options").children( ".b_fleche" ).remove();
		}

	});

	jQueryObject(".nav_onglet a").mouseout(function(){
		if(jQueryObject(this).hasClass('on') == false){
			jQueryObject(this).stop().animate({paddingBottom:"5px", marginTop:"14px"},{queue:false, duration:100, easing:"circEaseOut"});
		}
	});

	jQueryObject(".nav_onglet a").click(function(){
		jQueryObject(".str_left").css({display:"none"});
		jQueryObject(".nav_onglet a").removeClass('on');
		jQueryObject(".nav_onglet a").css({paddingBottom:"5px", marginTop:"14px"});

		jQueryObject( this ).css({paddingBottom:"10px", marginTop:"0px"});
		jQueryObject( this ).addClass('on');
		jQueryObject( "#" + jQueryObject(this).attr("name") ).css({display:"block"});
	});

	// carrousel
	jQueryObject(".obj_carrousel").each(function(){
		jQueryObject( this ).children(".masque").css({height: jQueryObject( this ).children(".masque").children(".items").children(".obj_article:eq(0)").height() + "px"});
		jQueryObject(this).children(".nav_items").children("a:first").addClass("on");
		
		jQueryObject(this).children(".nav_items").children("a").click(function(){
			jQueryObject(this).parent(".nav_items").children("a").removeClass("on");
			jQueryObject(this).addClass("on");

			jQueryObject( this ).parent(".nav_items").prev(".masque").animate(
				{height: jQueryObject( this ).parent(".nav_items").prev(".masque").children(".items").children(".obj_article:eq(" + jQueryObject(this).attr("name") + ")").height() + "px"},
				{queue:false, duration:250, easing:"circEaseOut"}
				);
			
			var position = jQueryObject(this).attr("name") * -540;
			jQueryObject( this ).parent(".nav_items").prev(".masque").children(".items").animate({left: position + "px"}, {queue:false, duration:500, easing:"circEaseOut"});
		});

	});

	//onglet + changement page
	jQueryObject(".str_left:gt(0)").css({display:"none"});
	jQueryObject(".nav_onglet li:first a").addClass('on');

	jQueryObject(".nav_onglet a").mouseover(function(){
		if(jQueryObject(this).hasClass('on') == false){
			jQueryObject(this).stop().animate({paddingBottom:"15px", marginTop:"4px"},{queue:false, duration:100, easing:"circEaseOut"});
		}
	});

	//cufon
	Cufon.replace(
		'h1, .str_content h2', 
		{ fontFamily: 'helvetica-light' });

}
//--------------------------------------
// function pg_forum -------------------
function fnPageForum(){
	jQueryObject(".entete").each(function(){
		var newHeight = jQueryObject(this).height();
		jQueryObject(this).parents(".ctn_table").css({height: newHeight + "px"});
		jQueryObject(this).addClass('off');
	});

	jQueryObject(".entete").click(function(){
		if(jQueryObject(this).hasClass("off")){
			var newHeight = jQueryObject(this).parents("table").outerHeight(true);
			jQueryObject(this).parents(".ctn_table").stop().animate({height: newHeight + "px"},{queue:false, duration:250, easing:"circEaseOut"});
			jQueryObject(this).removeClass('off');
			jQueryObject(this).children(".col4").children("a").text("- fermer");
		}else{
			var newHeight = jQueryObject(this).outerHeight(true);
			jQueryObject(this).parents(".ctn_table").stop().animate({height: newHeight + "px"},{queue:false, duration:250, easing:"circEaseOut"});
			jQueryObject(this).addClass('off');
			jQueryObject(this).children().children("a").text("+ ouvrir");
		}
	});

	Cufon.replace(
		'h1', 
		{ fontFamily: 'helvetica-light' });
	Cufon.replace(
		'.entete-forum h2', 
		{ fontFamily: 'helvetica' });
}
