// JavaScript Document

$(function()
{
	/******** Primetime ********/
	/* Masque les contenus texte */
	var parseContenutexte = 0;
	$("div.home div.contenuTexte").each(function(){
		nbContentPt++;
		if(parseContenutexte == 0) $(this).css('display','block');
		parseContenutexte++
	});
	/* Masque les boutons */
	var parseBouton = 0;
	$("div.home div.bouton").each(function(){
		if(parseBouton != 0) $(this).animate({
			width:0,
			paddingLeft:0
		},0);
		if(parseBouton == 0) $(this).css('display','block');
		parseBouton++
	});
	var animSlide;
	var defilement;
	/* lancement automatique du slideshow */
	$(document).ready(function(){
		if(animation == false){
			defilement = true;
			animation = true;
			animSlide = setInterval(function(){
			defilerPrimetime(posNavPtInit, 'suiv')}, 12000
			);
		}
		return false;
	});
	
	/* Masque les contenus image */
	var parseContenuImage = 0;
	$("div.home div.contenuImage").each(function(){
		if(parseContenuImage != 0) $(this).animate({
			width:0
		},0);
		if(parseContenuImage == 0) $(this).css('display','block');
		parseContenuImage++
	});
	/* Primetime - PrÃ©cÃ©dent */
	$("div.home div.flecheGauche").click(function(){
		if(animation == false || defilement == true){
			clearInterval(animSlide);
			animation = true;
			defilerPrimetime(posNavPtInit, 'prec');
			animSlide = setInterval(function(){
			defilerPrimetime(posNavPtInit, 'suiv')}, 12000
			);			
		}
		return false;
	});
	/* Primetime - Suivant */
	$("div.home div.flecheDroite").click(function(){
		if(animation == false || defilement == true){
			clearInterval(animSlide);
			animation = true;
			defilerPrimetime(posNavPtInit, 'suiv');
			animSlide = setInterval(function(){
			defilerPrimetime(posNavPtInit, 'suiv')}, 12000
			);			
		}
		return false;
	});
	/* Primetime - Gestion navigation */
	$("div.home div.navigation a").click(function(){
		if(animation == false || defilement == true){
			clearInterval(animSlide);
			animation = true;
			var monNumBtn = $(this)[0].id.substring(3, 4);
			defilerPrimetime(monNumBtn, 'nav');
			animSlide = setInterval(function(){
			defilerPrimetime(posNavPtInit, 'suiv')}, 12000
			);				
		}
		return false;
	});
	
	
	/******** Slideshow ********/
	/* Masque les photos */
	var parsePhoto = 0;
	$("div.story div.slider div").each(function(){
		nbImageSs++;
		if(parsePhoto != 0) $(this).fadeOut(0);
		parsePhoto++
	});
	/* Slideshow - Gestion navigation */
	$("div.story div.navigation a").click(function(){
		var nbCarac = $(this)[0].id.length;
		var monNumBtn = $(this)[0].id.substring(3, nbCarac);
		allerPhotoSlideshow(monNumBtn);
		return false;
	});
	
	
	/******** Rollover products ********/
	$("div.products div.produits > div div.rollover").css({
		width:0,
		marginLeft:100
	});
	$("div.products div.produits > div").mouseenter(function(){
		$(this).children().animate({
			width:200,
			marginLeft:0
		},200,'swing');
	});
	$("div.products div.produits > div").mouseleave(function(){
		$(this).children().animate({
			width:0,
			marginLeft:100
		},200,'swing');
	});
	$("div.products div.produits > div").click(function(){
		$(location).attr('href','/'+translated_url+'/nuts/'+$(this)[0].className.split('prod')[1]);
		
	});
	
	
	/******** Where does it come from ? ********/
	$("div.productsDetails #where2").fadeIn(0);
	$("div.productsDetails div.where").mouseenter(function(){
		$("div.productsDetails #where1").fadeIn(200);
		$("div.productsDetails #where2").fadeOut(200);
	});
	$("div.productsDetails div.where").mouseleave(function(){
		$("div.productsDetails #where1").fadeOut(200);
		$("div.productsDetails #where2").fadeIn(200);
	});
	
	
	/******** Available Format ********/
	/* position */
	/* gestion photos */
	var photoAAffichee;
	$("div.productsDetails div.vigPhotos div.vigPhoto").mouseenter(function(){
		photoAAffichee = $("div.productsDetails div.photos div#"+$(this)[0].id.substring(3, 9));
		photoAAffichee.fadeIn(300);
	});
	$("div.productsDetails div.vigPhotos div.vigPhoto").mouseleave(function(){
		$("div.productsDetails div.photos div#"+$(this)[0].id.substring(3, 9)).fadeOut(100);
	});
	$("div.productsDetails div.vigPhotos div.vigPhoto").mousemove(function(e){
		photoAAffichee.css({
			'top':e.pageY-photoAAffichee.height()-20,
			'left':e.pageX-photoAAffichee.width()-20
		});
	}); 
	
	
	/******** Liens menu droite ********/
	$("div.menuRight div.menuRightContent div").click(function(){
		$(location).attr('href',$(this).children()[0].href);
	});
	
	
	/******** Gestion menu FAQ ********/
	$("div.faq div.faqs h2").click(function(){
		if ($(this).next("div.texte:visible").length != 0) {
			$(this).next("div.texte").slideUp(300, function(){$(window).trigger('resize');});
			$("div.faq div.faqs h2").css({
				'background-image':'url(/assets/images/boutons/plus.png)',
				'opacity':1
			});
        }
        else {
			$("div.faq div.faqs div.texte").slideUp(300);
			$("div.faq div.faqs h2").css({
				'background-image':'url(/assets/images/boutons/plus.png)',
				'opacity':0.5
			});
			$(this).next("div.texte").slideDown(300, function(){$(window).trigger('resize');});
			$(this).css({
				'background-image':'url(/assets/images/boutons/moins.png)',
				'opacity':1
			});
        }
	});
	
	
	/******** Gestion des formulaires de contact ********/
	$("div.contact div.form fieldset div#form1").show();
	$("div.contact div.form fieldset select#questions").change(function(){
		var monNumForm = $("div.contact div.form fieldset select#questions option:selected").val().substring(5, 6);
                                    if (parseInt(monNumForm) == 5)
                                        $('.bouton').hide()
                                    else
                                        $('.bouton').show() ;
                                    $('#id_form').val(monNumForm);
		$("div.contact div.form fieldset div.form").hide();
		$("div.contact div.form fieldset div#form"+monNumForm).show();
	});
	
	
	/******** Gestion de la box "Find our products" ********/
	var monCodePostal;
	$("div.header div.findProducts a").click(function(){
		$("div.blackBackgroundFP").fadeIn(300);
		$("div.blackBackgroundFP div.boxFindProducts").css('margin-top', ($(window).height()-$("div.blackBackgroundFP div.boxFindProducts").height())/2-10);
		return false;
	});
	$("div.blackBackgroundFP div.fermer").click(function(){
		$("div.blackBackgroundFP").fadeOut(100);
		return false;
	});
	$("input#postal_code").focus(function(){
		monCodePostal = $(this)[0].value;
		$(this)[0].value = '';
	});
	$("input#postal_code").blur(function(){
		if($(this)[0].value == '') $(this)[0].value = monCodePostal;
	});
	$("input#postal_code2").focus(function(){
		monCodePostal = $(this)[0].value;
		$(this)[0].value = '';
	});
	$("input#postal_code2").blur(function(){
		if($(this)[0].value == '') $(this)[0].value = monCodePostal;
	});
	
	
	/******** Gestion de la box "Nutritional Facts" ********/
	$("div.blackBackgroundNF div.nutritionalFacts table#tab2").hide();
	$("div.productsDetails div.bouton a").click(function(){
		$("div.blackBackgroundNF").fadeIn(300);
		$("div.blackBackgroundNF div.nutritionalFacts").css('margin-top', ($(window).height()-$("div.blackBackgroundNF div.nutritionalFacts").height())/2-20);
		return false;
	});
	$("div.blackBackgroundNF div.fermer").click(function(){
		$("div.blackBackgroundNF").fadeOut(100);
		$("div.blackBackgroundNF div.nutritionalFacts table#tab1").show();
		$("div.blackBackgroundNF div.nutritionalFacts table#tab2").hide();
		$("div.blackBackgroundNF div.nutritionalFacts div.amount").css('background-image','url(/assets/images/boutons/amount_actif_bg.png)');
		$("div.blackBackgroundNF div.nutritionalFacts div.dailyValue").css('background-image','url(/assets/images/boutons/daily_value_inactif_bg.png)');
		return false;
	});
	$("div.blackBackgroundNF div.nutritionalFacts div.amount").click(function(){
		$("div.blackBackgroundNF div.nutritionalFacts table#tab1").show();
		$("div.blackBackgroundNF div.nutritionalFacts table#tab2").hide();
		$("div.blackBackgroundNF div.nutritionalFacts div.amount").css('background-image','url(/assets/images/boutons/amount_actif_bg.png)');
		$("div.blackBackgroundNF div.nutritionalFacts div.dailyValue").css('background-image','url(/assets/images/boutons/daily_value_inactif_bg.png)');
	});
	$("div.blackBackgroundNF div.nutritionalFacts div.dailyValue").click(function(){
		$("div.blackBackgroundNF div.nutritionalFacts table#tab2").show();
		$("div.blackBackgroundNF div.nutritionalFacts table#tab1").hide();
		$("div.blackBackgroundNF div.nutritionalFacts div.amount").css('background-image','url(/assets/images/boutons/amount_inactif_bg.png)');
		$("div.blackBackgroundNF div.nutritionalFacts div.dailyValue").css('background-image','url(/assets/images/boutons/daily_value_actif_bg.png)');
	});
	
	
	/******** Replace les diffÃ©rents Ã©lÃ©ments en fonction de la taille du browser ********/
	$(window).bind('resize', function(){
		if($(window).height() > $('div.header').height()+$('div.middle').height()+$('div.footer').height()) {
			$('div.home')
				.css({
					position:'fixed',
					top:($(window).height()-$('div.middle').height())/2+19,
					left:0
				});
			$('div.footer')
				.css({
					position:'fixed',
					top:$(window).height()-$('div.footer').height(),
					left:0
				});
		}
		else {
			$('div.middle')
				.css({
					position:'static',
					top:'auto',
					left:'auto'
				});
			$('div.footer')
				.css({
					position:'static',
					top:'auto',
					left:'auto'
				});
		}
		$("div.blackBackgroundFP div.boxFindProducts").css('margin-top', ($(window).height()-$("div.blackBackgroundFP div.boxFindProducts").height())/2-10);
		$("div.blackBackgroundNF div.nutritionalFacts").css('margin-top', ($(window).height()-$("div.blackBackgroundNF div.nutritionalFacts").height())/2-20);
	});
	
	
	/******** Lance le prÃ©chargement des images ********/
	preloadImages(
		'/assets/images/boutons/amount_inactif_bg.png',
		'/assets/images/boutons/daily_value_actif_bg.png',
		'/assets/images/boutons/moins.png'
	);
});


$(document).ready(function(){
	/******** DÃ©clenche le replacement des diffÃ©rents Ã©lÃ©ments en fonction de la taille du browser ********/
	$(window).trigger('resize');
});



var posNavPtInit = 1;
var nbContentPt = 0;
var animation = false;

/******** Fait dÃ©filer le primetime ********/
function defilerPrimetime(posNav, defilement){
	var contenuTexteACacher = "ct"+posNavPtInit;
	var boutonACacher = "btn"+posNavPtInit;
	var contenuImageACacher = "ci"+posNavPtInit;
	var navigationADesactiver = "nav"+posNavPtInit;
	
	if(defilement == 'suiv') {
		if(posNav == nbContentPt) posNav=1;
		else posNav++;
	}
	if(defilement == 'prec') {
		if(posNav == 1) posNav=nbContentPt;
		else posNav--;
	}
	
	posNavPtInit=posNav;
	
	var contenuTexteAAfficher = "ct"+posNav;
	var boutonAAfficher = "btn"+posNav;
	var contenuImageAAfficher = "ci"+posNav;
	var navigationAActiver = "nav"+posNav;
	
	$("div.home div.navigation #"+navigationADesactiver)[0].children[0].src = '/assets/images/boutons/nav_inactif.png';
	$("div.home div.navigation #"+navigationAActiver)[0].children[0].src = '/assets/images/boutons/nav_actif.png';
	
	$("div.home #"+contenuTexteACacher).fadeOut(300);
	$("div.home #"+boutonACacher).animate({
		width:0,
		paddingLeft:0
	},300,'swing');
	$("div.home #"+contenuImageACacher).animate({
		width:0
	},300,'swing', function(){
		$("div.home #"+boutonACacher).css('display','none');
		$("div.home #"+contenuImageACacher).css('display','none');
		
		$("div.home #"+boutonAAfficher).css('display','');
		$("div.home #"+contenuImageAAfficher).css('display','');
		
		$("div.home #"+contenuTexteAAfficher).fadeIn(300);
		$("div.home #"+boutonAAfficher).animate({
			width:225,
			paddingLeft:34
		},300,'swing');
		$("div.home #"+contenuImageAAfficher).animate({
			width : 578
		},300,'swing', function(){
			animation = false;
		});
	});
}



var posNavSsInit = 1;
var nbImageSs = 0;

/******** Fait dÃ©filer le slideshow ********/
function defilerSlideshow() {
	var imageACacher = "img"+posNavSsInit;
	var navADesactiver = "nav"+posNavSsInit;
	
	if(posNavSsInit != nbImageSs) {
		posNavSsInit++;
	}
	else posNavSsInit = 1;
	
	var imageAAfficher = "img"+posNavSsInit;
	var navAActiver = "nav"+posNavSsInit;
	
	$("div.story div.navigation #"+navADesactiver)[0].children[0].src = '/assets/images/boutons/nav_inactif.png';
	$("div.story div.navigation #"+navAActiver)[0].children[0].src = '/assets/images/boutons/nav_actif.png';
	
	$("div.story div.slider #"+imageACacher).fadeOut(300);
	$("div.story div.slider #"+imageAAfficher).fadeIn(500);
}


function allerPhotoSlideshow(posImg) {
	clearInterval(monTimer);
	
	var imageACacher = "img"+posNavSsInit;
	var navADesactiver = "nav"+posNavSsInit;
	
	posNavSsInit = posImg;
	
	var imageAAfficher = "img"+posNavSsInit;
	var navAActiver = "nav"+posNavSsInit;
	
	$("div.story div.navigation #"+navADesactiver)[0].children[0].src = '/assets/images/boutons/nav_inactif.png';
	$("div.story div.navigation #"+navAActiver)[0].children[0].src = '/assets/images/boutons/nav_actif.png';
	
	$("div.story div.slider #"+imageACacher).fadeOut(300);
	$("div.story div.slider #"+imageAAfficher).fadeIn(500);
	
	monTimer = setInterval(defilerSlideshow, 2000);
}


/******** PrÃ©charge les images ********/
function preloadImages(){
	$.each(arguments, function(){
		$('<img />').attr('src',this);
	});
}

