$(document).ready(function(){
	if(($.browser.msie)&&($.browser.version < 7)) {
		$(document).pngFix();
		/*$("#entete h1").pngFix();
		$("#view_accueil #page").pngFix();
		$("#etoile-video").pngFix();
		$("#aimerez_aussi").pngFix();
		$("#fromage_grains").pngFix();
		$(".fromage a").pngFix();
		$("#flash_video").pngFix();
		$(".slideshow").pngFix();
		$(".intro_tout table a").pngFix();*/
	}
	//Cufon.replace("h1", {autoDetect: true, fontFamily: 'RonniaExtraBold', hover: true});
	/*Cufon.replace(".liste_resultats h1, h2, h3, h4, .numero a", {fontFamily: 'RonniaSemiBold', hover: true});
	Cufon.replace(".item h2", {fontFamily: 'RonniaExtraBold', hover: true});*/
	Cufon.replace("h1, h2, h3, h4, .numero a", {autoDetect: true, hover: true});
	activerNavFromages();
	activerSlideshow();
	activerSwitchAlcool();
	activerSlider();
	activerTopLayer();
	activerEtatNav();
	ouvrirAbonnement();
});

/*--------------------------------------------------------------
	Ouvre le toplayer d'abonnement si #abonnement dans l'URL
--------------------------------------------------------------*/
function ouvrirAbonnement(){
	if(String(window.location).indexOf("#abonnement") > -1) {
		$("a.pop-abonnement").eq(0).click();
	}
}

/*--------------------------------------------------------------
	Activer le bon element dans la nav de droite
--------------------------------------------------------------*/
function activerEtatNav() {
	var adresse = String(window.location).replace("http://", "");
	adresse = adresse.replace("172.16.20.117:8080", "");
	adresse = adresse.replace("172.16.20.118:8080", "");
	adresse = adresse.replace("fromagesdici.com", "");
	adresse = adresse.replace("www.fromagesdici.com", "");
	adresse = adresse.replace("ourcheeses.com", "");
	adresse = adresse.replace("www.ourcheeses.com", "");
	$("#nav_tout a, #nav_recettes a").each(function(){
		var href = $(this).attr("href");
		if(adresse == href) {
			$(this).parent("li").addClass("actif");
		}
	});
}

/*--------------------------------------------------------------
	TOP LAYER
--------------------------------------------------------------*/
function activerTopLayer() {
	$("a.pop-ancre, a.pop-abonnement").click(function(){
		afficherOverlay();
		initTopLayer($(this));
		$(this).blur();
		return false;
	});
}

function initTopLayer(__el) {
	$("body").append("<div id=\"toplayer\"><p id=\"bt-fermer-toplayer\"><a href=\"#\"></a></p><div id='toplayer-contenu'></div></div>");
	
	switch (__el.attr("class")) {
		case "pop-ancre":
			$(__el.attr("href")).clone(true).appendTo($("#toplayer-contenu"));
			positionTopLayer();
			Cufon.refresh();
			$("#toplayer").css({display:"block"});
			break;
		
		case "pop-abonnement":
			$("#toplayer-contenu").load(__el.attr("href"), function(){
				$("#toplayer-contenu").addClass("abonnement");
				positionTopLayer();
				validerInfolettre();
				Cufon.refresh();
				$("#toplayer").css({display:"block"});
			});
			break;
			
		default: 
			// rien
			break;
	}
	
	$("#bt-fermer-toplayer").click(function(){
		$("#overlay").click();
		$(this).blur();
		return false;
	});
	
	$(document).keyup(function(event){
		if (event.keyCode == 27) {
			$("#overlay").click();
		}
	});
}

function positionTopLayer() {
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	var largeurToplayer = $("#toplayer").width();
	var hauteurToplayer = $("#toplayer").height();
	//var marginTop = arrayPageScroll[1] + -(hauteurToplayer/2);
	var marginTop = -(hauteurToplayer/2);
	var marginLeft = -(largeurToplayer/2);
	
	$("#toplayer").css("margin-top", marginTop);
	$("#toplayer").css("margin-left", marginLeft);
}

function cacherTopLayer() {
	$(document).unbind("keyup");
	$("#toplayer a").unbind("click");
	$("#toplayer").remove();
}

function afficherOverlay() {
	$("body").append("<div id=\"overlay\"></div>");
	var arrayPageSize = getPageSize();
	var hauteurOverlay = arrayPageSize[1];
	
	$("#overlay").css("height", hauteurOverlay);
	$("#overlay").css("opacity", "0.8");
	$("#overlay").css("display", "block");
	$("#loading").css("display", "block");
	
	$("#overlay").click(function(){
		cacherTopLayer();
		cacherOverlay();
		$(this).blur();
		return false;
	});
	
	if(($.browser.msie)&&($.browser.version < 7)) {
		visibilitySelects("hidden");
	}
}

function cacherOverlay() {
	$("#overlay").remove();
	
	if(($.browser.msie)&&($.browser.version < 7)) {
		visibilitySelects("visible");
	}
}

function visibilitySelects(visibility){
	$("select").each(function(i){
		$(this).css("visibility", visibility);
	});
}


/*--------------------------------------------------------------
	NAVIGATION DROITE
--------------------------------------------------------------*/
function activerNavFromages() {
	$("#nav_fromages>ul>li").each(function(){
		$(this).click(function(){
			if($(this).is(".actif")){
				$(this).removeClass("actif");
			} else {
				$("#nav_fromages li.actif").removeClass("actif");
				$(this).addClass("actif");
			}
		});
	});
	$("body").click(function(event){
		if(!$(event.target).is("#nav_fromages *")){
			$("#nav_fromages li.actif").removeClass("actif");
		}
	});
}

/*--------------------------------------------------------------
	SWITCH ALCOOL
--------------------------------------------------------------*/
function activerSwitchAlcool() {
	$("#verres-alcool a, #liens-alcool a").each(function(){
		$(this).click(function(){
			var href = $(this).attr("href");
			if(!$("#alcools").hasClass("block")){
				if(!$(href).hasClass("actif")){
					$("#alcools").addClass("block");
					$("#verres-alcool .actif").removeClass("actif");
					$("#liens-alcool .actif").removeClass("actif");
					$(href + "-verre").addClass("actif");
					$(href + "-lien").addClass("actif");
					animerSwitchAlcool(href);
				} else {
					$("#verres-alcool .actif").removeClass("actif");
					$("#liens-alcool .actif").removeClass("actif");
					$("#alcools").addClass("block");
					$("#alcools").slideUp("slow", function(){
						$(".alcool.actif").removeClass("actif");
						$("#alcools").removeClass("block");
					});
				}
			}
			$(this).blur();
			return false;
		});
	});
}

function animerSwitchAlcool(__href) {
	if($("#alcools .alcool").hasClass("actif")){
		$("#alcools").slideUp("slow", function(){
			$(".alcool.actif").removeClass("actif");
			$(__href).addClass("actif");
			$("#alcools").slideDown("slow", function(){
				$("#alcools").removeClass("block");
			});
		});
	} else {
		$("#alcools").css("display", "none");
		$(__href).addClass("actif");
		$("#alcools").slideDown("slow", function(){
			$("#alcools").removeClass("block");
		});
	}
}

/*--------------------------------------------------------------
	SLIDER
--------------------------------------------------------------*/
function activerSlider() {
	equalHeight($(".slide"));
	$(".slider").each(function(){
		var __this = this;
		__this.cur = Math.floor(Math.random()*$(".slide", this).length);
		__this.block = false;
		
		$(this).append("<p class=\"bt-precedent\"><a href=\"#\"></a></p>");
		$(this).append("<p class=\"bt-suivant\"><a href=\"#\"></a></p>");
		
		var newHeight = $(".slide:eq(0)", this).height();
		$(this).css({height:newHeight});
		
		$(".slide:eq("+__this.cur+")", this).css({left:0});
		
		$(".bt-suivant a", this).click(function(){
			animerSlide(__this, 1);
			$(this).blur();
			return false;
		});
		$(".bt-precedent a", this).click(function(){
			animerSlide(__this, -1);
			$(this).blur();
			return false;
		});
	});
}

function animerSlide(__el, __dir) {
	if(!__el.block) {
		__el.block = true;
		var current = __el.cur;
		var next = __el.cur + __dir;
		var newLeft = $(__el).width() * __dir;
		
		if(next >= $(".slide", __el).length) next = 0;
		if(next < 0) next = $(".slide", __el).length - 1;
		
		var newHeight = $(".slide:eq(" + next + ")", __el).height();
		
		$(".slide:eq(" + current + ")", __el).css({zIndex: 10});
		$(".slide:eq(" + next + ")", __el).css({left: newLeft, zIndex: 100});
		//$(__el).animate({height:newHeight}, 600);
		$(".slide:eq(" + current + ")", __el).animate({left:-newLeft}, 600);
		$(".slide:eq(" + next + ")", __el).animate({left:0}, 600, function(){
			__el.block = false;
		});
		
		__el.cur = next;
	}
}

function equalHeight(group) {
	var tallest = 0;
	group.each(function() {
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

/*--------------------------------------------------------------
	SLIDESHOW PHOTO
--------------------------------------------------------------*/
function activerSlideshow() {
	$(".slideshow").each(function(){
		var __this = this;
		__this.cur = 0;
		__this.block = false;
		
		$("li:eq(0)", this).addClass("actif");
		$("p img", this).addClass("img-0");
		
		$("li a", this).click(function(){
			animerSlideshow(__this, this);
			$(this).blur();
			//$(".slideshow").pngFix();
			return false;
		});
	});
}

function animerSlideshow(__el, __a) {
	var next = $("li a", __el).index(__a);
	var current = __el.cur;
	if(current != next){
		__el.cur = next;
		$(".actif", __el).removeClass("actif");
		$(__a).parent("li").addClass("actif");
		
		if($.browser.msie) $("p .img-" + current, __el).css("display", "none");
		else  $("p .img-" + current, __el).fadeOut(600);
		
		if($("p img", __el).is(".img-" + next)){
			if($.browser.msie) $("p .img-" + next, __el).css("display", "block");
			else  $("p .img-" + next, __el).fadeIn(600);
		} else {
			$("p", __el).append("<img src=\"" + $(__a).attr("href") + "\" class=\"img-" + next + "\" style=\"display:none;\" />");
			$("p .img-" + next, __el).load(function(){
				if($.browser.msie) $(this).css("display", "block");
				else  $(this).fadeIn(600);
			});
		}
	}
}

function nextSlideshow(__el) {
	var next = __el.cur+1;
	if(next >= $("li a", __el).length) next = 0;
	if(next < 0) next = $("li a", __el).length - 1;
	$("li a:eq(" + next + ")", __el).click();
}

/*-----------------------------------------------------
	RECHERCHE
------------------------------------------------------*/
function bindRechercheRecette(__lang){
	var section = "/recettes/";
	if($('html').attr('lang') == "en"){
		section = "/recipes/";
	}
	
	$("#frm-recherche").parents('form').bind('submit', function(){
		var confirmation = false;
		if($("#frm-recherche").val().length > 2){
			confirmation = true;	
		}
		
		return confirmation;
	});
	
	$("#frm-recherche").autocomplete(data, {
		selectFirst:false, 
		matchContains:true, 
		formatItem:function(__item) {
			return __item.titre_recette;
		}
	}).result(function(__ev, __item) {
		location.href = section + __item.slug_recette;
	});	
}

function bindRechercheFromage(){
	var section = "/repertoire/";
	if($('html').attr('lang') == "en"){
		section = "/directory/";
	}
	
	$("#frm-recherche").parents('form').bind('submit', function(){
		var confirmation = false;
		if($("#frm-recherche").val().length > 2){
			confirmation = true;	
		}
		
		return confirmation;
	});
	
	$("#frm-recherche").autocomplete(data, {
		selectFirst:false, 
		matchContains:true, 
		formatItem:function(__item) {
			return __item.titre_fromage;
		}
	}).result(function(__ev, __item) {
		location.href = section + __item.slug_fromage;
	});	
}
/*-----------------------------------------------------
	VALIDATIONS
-----------------------------------------------------*/
function validerInfolettre(){
	$("#frm_infolettre").submit(function(){
		$("#erreur, #doublons, #merci").hide();
		if(validerFormulaire(this) == 0){
			var confirmation = false;
			var erreur;
			var post = {
				OFC4_Optin_FormDataType: $("input[name='OFC4_Optin_FormDataType']").val(),
				OFC4_Optin_FormData: $("input[name='OFC4_Optin_FormData']").val(),
				OFC4_Optin_FormValidation: $("input[name='OFC4_Optin_FormValidation']").val(),
				OFC4_Optin_OptinField_EMail: $("input[name='OFC4_Optin_OptinField_EMail']").val(),
				OFC4_Optin_OptinField_Language: $("input[name='OFC4_Optin_OptinField_Language']").val()
			}

			$.post('/abonnement/inscription', post, function(data){
				var postBackHtml = $(data);
				if(postBackHtml.find('form').length > 0){
					var destination = postBackHtml.find('#postbackform').attr('action');
					confirmation = false;
					
					if(destination.indexOf('doublons') != "-1"){
						erreur = "doublons";
						$('#doublons').show();
					} else {
						erreur = "erreur";
						$('#erreur').show();
					}
					// $('#erreur').html(erreur);
				} else {
					confirmation = true;
					$('#toplayer-contenu.abonnement').removeClass("abonnement");
					$('#abonnement form').hide();
					$('#abonnement #merci').show();
					
					//$('#merci').html(erreur);
					//$('#bt-fermer-toplayer a').trigger('click');
				}
				
				return confirmation;	
			})
			
			
		}
		return false;
	});
}
function validerCourriel(val){
	var reg =/^[_a-zA-Z0-9.\-]*@[a-zA-Z0-9]([_a-zA-Z0-9\-]+\.)+([a-zA-Z]{2,10})$/;
	if(val.match(reg)) {
		return true;
	} else {
		return false;
	}
}

function validerFormulaire(__frm) {
	var erreur = 0;
	$(".requis", __frm).each(function(){
		if($.trim($(this).val()) == ""){
			$(this).parents("p").addClass("erreur");
			erreur++;
		} else {
			$(this).parents("p").removeClass("erreur");
		}
	});
	
	$(".courriel", __frm).each(function(){
		if(!validerCourriel($.trim($(this).val()))){
			//$(this).parents("p").addClass("erreur");
			$("#erreur_courriel").css("display", "block");
			erreur++;
		} else {
			//$(this).parents("p").removeClass("erreur");
			$("#erreur_courriel").css("display", "none");
		}
	});
	
	return erreur;
}

/**********************************************************************
DIMENSIONS DE L'ÉCRAN
**********************************************************************/
function getPageScroll(){
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}