/*
 * contactable 1.0 - 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 2009-08-24 $
 *
 */
 
//extend the plugin
(function($j){
	//define the new for the plugin ans how to call it	
	$j.fn.contactable = function(options) {
		//set default options  
		var defaults = {
			name: 'Name',
			email: 'Email',
			message : 'Message',
			recipient : 'info@pnlinpratica.com',
			subject : 'A contactable message',
			recievedMsg : 'Grazie, verrai ricontattato al pi&ugrave; presto',
			notRecievedMsg : 'Spiacenti, il tuo messaggio non &egrave; stato inviato, riprova pi&ugrave; tardi',
			disclaimer: ''
		};

		//call in the default otions
		var options = $j.extend(defaults, options);
		var contactShowStatus =1;

		
		//act upon the element that is passed into the design    
		return this.each(function(options) {
			//construct the form
			$j(this).html('<div  id="contactable"></div><div id="contactForm" class="feedContainer"><!--<a id="close_feedback" title="Close"><img src="include_template/plugin-images/btn-close.png" border="0" alt="Close" class="close" /></a>--><div class="topCrv"></div><div class="leftHand"><div class="leftshad"><div class="rightHand"><div class="rightshad"><div class="gradient"><form id="contactFormId"  method="" action=""><div id="loading">Please Wait..<br><img src="include_template/plugin-images/ajax-loader.gif" border="0"/></div><div class="holder"><input type="hidden" id="recipient" name="recipient" value="'+defaults.recipient+'" /><input type="hidden" id="subject" name="subject" value="'+defaults.subject+'" /><label ><input type="text" id="name" name="name" /></label><label><input type="text" id="email" name="email" /></label><label><textarea id="comment" name="comment"></textarea></label><label><input type="submit" name="Submit" value="SEND" class="btn" /><span style=" padding-top:4px;">'+defaults.disclaimer+'</span></label></div></form><p class="thankNote" id="callback"></p></div></div></div></div></div><div class="botCrv"></div></div>');
			
			$j('#contactFormId #name').example('Name');
			$j('#contactFormId #email').example('Email');
			$j('#contactFormId #comment').example('Message');
			
		/*	
			$j(this).html('<div id="contactable"></div><form id="contactForm" method="" action=""><div id="loading"></div><div id="callback"></div><div class="holder"><input type="hidden" id="recipient" name="recipient" value="'+defaults.recipient+'" /><input type="hidden" id="subject" name="subject" value="'+defaults.subject+'" /><p><label for="name">Name <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">Your Feedback <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="Send"/></p><p>'+defaults.disclaimer+'</p></div></form>');
		*/	
			//show / hide function
		function contactShow() {
				$j('#overlay').css({display: 'block'});
				$j('div#contactable').animate({"marginLeft": "-=5px"}, "fast"); 
				$j('#contactForm').animate({"marginLeft": "-=0px"}, "fast");
				$j('div#contactable').animate({"marginLeft": "+=748px"}, "slow"); 
				$j('#contactForm').animate({"marginLeft": "+=745px"}, "slow");
				$j('.feedContainer .close').css({right: '-4px'});
				contactShowStatus = 0;
			}
		function contactHide() {
				$j('#contactForm').animate({"marginLeft": "-=745px"}, "slow");
				$j('div#contactable').animate({"marginLeft": "-=748px"}, "slow").animate({"marginLeft": "+=5px"}, "fast"); 
				$j('#overlay').css({display: 'none'});
				$j('.feedContainer .close').css({right: '0px'});
				contactShowStatus =1;
			}
			
			$j('div#contactable').click(
				function() {
				if(contactShowStatus==1)
					contactShow();
				else
					contactHide();
			
			});
			
			$j('a#close_feedback').click(function() {
				contactHide();
			
			}
			);
			//validate the form 
			$j("#contactFormId").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: "Inserisci il tuo nome",
					email: "Inserisci la tua email",
					comment: "Inserisci il tuo messaggio"
				},
				submitHandler: function() {
					$j('.holder').hide();
					$j('#loading').show();
					$j.get('include_template/include/mail.php',{recipient:$j('#recipient').val(), subject:$j('#subject').val(), name:$j('#name').val(), email:$j('#email').val(), comment:$j('#comment').val()},
					function(data){
						$j('#loading').css({display:'none'});
						$j('.holder').show();
						//document.forms['contactFormId'].reset()
						name:$j('#name').val('');
						email:$j('#email').val('');
						comment:$j('#comment').val('');
						
						if( !data ) {
							$j('#callback').show().append(defaults.recievedMsg);
						} else {
							$j('#callback').show().append(defaults.notRecievedMsg);
						}
					});		
				}
			});
		});
	};
	//end the plugin call 
})(jQuery);

