var delai = 5000;
var nb_unes = 0;
var une_actuelle = 1;

$(document).ready(function(){
	$('a[@rel*=lightbox]').facebox();
	$("#menu ul li").each(function(){
	   $(this).mouseover(function(){
			$(this).addClass("menuliah");
			$(this).children("div").slideDown("normal");
			$(this).prev().children("div").fadeOut("fast");
			$(this).siblings().children("div").fadeOut("fast");
			$(this).prev().removeClass("menuliah");
	   });
	});
	$(document).click(function(){
	  $("#menu ul li div").fadeOut("fast");
	  $("#menu ul li").removeClass("menuliah");
	});
	
	if($(".contenu_page")) {
		var height_md = $(".content_page").height();
		var height_contenu = $(".contenu_page").height();
		
		if(height_md > height_contenu) {
			$(".contenu_page").height(height_md);
			$(".contenu_page2").height(height_md);
		}
	}
	
	$(".recette_titre span").dropShadow({left: 1, top: 1, opacity: 0.8, blur: 1});
	
	if($("#slider_recettes")) {
		$(".recettes_slide:eq(0)").clone().appendTo("#recettes_slider");
		$(".recettes_slide").each(function(){
			nb_unes = nb_unes + 1;
		});
		$("#recettes_slider").css({
			width: (334 * nb_unes)
		});
		setTimeout("slide_recette('"+une_actuelle+"')", delai);
	}
	$('.recette_description').corner("10px");
	$('.details_diet').corner("10px");
	$('.chapeau_news').corner("10px");
	$('.contenu_news').corner("10px");
});

function newsletter() {
	
	var email = document.inscr_nl.mail.value;
	
	if(email == "" || email == "Votre adresse Email") {
		return false;	
	}
	
	var place = email.indexOf("@",1);
	var point = email.indexOf(".",place+1);
	if ((place > -1)&&(email.length >2)&&(point > 1)) {
	}
	else {
		alert('L\'adresse email saisie n\'est pas valide.');
		return(false);
	}
	
	if(email.length<5) {
		alert ('L\'adresse email saisie n\'est pas valide.');
	}
	
	aleatoire = Math.random();

	var xhr_object = null;
	
	if(window.XMLHttpRequest) // Firefox
	   xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // Internet Explorer
	   xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else { // XMLHttpRequest non supporté par le navigateur
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
	   return;
	}
	
	var method   = "GET";
	var filename = "./include/ajax/fonction.php?action=inscr_nl&a="+aleatoire+"&email="+email;
	
	xhr_object.open(method, filename, true);
	
	xhr_object.onreadystatechange = function() {
	
		if(xhr_object.readyState == 1) {
		}
		
		if(xhr_object.readyState == 4) {
			var tmp = xhr_object.responseText;
			if(tmp == "ok") {
				alert("Votre inscription a bien été enregistrée.");	
			}
			else {
				alert("Votre adresse email est déjà enregistrée à notre newsletter.");	
			}
		}
	}	
	
	xhr_object.send(null);
	
}

function clear_champ(quoi,comment) {
	if(quoi == "mail") {
		var valeur = "Votre adresse Email";
	}
	if(quoi == "mc") {
		var valeur = "Recherche";
	}
	
	if(comment == "0") {
		if(document.getElementById(quoi).value == valeur) { 
			document.getElementById(quoi).value = "";
			document.getElementById(quoi).style.color = "#000000";
		}
	}
	if(comment == "1") {
		if(document.getElementById(quoi).value == "") { 
			document.getElementById(quoi).value = valeur;
			document.getElementById(quoi).style.color = "#C0C0C0";
		}
	}
}

function video(comment) {
	if(comment == "ouvre") {	
		var aleatoire = Math.random();
        var so = new SWFObject('interface/1/flash/player.swf?a='+aleatoire, 'flvplayer_pointcarre', '100%', '195', '8.0.22', '#000000');
            so.addParam('allowFullScreen', 'true');
            so.addVariable('url_flv', '../video/spot.flv');
			so.addVariable('film', './UserFiles/Media/film.mpeg');
			so.addVariable('auto_play', 'on');
            so.write('flash_video');
	}
	if(comment == "cache") {
		document.getElementById("flash_video").innerHTML = "<img alt=\"Visualiser la vidéo\" border=\"0\" src=\"interface/1/img_graph/pages/accueil/video.jpg\" onClick=\"video('ouvre');\" style=\"cursor: pointer;\" />";
	}
}

function change_recherche_accueil(quoi) {
	if(quoi == "1") {
		document.getElementById("form_recherche").className = "form_recherche1";
		document.getElementById("recherche_comment_carte").style.display = "none";
		document.getElementById("recherche_comment_form").style.display = "block";
	}
	if(quoi == "0") {
		document.getElementById("form_recherche").className = "form_recherche";
		document.getElementById("recherche_comment_form").style.display = "none";
		document.getElementById("recherche_comment_carte").style.display = "block";
	}
}

function recette_lance_slide(quoi) {
	document.getElementById("recette_slide_1").style.display = "none";
	document.getElementById("recette_slide_2").style.display = "none";
	document.getElementById("recette_slide_3").style.display = "none";
	document.getElementById("recette_slide_4").style.display = "none";
	
	document.getElementById("recette_slide_"+quoi).style.display = "block";
	alert(quoi);
}

function annuaire_plan(id) {
	aleatoire = Math.random();
	jQuery.facebox("<div id='loader_gmap'><img src='./interface/1/img_graph/loader.gif' border=0 align='absmiddle' />  Chargement en cours...</div><iframe src='./include/ajax/gmap.php?id="+id+"&a="+aleatoire+"' style='width: 490px; height: 350px; border: 0;' frameborder=0></iframe>")
/*
	$(".annuaire_plan").each(function(){
	   $(this).slideUp("normal");
	});
	$("#plan_"+id).slideDown("normal");

	document.getElementById("plan_"+id).innerHTML = "<iframe src='./include/ajax/gmap.php?id="+id+"' style='width: 490px; height: 350px; border: 0;'></iframe>";
*/
}

function slide_recette(quoi) {
	margin = 0 - ($("#recette_slide"+quoi).width() * une_actuelle);
	if(une_actuelle == 0) {
		margin = 0;
	}
	if(une_actuelle == nb_unes-1) {
		img_actuelle = 0;
	}
	else {
		img_actuelle = une_actuelle;
	}
	$("#recette_pointe").attr("src","./interface/1/img_graph/recettes/slide/"+img_actuelle+".gif");
	$("#recettes_slider").animate({
		marginLeft: margin
	},1000,function() {
		une_actuelle = une_actuelle + 1;
		if(une_actuelle == nb_unes) {
			une_actuelle = 1;
			$("#recettes_slider").css({marginLeft: 0});
		}
		setTimeout("slide_recette('"+quoi+"')", delai);
	});
}

function change_pub(quoi) {
	$(".video_porc").css({"display": "none"});
	$("#video_porc_"+quoi).css({"display": "block"});
	$(".video_porc_nav A").attr("class", "");
	$("#pub_"+quoi).attr("class", "video_porc_nav_a");
}

function choix_morceau(quoi) {
	$("#result_morceaux").html("<div class='recette_description' style='text-align: center; margin-top: 30px; font-weight: bold;'><img src='./interface/1/img_graph/loader.gif' border='0' /><br />Chargement en cours...</div>");
	$('.recette_description').corner("10px");
	$.get("./include/ajax/fonction.php?action=morceau&a="+Math.random()+"&mc="+quoi, function(data) {
		$("#result_morceaux").html(data);
		$('.recette_description').corner("10px");
	});
}