var $j = jQuery.noConflict();
var Form = new function()
{
	this.init = function(body)
	{

		if($j('form.formular').length)
		{
			$j('#validate_me').validate();
			$j('form.formular').ajaxForm({
			     beforeSubmit: function(formData, jqForm, options)
			     {
					if($j('#validate_me').valid())
						{
							$j('body').append('<div id="form_overlay" style="opacity:0"></div>');
							$j('body').append('<div id="form_waiting_message" style="opacity:0">Formulaire en cours d\'envoi...</div>');
							$j('#form_overlay').animate({
								opacity:0.7
							},300);
							$j('#form_waiting_message').animate({
								opacity:1
							},300);
							return true;
						}
						else
						{
							return false;
						}
			     },
			     success: function(data, statusText)
			     {
					if(data == "ok")
					{
							$j('.valide_message_form').html('<span class="form_ok">Votre demande a bien &eacute;t&eacute prise en compte.</span>');
						
						
					}
					else
					{
						$j('.valide_message_form').html('<span class="form_ko">'+data+'</span>');
					}
					$j('#form_overlay,#form_waiting_message').animate({
						opacity:0
					}, 300, function(){
						$j('#form_overlay,#form_waiting_message').remove();
						$j('html').scrollTop($j('a#valide_message_form').position().top) 
					});
					return false;
			     },
			     error: function (errorData, textStatus, errorThrown) {
			    	 dbg('tata');
			         // typically only one of textStatus or errorThrown 
			         // will have info
			          alert(errorData.data);
			       }
			   });
		}
	}
	
	
	
}

function dbg()
{
  try {
    console.debug(arguments);
  }
  catch(e)
  {
    for(var i in arguments)
    {
      if (i<5) alert(arguments[i]);
    }
  }
};



$j(function(){
	Form.init(document.body);
});	


if ($j.validator) {
	 $j.extend($j.validator.messages, {
	   required: 'Champs requis',
	   email: "Champ courriel non valide.",
	   url: "Merci d'entrer une url valide.",
	   number: "Merci d'entrer un nombre valide.",
	   accept: "Le fichier n'a pas une extension valide.",
	   equalTo: "Veuillez entrer une nouvelle fois la mme valeur.",
	   maxlength: $j.format("Pas plus de {0} caractres."),
	   minlength: $j.format("Au moins {0} caractres.")
	 });
	 $j.validator.setDefaults({
	   debug: false
	 });
	}
