/*
 * 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: 'Nome',
			company: 'Azienda',
			city: 'Citta',
			email: 'Email',
			message : 'Message',
			subject : 'Messaggio dalla form di contatto del sito Linkness',
			recievedMsg : 'Messaggio inviato con successo. Sarete contattati al più presto',
			notRecievedMsg : 'Non è stato possibile inviare il vostro messaggio',
			disclaimer: 'Compila tutti i campi per inviarci la tua richiesta.',
			hideOnSubmit: true
		};

		//call in the default otions
		var options = $.extend(defaults, options);
		var contactShowStatus =1;

		//act upon the element that is passed into the design    
		return this.each(function(options) {
			//construct the form
			$(this).html('<div  id="contactable"></div><div id="contactForm" class="feedContainer"><a id="close_feedback"  title="Close"><img src="/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="/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="company" name="company" /></label><label ><input type="text" id="city" name="city" /></label><label><input type="text" id="email" name="email" /></label><label><textarea id="comment" name="comment"></textarea></label><a href="http://www.linkness.com/privacy.php" target="_blank" style="color:#87960a;">Legge sulla privacy</a><br/><label><input type="submit" name="Submit" value="INVIA" 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>');
			
			$('#contactFormId #name').example('Nome');
			$('#contactFormId #email').example('Email');
			$('#contactFormId #comment').example('Messaggio');
			$('#contactFormId #company').example('Ragione Sociale');
			$('#contactFormId #city').example('Città');
			
			//show / hide function
			/*
			$('div#contactable').toggle(function() {
				$('#overlay').css({display: 'block'});
				$(this).animate({"marginLeft": "-=5px"}, "fast"); 
				$('#contactForm').animate({"marginLeft": "-=0px"}, "fast");
				$(this).animate({"marginLeft": "+=745"}, "slow"); 
				$('#contactForm').animate({"marginLeft": "+=748px"}, "slow"); 
				contactShowStatus = 0;
			}, 
			function() {
				$('#contactForm').animate({"marginLeft": "-=748px"}, "slow");
				$(this).animate({"marginLeft": "-=745px"}, "slow").animate({"marginLeft": "+=5px"}, "fast"); 
				$('#overlay').css({display: 'none'});
			});
			*/

			

/** AGGIUNTO ############################################## */

			$('a#close_feedback').click(function() {
					contactHide();
				}
			);
			
			$('div#contactable').click(
				function() {
				if(contactShowStatus==1)
					contactShow();
				else
					contactHide();
			
			});
			
			function contactShow() {
				$('#overlay').css({display: 'block'});
				$('div#contactable').animate({"marginLeft": "-=5px"}, "fast"); 
				$('#contactForm').animate({"marginLeft": "-=0px"}, "fast");
				$('div#contactable').animate({"marginLeft": "+=748px"}, "slow"); 
				$('#contactForm').animate({"marginLeft": "+=745px"}, "slow");
				$('.feedContainer .close').css({right: '-4px'});
				contactShowStatus = 0;
			}

			function contactHide() {
				$('#contactForm').animate({"marginLeft": "-=745px"}, "slow");
				$('div#contactable').animate({"marginLeft": "-=748px"}, "slow").animate({"marginLeft": "+=5px"}, "fast"); 
				$('#overlay').css({display: 'none'});
				$('.feedContainer .close').css({right: '0px'});
				contactShowStatus =1;
			}

/** / AGGIUNTO ############################################## */


			//validate the form 
			$("#contactFormId").validate({
				//set the rules for the fild names
				rules: {
					name: {
						required: true,
						minlength: 2
					},
					company: {
						required: true
					},
					city: {
						required: true
					},
					email: {
						required: true,
						email: true
					},
					comment: {
						required: true
					}
				},
				//set messages to appear inline
				messages: {
					name: "Inserisci il tuo nome",
					company: "Completa il campo Ragione Sociale",
					city: "Completa il campo Città",
					email: "Inserisci il tuo indirizzo Email",
					comment: "Inserisci un messaggio"
				},

				submitHandler: function() {
					$('.holder').hide();
					$('#loading').show();
					$.post('../../../include/mail.php',{subject:defaults.subject, name:$('#name').val(), email:$('#email').val(), comment:$('#comment').val(), company:$('#company').val(), city:$('#city').val()},
					function(data){
						
						$('#loading').css({display:'none'}); 
					
						if( data == 'success') {
							$('#callback').show().append(defaults.recievedMsg);
						} else {
							$('#callback').show().append(defaults.notRecievedMsg);
						}
					});		
				}
			});
		});
	};
})(jQuery);


