/*
 * 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	
	$.fn.contactable = function(options) {
		//set default options  
		var defaults = {
			name: 'Name',
			email: 'Email',
			message : 'Message',
			subject : '[Illicontact] Un internaute souhaite nous contacter',
			recievedMsg : 'Merci pour votre message',
			notRecievedMsg : 'Sorry but your message could not be sent, try again later',
			disclaimer: '',
			hideOnSubmit: true
		};

		//call in the default otions
		var options = $.extend(defaults, options);
		//act upon the element that is passed into the design    
		return this.each(function(options) {
			//construct the form
			var currentTime = new Date();
			var heure= currentTime.getHours();
			/*if (heure <9 || heure>=19 )
			{*/
				$(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">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="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>');
/*			}
			else 
			{
				$(this).html('<div id="contactable"></div><form id="contactForm" method="post" action="/ekiphone.php"><div id="loading"></div><div id="callback"></div><div class="holder"><p><label for="name">Votre num&eacute;ro pour un contact Illico<span class="red"> * </span></label><br /><input id="name" class="contact" name="name" /><br /></p><p></p><br /><div class="holder"><p></p>En indiquant votre num&eacute;ro de t&eacute;l&eacute;phone, vous allez &ecirc;tre mis en relation avec un commercial<br /> Assurez vous d\'&ecirc;tre &agrave; proximit&eacute; de votre t&eacute;l&eacute;phone<br />Service gratuit de mise en relation</div><p><input class="submit" type="submit" value="Lancer l\'appel"/></p><p class="disclaimer">'+defaults.disclaimer+'</p></div></form>');
			}*/
			//show / hide function
			$('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 
			/*if (heure <9 || heure>=19 )
			{*/
			$("#contactForm").validate({
				//set the rules for the fild names
				rules: {
					name: {
						required: true,
						minlength: 2
					},
					email: {
						required: true,
						email: true
					},
					comment: {
						required: true
					}
				},
				//set messages to appear inline
					messages: {
						name: "",
						email: "",
						comment: ""
					},			

				submitHandler: function() {
					$('.holder').hide();
					$('#loading').show();
					$.post('./js/contactable/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) {
								//hide the tab after successful submition if requested
								$('#contactForm').animate({dummy:1}, 2000).animate({"marginLeft": "-=450px"}, "slow");
								$('div#contactable').animate({dummy:1}, 2000).animate({"marginLeft": "-=447px"}, "slow").animate({"marginLeft": "+=5px"}, "fast"); 
								$('#overlay').css({display: 'none'});	
							}
						} else {
							$('#callback').show().append(defaults.notRecievedMsg);
						}
					});		
				}
			});
			/*}
			else
			{
				$("#contactForm").validate({
					//set the rules for the fild names
					rules: {
						name: {
							required: true,
							minlength: 10
						}
					},
					//set messages to appear inline
						messages: {
							name: ""
						},			
	
					submitHandler: function() {
						$('.holder').hide();
						$('#loading').show();
						$.post('/ekiphone.php',{name:$('#name').val()},
						function(data){
							$('#loading').css({display:'none'}); 
							if( data == 'success') {
								$('#callback').show().append(defaults.recievedMsg);
								if(defaults.hideOnSubmit == true) {
									//hide the tab after successful submition if requested
									$('#contactForm').animate({dummy:1}, 2000).animate({"marginLeft": "-=450px"}, "slow");
									$('div#contactable').animate({dummy:1}, 2000).animate({"marginLeft": "-=447px"}, "slow").animate({"marginLeft": "+=5px"}, "fast"); 
									$('#overlay').css({display: 'none'});	
								}
							} else {
								$('#callback').show().append(defaults.notRecievedMsg);
							}
						});		
					}
				});
			}*/
		});
	};
})(jQuery);


