$(document).ready(function(){
// mailing list bloc validation
	$("#mailing-email").focus(function(){
		if ( $(this).val() == "Entrer votre email" ){
			$(this).val("");
		}
	});

	$("#bt_go").click(function(){
		if ( !$("#mailing-email").validation({type:"email",text:"Entrer votre adresse email",autre_texte:"Entrer une adresse E-mail valide"}) ) return false;	
//		window.location = $(this).attr("href")+"?email-mailing="+encodeURI($("#mailing-email").val()) ;
		window.location = "/newsletter?email-mailing="+encodeURI($("#mailing-email").val()) ;
		return false;
	});
	
	
// clear mailing box
	$('#mailing-email').focus(function() {
		$(this).val('');
	});
	
	
// clear search box
	$('form.search input[name=query]').focus(function() {
		$(this).val('');
	});

	// protected email class
	$('a.myPmsg').emailencode();
	
	// block mailing
	$.get('/ajax/mailing.php', function(data) {
		$('#mailing').html(data);
	});
});

/* LiteTest email encode
* Version 0.9
* Author Jonas Hagstedt
* URI: http://www.litetest.com/Resources.aspx/EmailEncode
* Released with the MIT License: http://www.opensource.org/licenses/mit-license.php
*/
(function($) {
	$.fn.emailencode = function(options) {
		var settings = jQuery.extend({
			atsign: "#"
		}, options);

		return this.each(function() {
			var address = $(this).attr("href");
			var formatedAddress = address.replace(settings.atsign, "@");
			$(this).attr("href", "mailto:" + formatedAddress);
		});
	};
})(jQuery);

(function($) {
	//Définition du plugin
    $.fn.validation = function(options) {   
       
        // définition des paramètres par défaut
        var defaults = {
            type	   : "text",
			text	   : "Champs obligatoire vide",
			autre_texte: "Format du champs incorrecte",
			format	   : "jj/mm/aaaa"
        };   
		
        // mélange des paramètres fournis et des paramètres par défaut
        var opts = $.extend(defaults, options);       
        
		if ( $(this).val() == "" ) 
	  	{
			alert(opts.text);
			$(this).focus();
			return false;
	  	}
		
		
        switch (opts.type)  
		{
			case "text"  : break ;
			case "email" : var email = $(this).val();
						   var verif = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+$/;
						   if ( verif.exec(email) == null ) 
						   {
						   		alert(opts.autre_texte);
								$(this).focus();
								return false;
						   }
						   break ;
						   
			case "chiffre" :			   
			case "nombre" : var chiffre = $(this).val();
							 var verif = /^([0-9])+$/;
						   	 if ( verif.exec(chiffre) == null ) 
							 {
								alert(opts.autre_texte);
								$(this).focus();
								return false;
						     }
						     break ;	
							 
			case "date" :    var date = $(this).val();
							 var verif = /^[0-9]{2}-[0-9]{2}-[0-9]{4}$/; 
							 if ( opts.format == "jj/mm/aaaa" ) verif = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/; 
							 else if ( opts.format == "jj.mm.aaaa" ) verif = /^[0-9]{2}\.[0-9]{2\.[0-9]{4}$/; 
							 
						   	 if ( verif.exec(date) == null ) 
							 {
								alert(opts.autre_texte);
								$(this).focus();
								return false;
						     }
						     break ;	

			case "fichier" : var fichier = $(this).val();
							 var verif = /^[a-z0-9-_]+$/; 
						   	 if ( verif.exec(fichier) == null ) 
							 {
								alert(opts.autre_texte);
								$(this).focus();
								return false;
						     }
						     break ;
	
			case "telephone" : var tel = $(this).val();
							   var verif = /^[+]{0,1}[\ 0-9]+$/; 
						   	   if ( verif.exec(tel) == null ) 
							   {
							   		alert(opts.autre_texte);
									$(this).focus();
									return false;
							   }
			 			       break ;
						  
			default 	: break ;
		}

        // interface fluide
        return true;
    };   
})(jQuery);

