// JavaScript Document

//Test Modification ptsan 09/07/08




$('document').ready(function(){
	selectList();
	listCarousel();
	displayFunctions();
	tabNav();
	intiCoaching();
	initShowLayer();
	initFlashNav();
	initShowValue();
	setPopinEvent();
	dossier();
	closePopup();
	openPopup();
	showItem();
});

/* Valeur des imputs */
function initShowValue(){
	inputValue(".valueMail","Votre E-mail");
	inputValue(".valuePass","Votre mot de passe");
}

/* fonction generique de test d'input */

function inputValue(input,valueField){
	$(input).each(function(){
		$(this).click(function(){
			if($(this).attr("value") == valueField){
				this.value = "";
			}		 
		});
		$(this).blur(function(){
			if($(this).attr("value") == ""){
				this.value = valueField;
			}							  
		});
	});
}

/* Flash Saga MAGGI */
function initFlashNav(){
	if($("#maggiHistoryFlash").length){
		swfobject.embedSWF("/App_Themes/Common/swf/maggi_saga.swf", "maggiHistoryFlash", "640", "540", "8.0.0", "/App_Themes/Common/swf/expressInstall.swf");	
		
		$("#navigation a.sagaLinkFlash").each(function(i){
			$(this).click(function(){
				$("#maggiHistoryFlash")[0].showCategory(i+1);
				$(this).parent("li").siblings().removeClass("active");
				$(this).parent("li").addClass("active");
				return false;
		   });
		});
	}
}

function categoryChanged(index){
	var listLinkSaga = $("#navigation a.sagaLinkFlash");
	$(listLinkSaga[index-1]).parent("li").siblings().removeClass("active");
	$(listLinkSaga[index-1]).parent("li").addClass("active");
}

/* Flash video */
function flashRecipe(urlImg, urlFlv ){

	var flashRecipeBlock = $("#flashRecipe");
	if (flashRecipeBlock.length){
		var flashvars = {
			confFile: "/App_Themes/Common/xml/conf.xml",
			siteMainFile: "/App_Themes/Common/swf/player.swf",
			preview: urlImg,
			media: urlFlv
		};
		
		var params = {
		  allowScriptAccess: "always"
		};
		
		var attributes = {};
		
		swfobject.embedSWF("/App_Themes/Common/swf/loader.swf", "flashRecipe", "367", "290", "8.0.0", "/App_Themes/Common/swf/expressInstall.swf", flashvars, params, attributes);
	}	
}
/* Flash video Bouillons */
function flashBouillons(urlImg, urlFlv ,id){

	var flashRecipeBlock = $("#"+id);
	if (flashRecipeBlock.length){
		var flashvars = {
			confFile: "/App_Themes/Common/xml/conf.xml",
			siteMainFile: "/App_Themes/Common/swf/player.swf",
			preview: urlImg,
			media: urlFlv
		};
		
		var params = {
		  allowScriptAccess: "always"
		};
		
		var attributes = {};
		
		swfobject.embedSWF("/App_Themes/Common/swf/loader.swf", id, "367", "290", "8.0.0", "/App_Themes/Common/swf/expressInstall.swf", flashvars, params, attributes);
	}	
}

/* transformation de listes en selects */
function selectList(){
	$(".selectList").each(function(){
		var selectTxt = '<select><option selected="selected">'+$(this).find(".titleSelect").html()+'</option>';
		$(this).find("a").each(function(){
			 selectTxt += '<option value="'+$(this).attr("href")+'">'+$(this).html()+'</option>';
		});
		$(this).html(selectTxt);
		var temp=$(this).parent();
			
		if(temp.attr('id')=="footer"){
				
			$(this).find("select").change(function(){
				window.open($(this).find("option")[this.selectedIndex].value);
			});
		}else{
			//alert("---");
			$(this).find("select").change(function(){
				//alert("--- "+$(this).parent().parent().parent().find("a.btnOK").attr("href",$(this).find("option")[this.selectedIndex].value));
				if(this.selectedIndex==0){
					$(this).parent().parent().parent().find("a.btnOK").attr("href","");
				}else{
					$(this).parent().parent().parent().find("a.btnOK").attr("href",$(this).find("option")[this.selectedIndex].value);
				}
			});
			
			
		}
		
	});
}

/* Carrousel */
function listCarousel(){
	if($('.carouselList').length){
		if($('.productCarousel').length){
			var initNumber = 1;
			$('.carouselList li').each(function(i){
				if($(this).hasClass("active")){
					initNumber = i+1;
				}
			})
			$('.carouselList').jcarousel({
				scroll: 3,
				start: initNumber
			});
		}
		else {
			$('.carouselList').jcarousel({
				scroll: 3			  
			});
		}
	}
}

/* Affichage des boutons d'impressions */
function displayFunctions(){
	if($(".printFunctionBlock").length){
		$(".printFunctionBlock .sendToFriend").after("<li class=\"print\"><a href=\"#\"><img src=\"/App_Themes/Common/img/link_printRecipe.gif\" alt=\"Imprimer la recette\" /></a></li>");
	}
	
	if($("#coachingContainer").length){
		$("#coachingContainer").prepend("<p class=\"linkPrintContainer print\"><a href=\"#\"><img src=\"/App_Themes/Common/img/coaching/profil/link_printFile.gif\" alt=\"Imprimer le dossier\" /></a></p>");
		$("#coachingContainer").prepend("<p class=\"linkPrintContainer linkConseiller\"><a href=\"/coaching-culinaire/envoi-a-un-ami.aspx\"><img src=\"/App_Themes/Common/img/coaching/profil/link_conseiller.gif\" alt=\"Conseiller ce coaching à un ami \" /></a></p>");
	}
	
	if($(".printFunctionPdtBlock").length){
		$(".printFunctionPdtBlock .sendToFriend").after("<li class=\"print\"><a href=\"#\"><img src=\"/App_Themes/Common/img/link_printProduct.gif\" alt=\"Imprimer la fiche produit\" /></a></li>");
	}
	
	$(".print").click(function(){
		window.print();							   
   });
}

/* Navigation par onglets */
function tabNav(){
	initNavTab();


	$(".stdNavTab li").removeClass("active");
	$(".stdContentTab .layerTab").hide();
	
	$(".stdNavTab li:first-child").addClass("active");
	$(".stdContentTab .layerTab:first-child").show();
	
	$(".stdNavTab li a").click(function(){
		
		$(".stdNavTab li").removeClass("active");
		$(".stdContentTab .layerTab").hide();
		
		var linkTab = $(this).attr("href").split("#");
		$("#"+linkTab[1]).show();
		
		$(this).parent("li").addClass("active");
		
		return false;
	});
	
	initWidthOfNav() // juste pour la page => videoMaggi
	
}


function initNavTab(){

	$('.stdContentTab').before("<ul class='stdNavTab'></ul>");
	
	$('.stdContentTab .tabTitle').each(function(i){
		var idBlock = "#"+$(this).parent().parent().attr("id");
		var idBlockitems = $(this).parent().parent().attr("id");
		
		$('.stdNavTab').append("<li><a href='"+idBlock+"'><span class='"+idBlockitems+"'>"+$(this).html()+"</span></a></li>"); 
		$(this).addClass("displayTitle");
	});	
	
}

function intiCoaching(){
	var contentCoaching = $(".listQuestionsCoaching");
	if (contentCoaching.length){
		$(".listQuestionsCoaching .errorMsg").hide();
		$(".listQuestionsCoaching .questionBlock").hide();
		$(".listQuestionsCoaching #question1").show();
		formCoaching();
	}
}

function formCoaching(){
	$(".listQuestionsCoaching .previousLink a").click(function(){
		var linkPrevious = $(this).attr("href");
		$(".listQuestionsCoaching .questionBlock").hide();
		$(linkPrevious).show();
		return false;
	});
	$(".listQuestionsCoaching .nextLink a").click(function(){
		var inputQuestion = $(this).parent().parent().parent().find("input[@type=radio]");
		if(inputQuestion.is("[@checked]")){
			var linkNext = $(this).attr("href");
			$(".listQuestionsCoaching .questionBlock").hide();
			$(".listQuestionsCoaching .errorMsg").hide();
			$(linkNext).show();
		}
		else{
			$(this).parent().parent().siblings(".errorMsg").show();		
		}
		return false;
	});
	$(".listQuestionsCoaching .validLink input").click(function(){
		var inputQuestion = $(this).parent().parent().parent().find("input[@type=radio]");
		if(inputQuestion.is("[@checked]")){
			$(".listQuestionsCoaching .errorMsg").hide();	
		}
		else{
			$(this).parent().parent().siblings(".errorMsg").show();
			return false;
		}
	});
}

function initShowLayer(){
	$('.showInfos').each(function(){
		var blockDisplay = $(this).attr("href");
		hideLayer(blockDisplay);
		
		$(this).click(function(){
			if($(blockDisplay).css("display") == "none" ){
				$(blockDisplay).show();
				if($(this).parent().hasClass("displayLink")){
					$(this).addClass("active");
				}
			}
			else{
				$(blockDisplay).hide();
				if($(this).parent().hasClass("displayLink")){
					$(this).removeClass("active");
				}
			}
			return false;
			
		});
	});
}

function hideLayer(blockHide){
	$(blockHide).addClass("hideBlock");
}

// INFOBULLE
//* Permet d'ajouter ou de supprimer un vnement par objet dans la page */
//~ http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html

function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}
/* */

var idAnchor    = 'wfa';
var idClose     = 'wfclose';
var idInfobulle = 'wfinfobulle';

function inverseDisplayPresente() {
  if(document.getElementById(idInfobulle).style.display != 'block' ) {
    document.getElementById(idInfobulle).style.display = 'block';
  }
  else {
    document.getElementById(idInfobulle).style.display = 'none';
  }
}

function launch() {
  if(document.getElementById(idInfobulle)) {
      document.getElementById(idInfobulle).style.display = 'none';
  }
  if(document.getElementById(idAnchor)) {
      document.getElementById(idAnchor).setAttribute('href','javascript:inverseDisplayPresente();');
  }
  if(document.getElementById(idClose)) {
      document.getElementById(idClose).setAttribute('href','javascript:inverseDisplayPresente();');
  }
}

addEvent(window,'load',launch);

function setPopinEvent(){
		$('.popin').each(function(){ 
			var wi = $(this).width();
			var hi = $(this).height();
			
			var hiWindow = $(document)[0].documentElement.clientHeight;
			var wiWindow = $(document).width();

			var top = (hiWindow/2)-(hi/2)+getScrollTop();
			if(top<0) top = 10;
			var left = (wiWindow/2)-(wi/2);
			$(this).attr('style','top:'+top+'px;left:'+left+'px;');
		});
		
		//btn close
		$('.close').unbind();
		$('.close').bind('click',function(){
			$(this).parent().parent().hide();
			return false;
		});	
}
function getPopin(id){
	setPopinEvent();
	$('.popin').hide();
	$("#"+id).show();
}
function getScrollTop(){
	if( document.body && ( document.body.scrollLeft || document.body.scrollTop ))
		return document.body.scrollTop;
	else
		return document.documentElement.scrollTop;
}

function dossier()
{
	$('#btnDossier').click( function() { 
				if($('#id_Dossier').val()==""){
					return false;
				}else{		
						window.location.href=$('#id_Dossier').val();
						return false;
					}
								     
			
	 } );
	 
}
function closePopup(){	
	$("#closePopin").click( function() { 								     
		window.close();	
	 } );
}
function openPopup(){	
	/* $(".OpenPopin").click( function() { 								     
		window.open('/nos-produits-vos-reductions/popUpOctobre.html', '', 'height=520, width=410, top=100, left=100, toolbar=no, menubar=no, location=no, resizable=yes, scrollbars=no, status=no'); 		return false;
	 } ); */
	 return false;
}
function initWidthOfNav(){
	var wi = $('.stdNavTabVideo').width();
	var nbrTab = $('.stdNavTabVideo li').length;
	var aSosutrire = 40 * nbrTab;
	wi -= aSosutrire;
	var myWi = Math.floor(wi/nbrTab);
	$('.stdNavTabVideo li span').width(myWi);
	
}
function showItem(){
	$(".productCarousel .jcarousel-item h1 a").unbind("click");
	$(".productCarousel .jcarousel-item h1 a").bind("click",function(){
		
		var index=$(".productCarousel .jcarousel-item").index($(this).parents('li'));
			
			$(".productCarousel .jcarousel-item").removeClass("active");
			$(this).parents('li').addClass("active");
			$(".contentPartner .items").hide();
			$(".contentPartner #item"+index).show();
			
			
			var h1 = $(this).parents('h1');
			var content1 = $(this).html();
			
			$('.productCarousel .jcarousel-item h1 > img').each(function(){
						var content = $(this).parents('h1').html();
						$(this).parents('h1').html("<a href='#' class='pdtBlock'>"+content+"</a>")
			})
					
		$(this).replaceWith(content1+'');
		showItem();
			return false;
			
	});
	
	
}