/*
 * 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: jQueryId: jquery.contactable.js 2010-01-18 jQuery
 *
 */
 
//extend the plugin
(function(jQuery){

	//define the new for the plugin ans how to call it	
	jQuery.fn.contactable = function(options) {
		//set default options  
		var defaults = {
			url: 'http://www.squashedpixel.co.uk/themes/squashedpixel/templates/mail.php',
			name: 'Name',
			email: 'Email',
			telno: 'Phone Number',			
			message : 'Message',
			subject : 'A contactable message',
			submit : 'SEND',
			recievedMsg : 'Thank you for your message, someone from Squashed Pixel will be in touch shortly.<br /> If your enquiry is more urgent please call the office on 01274 270 029',
			notRecievedMsg : 'Sorry but your message could not be sent, try again later',
			disclaimer: 'We aim to respond to all enquiries as quickly as possible but please be aware that during busy times this may take a little longer than usual',
			hideOnSubmit: false

		};

		//call in the default otions
		var options = jQuery.extend(defaults, options);
		//act upon the element that is passed into the design    
		return this.each(function() {
			//construct the form
			var this_id_prefix = '#'+this.id+' ';
			jQuery(this).html('<div id="contactable_inner"></div><form id="contactForm" method="" action=""><div id="loading"></div><div id="callback"></div><div class="holder"><h2>Quick Enquiry</h2><p>Want to know if Squashed Pixel can help your business? Fill out the short form below and well get back to you with more information as soon as we can</p><p><label for="name">'+options.name+'<span class="red"> * </span></label><br /><input id="name" class="contact" name="name"/></p><p><label for="email">'+options.email+' <span class="red"> * </span></label><br /><input id="email" class="contact" name="email" /></p><p><label for="telno">'+options.telno+'<span class="red"> * </span></label><br /><input id="telno" class="contact" name="telno" /></p><p><label for="message">'+options.message+' <span class="red"> * </span></label><br /><textarea id="message" name="message" class="message" rows="4" cols="30" ></textarea></p><p><input class="submit" type="submit" value="'+options.submit+'"/></p><p class="disclaimer">'+options.disclaimer+'</p></div></form>');
			//show / hide function
			jQuery(this_id_prefix+'div#contactable_inner').toggle(function() {
				jQuery(this_id_prefix+'#overlay').css({display: 'block'});
				jQuery(this).animate({"marginLeft": "-=5px"}, "fast"); 
				jQuery(this_id_prefix+'#contactForm').animate({"marginLeft": "-=0px"}, "fast");
				jQuery(this).animate({"marginLeft": "+=387px"}, "slow"); 
				jQuery(this_id_prefix+'#contactForm').animate({"marginLeft": "+=390px"}, "slow"); 
			}, 
			function() {
				jQuery(this_id_prefix+'#contactForm').animate({"marginLeft": "-=390px"}, "slow");
				jQuery(this).animate({"marginLeft": "-=387px"}, "slow").animate({"marginLeft": "+=5px"}, "fast"); 
				jQuery(this_id_prefix+'#overlay').css({display: 'none'});
			});
			
			//validate the form 
			jQuery(this_id_prefix+"#contactForm").validate({
				//set the rules for the fild names
				rules: {
					name: {
						required: true,
						minlength: 2
					},
					email: {
						required: true,
						email: true
					},
					telno: {
						required: true,
						minlength:2
					},
					message: {
						required: true
					}
				},
				//set messages to appear inline
					messages: {
						name: "",
						email: "",
						telno: "",
						message: ""
					},			

				submitHandler: function() {
					jQuery(this_id_prefix+'.holder').hide();
					jQuery(this_id_prefix+'#loading').show();
jQuery.ajax({
  type: 'POST',
  url: options.url,
  data: {subject:options.subject, name:jQuery(this_id_prefix+'#name').val(), email:jQuery(this_id_prefix+'#email').val(),telno:jQuery(this_id_prefix+'#telno').val(), message:jQuery(this_id_prefix+'#message').val()},
  success: function(data){
						jQuery(this_id_prefix+'#loading').css({display:'none'}); 
						if( data == 'success') {
							jQuery(this_id_prefix+'#callback').show().append(options.recievedMsg);
							if(options.hideOnSubmit == true) {
								//hide the tab after successful submition if requested
								jQuery(this_id_prefix+'#contactForm').animate({dummy:1}, 2000).animate({"marginLeft": "-=450px"}, "slow");
								jQuery(this_id_prefix+'div#contactable_inner').animate({dummy:1}, 2000).animate({"marginLeft": "-=447px"}, "slow").animate({"marginLeft": "+=5px"}, "fast"); 
								jQuery(this_id_prefix+'#overlay').css({display: 'none'});	
							}
						} else {
							jQuery(this_id_prefix+'#callback').show().append(options.notRecievedMsg);
							setTimeout(function(){
								jQuery(this_id_prefix+'.holder').show();
								jQuery(this_id_prefix+'#callback').hide().html('');
							},2000);
						}
					},
  error:function(){
						jQuery(this_id_prefix+'#loading').css({display:'none'}); 
						jQuery(this_id_prefix+'#callback').show().append(options.notRecievedMsg);
                                        }
});		
				}
			});
		});
	};
 
})(jQuery);

