/*js per newsletter*/

   /*
	 * contactable2 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.contactable2.js 2010-01-18 $
	 *
	 */
 
//extend the plugin
(function($){
	//define the new for the plugin ans how to call it	
	$.fn.contactable2 = function(options) {
		//set default options  
		var defaults = {
			nome_required: 'Nome',
			cognome_required: 'Cognome',
			email_required: 'Email',
			message : 'Message',
			subject : 'Messaggio dalla form di contatto del sito Linkness.',
			recievedMsg : 'Grazie per esseri iscritto alla Newsletter.',
			notRecievedMsg : 'Indirizzo già presente nella Newsletter.',
			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="contactable2"></div><div id="contactForm2" class="feedContainer"><a id="close_feedback2"  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="contactForm2Id"  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 >Nome<input type="text" id="nome_required" name="nome_required" /></label><label >Cognome<input type="text" id="cognome_required" name="cognome_required" /></label><label >E-mail<input type="text" id="email_required" name="email_required" /></label><label><a href="http://www.linkness.com/privacy.php" target="_blank" style="color:#87960a;">Legge sulla privacy</a><br/><input type="submit" name="Submit" value="INVIA" class="btn" style=" margin-top:8px;"/><span style=" padding-top:4px;">'+defaults.disclaimer+'</span></label></div></form><p class="thankNote" id="callback2"></p></div></div></div></div></div><div class="botCrv"></div></div>');
			
			$('#contactForm2Id #nome_required').example('Nome');
			$('#contactForm2Id #email_required').example('E-mail');
			$('#contactForm2Id #cognome_required').example('Cognome');

			
			//show / hide function
			/*
			$('div#contactable2').toggle(function() {
				$('#overlay22').css({display: 'block'});
				$(this).animate({"marginLeft": "-=5px"}, "fast"); 
				$('#contactForm2').animate({"marginLeft": "-=0px"}, "fast");
				$(this).animate({"marginLeft": "+=745"}, "slow"); 
				$('#contactForm2').animate({"marginLeft": "+=748px"}, "slow"); 
				contactShowStatus = 0;
			}, 
			function() {
				$('#contactForm2').animate({"marginLeft": "-=748px"}, "slow");
				$(this).animate({"marginLeft": "-=745px"}, "slow").animate({"marginLeft": "+=5px"}, "fast"); 
				$('#overlay22').css({display: 'none'});
			});
			*/

			

/** AGGIUNTO ############################################## */

			$('a#close_feedback2').click(function() {
					contactHide();
				}
			);
			
			$('div#contactable2').click(
				function() {
				if(contactShowStatus==1)
					contactShow();
				else
					contactHide();
			
			});
			
			function contactShow() {
				$('#overlay22').css({display: 'block'});
				$('div#contactable2').animate({"marginLeft": "-=5px"}, "fast"); 
				$('#contactForm2').animate({"marginLeft": "-=0px"}, "fast");
				$('div#contactable2').animate({"marginLeft": "+=748px"}, "slow"); 
				$('#contactForm2').animate({"marginLeft": "+=745px"}, "slow");
				$('.feedContainer .close').css({right: '-4px'});
				contactShowStatus = 0;
			}

			function contactHide() {
				$('#contactForm2').animate({"marginLeft": "-=745px"}, "slow");
				$('div#contactable2').animate({"marginLeft": "-=748px"}, "slow").animate({"marginLeft": "+=5px"}, "fast"); 
				$('#overlay22').css({display: 'none'});
				$('.feedContainer .close').css({right: '0px'});
				contactShowStatus =1;
			}

/** / AGGIUNTO ############################################## */


			//validate the form 
			$("#contactForm2Id").validate({
				//set the rules for the fild names
				rules: {
					nome_required: {
						required: true,
						minlength: 2
					},
					cognome_required: {
						required: true
					},

					email_required: {
						required: true,
						email: true
					}

				},
				//set messages to appear inline
				messages: {
					nome_required: "Inserisci il tuo nome",
					cognome_required: "Inserisci il tuo cognome",
					email_required: "Inserisci il tuo indirizzo E-mail"
				},
	
				submitHandler: function() {
					$('.holder').hide();
					$('#loading').show();
					$.post('../../../include/newsletter.php',{subject:defaults.subject, nome_required:$('#nome_required').val(), email_required:$('#email_required').val(), cognome_required:$('#cognome_required').val()},

					function(data){
					//	alert(data);
						$('#loading').css({display:'none'}); 
					
						if( data == 'success') {
							$('#callback2').show().text(defaults.recievedMsg);
						} else {
							$('#callback2').show().text(defaults.notRecievedMsg);
							$('#loading').hide();
							$('.holder').show();
							
						}
					});		
				}
			});
		});
	};
})(jQuery);