// close jqmodal when user strokes "esc" key
$().ready(function(){
   	
   	$(document).keydown( function( e ) {
	   if( e.which == 27) {  // escape, close box
	     $(".jqmWindow").jqmHide();
	   }
 	}); 
 	
});

/*------------------------------
	inline labels
------------------------------*/
$().ready(function(){
	
	// Select all textboxes and assign them to an array
	var textboxes = $('form.awesome input.input-text');
	
	// Iterate through all textboxes in the form
	textboxes.each(function(index, input){
	
	var label = $(input).prev();
	
	label.wrapInner("<span></span>")
	label.children("span").animate({opacity: 0.6},0);
	
	// check for autocomplete by browser
    if( index == 0 ){
      setInterval(function(){
         textboxes.each(function(index,inputX){
          if ( inputX.value!="" ) {
            $(inputX).prev().children("span").animate({opacity: 0},0);
            label.addClass('hastext'); 
          }
        });
     }, 100);
    }
	
	//ie
	label.click(function(){
		input.focus();
	});
	
	// Fade the label back when a field gains focus		
	input.onfocus = function(){
	  if (input.value==""){
	    //label.children("span").animate({opacity: 0.4},200);
	    label.addClass('focus');  
	  }
	}
	
	// Check if a field is empty when the user switches out
	input.onblur = function(){
	  if (input.value==""){
	    //label.children("span").animate({opacity: 0.6},200);  
	    label.removeClass('focus').removeClass('hastext');         
	  }
	}
	
	// Fade the label back if a field has text		
	if (input.value!="") {
	  label.addClass('hastext');
	}
	    
	// Fade the label back when the user starts to type		
	input.onkeypress = function(){
	  //label.children("span").animate({opacity: 0},200);
	  label.addClass('hastext');
	};
  });
  
});

/*------------------------------
	inscription
------------------------------*/
function showError(error, element){
	element.animate({opacity: 0},200);
	error.animate({opacity: 1},200);
	
	setTimeout (function(){
		error.animate({opacity: 0},200);
		element.animate({opacity: 1},200);
		},1000);
}

$().ready(function() { 
	
	$("#inscription form").validate({
		debug: false,
		onkeyup: false,
		onfocusout: false,
		
		errorPlacement: function(error, element) {
				error.insertAfter( element );
				showError(error, element);
				
				/* écouteurs */
				// touche entrée
				element.bind("keypress",function(e){	
					if(e.which == 13){ // si c'est la touche enter
						showError(error, element);
					}
				});
				//clic sur submit
				element.parents("form").children("button[type=submit]","input[type=submit]").click(function(){showError(error, element)});
				
	   	},
		
		rules: { 
			email:{
				required: true,
				minlength: 5,
				email: true
			}
		},
	
		messages: { 
			email:{
				required: "Vous devez saisir votre email",
				email: "L'email saisi n'est pas valide"
			}
		},
		
		submitHandler: function(form) {
		   	$(form).ajaxSubmit({
		   		success: function(responseText, statusText){
		   			alert(responseText);
					/*
					setTimeout (function(){
				   		$("#alert").jqmHide()
				   	},3500);
					*/
		   		}
		   	});
		   	
		   	
		}
	});

});




/*------------------------------------------------------------
	popins : inviter des amis à participer / mdp oublié
------------------------------------------------------------*/
var unload = function(hash){
	hash.o.remove(); hash.w.hide(); 
	$('div.jqmContent').empty();
	
}

var load = function(hash) {
	// URL du lien 
	var URL = hash.t.href;
	
	// chargement
	$('div.jqmContent').load(URL, {} ,
		function() { 
			// ajout du div d'affichage de messages au div contenant le formulaire
			$('.formbody').prepend("<div id='confirm_error' class='hide'></div>");
			
			$(".jqmContent form").validate({
				debug: false,
				onkeyup: false,
				submitHandler: function(form) {
				   	$(form).ajaxSubmit({
				   		success: function(responseText, statusText){
								 $("#confirm_error").html(responseText).fadeIn();
								 /*
								setTimeout (function(){
							   		$(".jqmWindow").jqmHide()
							   	 },3500);
								*/
				   		}
				   	});
			   	}
			});
			
		}//fin callback
	);
	
	hash.w.show();
}

$().ready(function(){
	$('.jqmWindow').jqm({
		trigger: '.trigger', 
		overlay:40,
		onShow: load,
		onHide: unload
	});
});


