/* 	Modal
	Auteur : Hervé FRACKOWIAK
	Date : 29/05/2009
*/



//Au chargement de la page
$(document).ready(function(){
	// Appel des modales
//	modal_init("#newsletter .titre","#modal_newsletter","/nocibe/AjaxNewsletter?"+paramsGlobal,-359,27);
	modal_init("#espacePerso","#modal_espacePerso","/nocibe/AjaxCustomerPersonalSpace?"+paramsGlobal,-39,27);
//	modal_init("#panier","#modal_panier","/nocibe/AjaxHeaderBasket?"+paramsGlobal,-28,47);
	
	$("#newsletter .savoirPlus").click(function(){
		Box.open({
			 url: "/nocibe/00_include/04_modal/infoNewsletter.jsp?"+paramsGlobal,
			 modal:true
		 });
	});
});


//Fonction d'initialisation
function modal_init(id_zone,id_modal,fichier,largeur,hauteur) {
	//Survol de la zone
	
		$(id_zone).bind("click",function(){			
			modal_open(id_zone,id_modal,fichier,largeur,hauteur);			
		});
	
	
	function modal_open(id_zone,id_modal,fichier,largeur,hauteur) {
		$(id_zone).unbind("click");
		//On verifie l'existence
		if($(id_modal).length==0) {
			//Loader les elements
			$.get(fichier,function(data){
				$("body").prepend('<div id="modal_sortie"><img src="/nocibe/res/fr_FR/img/1ptrans.gif" border="0" alt=""></div>'+data);
				$("#modal_sortie img").css({width: $(window).width(), height: $(document).height()});
				//Changement z-index
				$(id_zone).css("z-index","100");
				//Recuperation de la position
				var origine = $(id_zone+" img").offset();
				var top = origine.top + hauteur;
				var left = origine.left + largeur;
				//Calcul de top et left
				var hmodal = "-"+$(id_modal).height()+"px";
				$(id_modal).css({"top":hmodal,"left":left+"px"});
				//Animation de l'element
				$(id_modal).animate({
					opacity:"show",
					top:top+"px"
				},750,function(){
					//Bind de l'oubli de mot de passe.
					$("#modal_espacePerso .password a").click(function(){
						//On appel la sortie
						$("#modal_sortie").trigger("mouseenter");
						//On call la modale password
						Box.open({
							 url: "/nocibe/00_include/04_modal/oubli_pass.jsp?"+paramsGlobal,
							 modal:true
							// top:50,
							// left:60
						 });
					});
					
					//Type de sortie, normal ou btn_close
					if($(id_modal+" .btn_close_modal").length > 0)
					{
						//Sortie Bouton
						$(id_modal+" .btn_close_modal").click(function(){modal_exit(id_modal,id_zone,hmodal);});
						
					}
					else
					{
						//Sortie Calque
						$("#modal_sortie").bind("mouseenter mousemove",function(){modal_exit(id_modal,id_zone,hmodal);});
						
					}
				});
			});
		}
	}
	
	//Fonction de sortie de modal
	function modal_exit(id_modal,id_zone,hmodal)
	{		
		//Animation de l'element
		$(id_modal).animate({
			opacity:"hide",
			top:hmodal
		},750,function(){
			//Changement z-index
			$(id_zone).css("z-index","0");
			//Suppresion
			$("#modal_sortie, "+id_modal).remove();
		});	
		
		$(id_zone).bind("click",function(){			
			modal_open(id_zone,id_modal,fichier,largeur,hauteur);			
		});
	}
	
	
	// Validation du formulaire de login de la modale espace perso
	var validator1 = $("#espacePersoNonConnecte").validate({

		rules: {
			mdp: 	{ required: true, rangelength: [6, 10] },
			email:	{ required: true, email: true, maxlength: 70 }
		},
		messages: {
			mdp:	{ required: "Indiquez votre mot de passe", rangelength: "Votre mot de passe doit posséder au moins 6 caractères" },
			email:	{ required: "Indiquez votre adresse e-mail",	email: "Le format de l'e-mail saisi est incorrect", maxlength: "L'adresse e-mail ne doit pas excéder 70 caractères" }
		},
		// the errorPlacement has to take the table layout into account
		errorPlacement: function(error, element) {
			error.appendTo( element.next().next() );
			element.next().removeClass("valid").addClass("notvalid");
		},
		// specifying a submitHandler prevents the default submit, good for the demo
		submitHandler: function() {
			alert("formulaire OK!");
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			label.parent().prev().removeClass("notvalid").addClass("valid");
			label.remove();
		}
	});
	
	
}
