// JavaScript Document

$('document').ready(function(){
	selectList();
	listCarousel();
	displayFunctions();
	tabNav();
	intiCoaching();
	initShowLayer();
});

/* Flash video */
function flashRecipe(urlImg, urlFlv ){

	var flashRecipeBlock = $("#flashRecipe");
	if (flashRecipeBlock.length){
		var flashvars = {
			confFile: "xml/conf.xml",
			siteMainFile: "swf/player.swf",
			preview: urlImg,
			media: urlFlv
		};
		
		var params = {
		  allowScriptAccess: "always"
		};
		
		var attributes = {};
		
		swfobject.embedSWF("swf/loader.swf", "flashRecipe", "367", "290", "8.0.0", "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);
		$(this).find("select").change(function(){
			window.open($(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: 1,
				start: initNumber
			});
		}
		else {
			$('.carouselList').jcarousel({
				scroll: 1			  
			});
		}
	}
}

/* Affichage des boutons d'impressions */
function displayFunctions(){
	if($(".printFunctionBlock").length){
		$(".printFunctionBlock .sendToFriend").after("<li class=\"print\"><a href=\"#\"><img src=\"img/common/link_printRecipe.gif\" alt=\"Imprimer la recette\" /></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;
	});
	
}


function initNavTab(){
	$('.stdContentTab').before("<ul class='stdNavTab'></ul>");
	$('.stdContentTab .tabTitle').each(function(i){
		var idBlock = "#"+$(this).parent().parent().attr("id");
		$('.stdNavTab').append("<li><a href='"+idBlock+"'><span>"+$(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);




