/*
 * contactable 1.2.1 - jQuery Ajax contact form
 *
 * Copyright (c) 2009 Philip Beel (http://www.theodin.co.uk/)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Revision: $Id: jquery.contactable.js 2010-01-18 $
 *
 */
 
//extend the plugin
(function($){

  //define the new for the plugin ans how to call it
 //Définir le nouveau pour le plugin ans comment l'appeler
  $.fn.contactable = function(options) {
    //set default options
//options par défaut
    var defaults = {
      name: 'Name',
      email: 'Email',
      message : 'Message',
      subject : 'Merci',
      recievedMsg : 'Votre message &agrave; &eacute;t&eacute; envoy&eacute;',
      notRecievedMsg : 'D&eacute;sol&eacute; mais votre message n\'a pas pu &ecirc;tre envoy&eacute;, reessayez plus tard',
      disclaimer: ' N\'h&eacute;sitez pas &agrave; nous contacter',
      hideOnSubmit: true
    };

    //Options appel à la valeur par défaut
    var options = $.extend(defaults, options);
    //act upon the element that is passed into the design    
    return this.each(function(options) {
      //construct the form
      $(this).html('<div id="contactable"></div><form id="contactForm" method="" action=""><div id="loading"></div><div id="callback"></div><div class="holder"><p><label for="name">Votre Nom <span class="red"> * </span></label><br /><input id="name" class="contact" name="name" /></p><p><label for="email">E-Mail <span class="red"> * </span></label><br /><input id="email" class="contact" name="email" /></p><p><label for="tel">Telephone</label><br /><input id="tel" class="contact" name="tel" /></p><p><label for="comment">Votre Message *  <span class="red"> * </span></label><br /><textarea id="comment" name="comment" class="comment" rows="4" cols="30" ></textarea></p><p><input class="submit" type="submit" value="Envoyer"/></p><p class="disclaimer">'+defaults.disclaimer+'</p></div></form>');
      //montrer / fonction cacher
      $('div#contactable').toggle(function() {
        $('#overlay').css({display: 'block'});
        $(this).animate({"marginLeft": "-=5px"}, "fast"); 
        $('#contactForm').animate({"marginLeft": "-=0px"}, "fast");
        $(this).animate({"marginLeft": "+=387px"}, "slow"); 
        $('#contactForm').animate({"marginLeft": "+=390px"}, "slow"); 
      }, 
      function() {
        $('#contactForm').animate({"marginLeft": "-=390px"}, "slow");
        $(this).animate({"marginLeft": "-=387px"}, "slow").animate({"marginLeft": "+=5px"}, "fast"); 
        $('#overlay').css({display: 'none'});
      });
      
      //validate the form 
      $("#contactForm").validate({
        //set the rules for the fild names
    //fixer les règles pour les noms FILD
        rules: {
          name: {
            required: true,
            minlength: 2
          },
          email: {
            required: true,
            email: true
          },
          comment: {
            required: true
          }
        },
        //Messages mis à comparaître en ligne dans le mail reçu
          messages: {
            name: "",
            email: "",
            comment: ""
          },      

        submitHandler: function() {
          $('.holder').hide();
          $('#loading').show();
          $.post('mail.php',{subject:defaults.subject, name:$('#name').val(), email:$('#email').val(), comment:$('#comment').val()},
          function(data){
            $('#loading').css({display:'none'}); 
            if( data == 'success') {
              $('#callback').show().append(defaults.recievedMsg);
              if(defaults.hideOnSubmit == true) {
                //Masquer l'onglet après submition succès à la demande
                $('#contactForm').animate({dummy:1}, 2000).animate({"marginLeft": "-=390px"}, "slow");
                $('div#contactable').animate({dummy:1}, 2000).animate({"marginLeft": "-=387px"}, "slow").animate({"marginLeft": "+=5px"}, "fast"); 
                $('#overlay').css({display: 'none'});  
              }
            } else {
              $('#callback').show().append(defaults.notRecievedMsg);
            }
          });    
        }
      });
    });
  };
})(jQuery);


