/*
 * 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 : 'Clearway message',
			recievedMsg : 'Thank you for your message :) ',
			notRecievedMsg : 'Sorry but your message could not be sent. Reach us directly at info@clearwaydesign.com',
			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
			$(this).html('<div id="contactable"></div><form id="contactForm" method="" action=""> <label> Name* </label><input  id="name" type="name" class="formsmall name" name="name" /><br /> <label> Email* </label><input id="email" type="email" class="formsmall email" name="email" /><br /> <label> Message* </label> <textarea id="comment" name="comment" class="formsmall message"></textarea> <br /> <input type="submit" class="submit" value="Send" /><br /><div id="loading"></div></form><div id="callback"></div><div id="callbackerror"></div> ');
			//show / hide function
		
			//validate the form 
			$("#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: "What's your name?",
						email: "Your email needs to be valid",
						comment: "Say Something..."
					},			

				submitHandler: function() {
					$('#loading').show();
					$.post('mail.php',{subject:defaults.subject, name:$('#name').val(), email:$('#email').val(), comment:$('#comment').val()},
					function(data){
						$('#loading').css({display:'none'}); 
						if( data == 'success') {
							$('#contactForm').css({display:'none'}); 
							$('#callback').show().append(defaults.recievedMsg);
							
						} else {
							$('#callbackerror').show().append(defaults.notRecievedMsg);
						}
					});		
				}
			});
		});
	};
})(jQuery);






