/*
if (document.cookie){
}else{
	document.cookie = 'essai=cookie';
	if (document.cookie) {
	}else{
		document.location.href='erreur_cookies.php';
	}
}
*/
var baseUrl = "http://www.toutelanutrition.com";
	
$(document).ready(function(){
	
	cleanInputText("#chps_rech", "Recherche");
	cleanInputText("#chpsemail", "Entrez votre adresse email");
	cleanInputText("#chpsrecuppwrd", "Entrez votre identifiant (par exemple votre email)");
	
	initMenuMarque();
	recopieform();
	initBtPrix();
	initChangeImg();
	initSplashSwf();
	initBtPan();
	initAutoSuggest("#chps_rech");
	initBanniere();
	initAutoSubmitPaiesec();
	
	if($("#decompte").size() > 0){
		setInterval(function(){decompte("#decompte");}, 1000);
	}
	if($(".decompte_vf span").size() > 0){
		setInterval(function(){decompte(".decompte_vf span");}, 1000);
	}
	
	$("#list_cmd").stripe("#fff", "#f7f7f7");
	$("#list_bc").stripe("#fff", "#f7f7f7");
	$("#list_cmd_old").stripe("#fff", "#f7f7f7");
	$("#panlist").stripe("#fff", "#f7f7f7");
	$("#cmdlist").stripe("#fff", "#f7f7f7");
	
	$(".feedbackinfo").animate({backgroundColor:'#ffbb2a'}, 1000).animate({backgroundColor:'#ff0'}, 1000).animate({backgroundColor:'#ffbb2a'}, 1000);
	
});

function initAutoSubmitPaiesec(){
	
	$('html').animate({padding:0}, 5000, function() {
		$("#paiesec").submit();
	});
}

function initBanniere(){
		
	startImg = Math.round(Math.random() * 7);
	
	jQuery('#mycarousel').jcarousel({
		vertical: true,
		scroll: 1,
		wrap: 'circular',
		start:startImg,
		auto:5,
        buttonNextHTML: null,
        buttonPrevHTML: null
	});
}

function initBtPan(){
	$("#btrecalc").click(function(){
		$("#formaffichpan").attr("action", baseUrl+"/front_panier/recalcule_panier");
		$("#formaffichpan").submit();
		return false;
	});
	$("#btsuite").click(function(){
		$("#formaffichpan").attr("action", baseUrl+"/front_panier/identification");
		$("#formaffichpan").submit();		
		return false;		
	});
}


function decompte(elt){	
	$.each($(elt), function(){	
		var now_date = new Date();	
		var now = now_date.valueOf();
		var fin = parseInt($(this).prev().val(), 10);
		var ms = parseInt(fin, 10)-now;
		
		var totalsecondes = ms;
		var reste = ms/1000;

		var jours = Math.floor(reste/86400);
		reste = Math.round(reste%86400);
		var heures = Math.floor(reste/3600);
		reste = Math.round(reste%3600);
		var minutes =  Math.floor(reste/60);
		reste = Math.round(reste%60);
		var secondes = reste;

		if(jours < 10){
			jours = "0"+jours;
		}
		if(heures < 10){
			heures = "0"+heures;
		}
		if(minutes < 10){
			minutes = "0"+minutes;
		}
		if(secondes < 10){
			secondes = "0"+secondes;
		}

		var compteur = "";
		if(totalsecondes > 0){
			if(totalsecondes/1000 > 86400){
				compteur = jours+"j ";
			}
			if(totalsecondes/1000 > 3600){
				compteur += heures+"h ";
			}
			if(totalsecondes/1000 > 60){
				compteur += minutes+"m ";
			}
			compteur += secondes+"s ";
		}else{
			compteur = "Termin&eacute; ! ";
		}
		$(this).empty().append(compteur);
	});
}


function cleanInputText(id, msg){
	$(id).click(function(){
		if($(this).val() == msg){
			$(this).val("");
		}
	});
}


function initMenuMarque(){
	$("#chpsprod").change(function(){
		document.location.href=baseUrl+"/front_navigation/liste/marque/"+$(this).val();
	});
}

function recopieform(){
	$("#recopie_form").click(function(){
		if($(this).attr("checked")){
			$("#civ_l").val($("#civ_f").val());
			$("#nom_l").val($("#nom_f").val());
			$("#prenom_l").val($("#prenom_f").val());
			$("#adr_l").val($("#adr_f").val());
			$("#code_l").val($("#code_f").val());
			$("#ville_l").val($("#ville_f").val());
			$("#mail_l").val($("#mail_f").val());
			$("#pays_l").val($("#pays_f").val());
			$("#tel_l").val($("#tel_f").val());
		}else{
			$("#civ_l").val("");
			$("#nom_l").val("");
			$("#prenom_l").val("");
			$("#adr_l").val("");
			$("#code_l").val("");
			$("#ville_l").val("");
			$("#mail_l").val("");
			$("#pays_l").val("");
			$("#tel_l").val("");
		}
	});
}

function initBtPrix(){
	$.each($(".modif_prix"), function(){
		$(this).find(".btplus").click(function(){
			var qttplus = parseInt($(this).next().next().val())+1;
			if(qttplus < 100){
				$(this).next().next().val(qttplus);
			}
			return false;
		});
		$(this).find(".btmoins").click(function(){
			var qttmoins = parseInt($(this).next().val())-1;
			if(qttmoins > 0){
				$(this).next().val(qttmoins);
			}
			return false;
		});
	});
}


function initChangeImg(){
	$(".side_img ul li a").click(function(){
		$(this).parent().parent().find("a").removeClass("current");
		$(this).addClass("current");
		var recupImgSrc = $(this).attr("href");
		
		var monImg = $(this).parent().parent().parent().find("img")[0];
		$(monImg).fadeOut(200, function(){
			$(this).attr("src", recupImgSrc);
		})
		
		monImg.onload=function(){
			if(this.complete){
				$(monImg).fadeIn(200);
			}
		}
		return false;
	});
}

function initSplashSwf(){
	$.each($(".splashpromo"), function(){
		var swf = "<embed type='application/x-shockwave-flash' src='"+baseUrl+"/swf/splashpromo.swf' bgcolor='#fff' quality='high' wmode='transparent' height='86' width='86'>";
		$(this).append(swf);
	});
	$.each($(".splashcoup"), function(){
		var swf = "<embed type='application/x-shockwave-flash' src='"+baseUrl+"/swf/splashcoup.swf' bgcolor='#fff' quality='high' wmode='transparent' height='86' width='86'>";
		$(this).append(swf);
	});
	$.each($(".splashnouveau"), function(){
		var swf = "<embed type='application/x-shockwave-flash' src='"+baseUrl+"/swf/splashnouveau.swf' bgcolor='#fff' quality='high' wmode='transparent' height='86' width='86'>";
		$(this).append(swf);
	});
	$.each($(".splashvente"), function(){
		var swf = "<embed type='application/x-shockwave-flash' src='"+baseUrl+"/swf/splashvente.swf' bgcolor='#fff' quality='high' wmode='transparent' height='86' width='86'>";
		$(this).append(swf);
	});
}


function initAutoSuggest(id){
	
	previousKeyword = $(id).val();
	$("#searchpane .suggest").prepend("<ul style='display:none;'><li></li></ul>");
	minCar = 2;
	
	$(id).keyup(function(){
		
		var keyword = $(this).val();
		var keywordSize = keyword.length;
		
		if(keyword == "" || keyword == null){
			$("#searchpane .suggest ul").hide();
		}else if(keyword != previousKeyword && keywordSize > minCar){
			previousKeyword = keyword;
			var dataToSend = "";
			dataToSend += "&keyword="+keyword;
			$.ajax({
				url: baseUrl+'/front_navigation/recherche_ajax',
				type: 'POST',
				data: dataToSend,
				dataType: 'xml',
				error: function(){},
				beforeSend: function(){},
				success: function(xml){
					var quantite = $(xml).find('suggestions').find('ligne').size();
					if(quantite > 0){
						var ligne = "";
					    $(xml).find('suggestions').find('ligne').each(function(){
							var url = $(this).find("url").text();
							var name = $(this).find("name").text();
							ligne += "<li><a href='"+url+"'>"+name+"</a></li>";
					    });
						$("#searchpane .suggest ul").empty().append(ligne).show();
					}
					//currentActiveSuggest = -1;
				}
			});
		}
	});
	
	$(id).blur(function(){
		$("body").animate({margin:0}, 500, function(){
			$("#searchpane .suggest ul").hide();
		});
	});
	/*
	currentActiveSuggest = -1;
	$(id).keyup(function(event) {
		
		var nbrSuggest = $("#searchpane .suggest ul li").size();
		
	  if(event.keyCode == '38') {
	  	if(parseInt(currentActiveSuggest-1) >= 0){currentActiveSuggest -= 1;}
		$("#searchpane .suggest ul li a").removeClass("active");
		$("#searchpane .suggest ul li a:eq("+currentActiveSuggest+")").addClass("active");
	  }
		
	  if(event.keyCode == '40') {
	  	if(currentActiveSuggest+1 < nbrSuggest){currentActiveSuggest += 1;}
		$("#searchpane .suggest ul li a").removeClass("active");
		$("#searchpane .suggest ul li a:eq("+currentActiveSuggest+")").addClass("active");
	  }
	  
	  if(event.keyCode == '13') {
		var url = $("#searchpane .suggest ul li a.active").attr("href");
		document.location.href = url;
	  }
	  
   });
	*/
}











































