$(document).ready(function () {
   initPopin();
});

var initPopin = (function () {		
	//Creer les divs 
	var divs = '<div id="mask"></div><div id="popin"></div>';
	
	var oPage = $("#page"); // BICM TEST.
	if (!oPage) return;
	
	$("#page").after(divs);
		
	// cacher les div popin	
	$("#popin").hide();
	$("#mask").hide();

	if(ISPROFILLOG)
	{
		$("#mask").click(function(){
			hidePopin();
		});
	}

	
	initShowPopin();	
});

var initShowPopin = (function () {
 $("a.loadPopin").bind("click", function(){
	 showPopin();
	 //afficher le popin
     $("#popin").load($(this).attr("href"), "", function(){initClosePopin(); initSubmitFormInPopin();});
	 $("#popin").show();
	 $("#popin").css("z-index","10000000");
	 return false;
 });
 
 if($("#cocktailForm").is("form")) {
 	$("input.submitCocktailForm").bind("click", function(){
		showPopin();
		//afficher le popin
		var myForm = $("form#cocktailForm");
		var result = "";
   	$("#cocktailForm select").each(function() {
      	result += $(this).attr("name")+"="+$(this).val()+"&";
   	})
		$.ajax({
      	type:"POST",
      	url: myForm.attr("action"),
      	data: result,
      	success:function(msg) {
      		//affichage le contenu de la page r�sultat
      		$("#popin").empty();
				$("#popin").append(msg);
      		initClosePopin();
      		initShowPopin();
   		}
   	});
		$("#popin").show();
		return false;
 	});
 }
});

var showPopin = (function () {
	 if($.browser.msie){
		// virer les selects pour IE
		$("select").css("visibility","hidden");
		$("div#popin select").css("visibility","visible");
	 }
	 if((navigator.userAgent.toLowerCase().indexOf("mac")!=-1)?true:false){
		// on masque les Flashs sur Mac (wmode dans les choux)
		$("embed").css("visibility","hidden");
	 }
    //masquer les div contenant du flash
    if($(".embed").is("div")){$(".embed").css("visibility","hidden");}
	 //hauteur du masque
	 var pageH = $("#page").height() + 150 + 20;//150=la hauteur du header; 20=padding de l'ecran
	 $("#mask").height(pageH+"px");
	 $("#mask").show();
	 window.location="#header";
});

var hidePopin = (function () {
	
	$("#popin").empty();
	$("#popin").hide();
	$("#mask").hide();
	
	if($.browser.msie){
		$("select").css("visibility","visible");
	}
	if((navigator.userAgent.toLowerCase().indexOf("mac")!=-1)?true:false){
		$("embed").css("visibility","visible");		
	}
   //afficher les div contenant du flash
   if($(".embed").is("div")){$(".embed").css("visibility","visible");}
	return false;
});

/*
* Special hide US extra popin and reopen the identification one
*/
var hidePopinIdentification = (function () {
	
	hidePopin();
	showPopinAccessProfil(URL_POPIN_IDENTIFICATION);
	return false;
});

var initClosePopin = (function () {
	$("a.closePopin").bind("click", function(){
		hidePopin();
		return false;
	});	
	
	$("a.closePopinIdentification").bind("click", function(){
		hidePopinIdentification();
		return false;
	});
	
});

//Initialisation du click sur le bouton valider du formulaire
var initSubmitFormInPopin = (function () {
	if($(".formInPopin").is("form")) {
		$(".formInPopin .btSubmitFormInPopin").bind("click", function(){
			submitFormInPopin();
			return false;
		});
	}
});

//Submission du formulaire
var submitFormInPopin = (function () {   
   var myForm = $(".formInPopin");
   
   var result = "";
   $(".formInPopin input[@type=hidden]").each(function() {
      result += $(this).attr("name")+"="+encodeURIComponent($(this).val())+"&";
   })
   $(".formInPopin input[@type=text]").each(function() {
      result += $(this).attr("name")+"="+encodeURIComponent($(this).val())+"&";
   })
	$(".formInPopin input[@type=radio]:checked").each(function() {
      result += $(this).attr("name")+"="+$(this).val()+"&";
   })
	$(".formInPopin input[@type=checkbox]:checked").each(function() {
      result += $(this).attr("name")+"="+$(this).val()+"&";
   })
   $(".formInPopin select").each(function() {
      result += $(this).attr("name")+"="+$(this).val()+"&";
   })
   $(".formInPopin textarea").each(function() {
      result += $(this).attr("name")+"="+encodeURIComponent($(this).val())+"&";
   })
	//Envoie les donnees saisies au serveur
   $.ajax({
      type:"POST",
      url: myForm.attr("action"),
      data: result,
      success:function(response) {
      		$("#popin").empty();
      		$("#popin").append(response);
      		
      		// ADD EVENT
      		initSubmitFormInPopin(); 
      		initClosePopin();
      }
   });  
});

// popin de confirmation d'age 
var showPopinAccessProfil = (function (urlPopIn) {
	var oPopin = $("#popin"); // BICM TEST.
	if (!oPopin) return;
	
	showPopin();
	$("#popin").show();
	$("#popin").load(urlPopIn, "", function(){initShowPopin();initIdentification();});
});

var showShakerPopin = (function () {
	showPopin();
	$("#popin").show();
	$("#popin").load("/FRONT/COINTREAU/templates/dynamique/POPIN_Shaker.php?lang="+LANG_ID, "", function(){initShowPopin();initClosePopin()});
});
 
