/*
 * 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(jQuery){

	//define the new for the plugin ans how to call it	
	jQuery.fn.contactable = function(options) {
		//set default options  
		var defaults = {
			name: 'Name',
			email: 'Email',
			message : 'Message',
			recipient : 'lihua.wang@mediawoz.com',
			subject : 'A contactable message',
			recievedMsg : 'Thankyou for your message!',
			notRecievedMsg : 'Sorry but your message could not be sent, try again later!',
			disclaimer: 'Please feel free to get in touch, we value your feedback.'
		};

		//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(options) {
			//construct the form
			jQuery(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">姓名 <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">反馈信息 <span class="red"> * </span></label><br /><textarea id="comment" name="comment" class="comment" rows="4" cols="30" ></textarea></p><p><input id="fsbm" type="submit" value=""/></p><p>'+defaults.disclaimer+'</p></div></form>');
			//show / hide function
			jQuery('div#contactable').toggle(function() {
				jQuery('#overlay').css({display: 'block'});
				jQuery(this).animate({"marginLeft": "-=0px"}, "fast"); 
				jQuery('#contactForm').animate({"marginLeft": "-=0px"}, "fast");
				jQuery(this).animate({"marginLeft": "+=259px"}, "slow"); 
				jQuery('#contactForm').animate({"marginLeft": "+=259px"}, "slow"); 
			}, 
			function() {
				jQuery('#contactForm').animate({"marginLeft": "-=259px"}, "slow");
				jQuery(this).animate({"marginLeft": "-=259px"}, "slow").animate({"marginLeft": "+=0px"}, "fast"); 
				jQuery('#overlay').css({display: 'none'});
			});
			
			//validate the form 
			jQuery("#contactForm").validate({
				//errorElement: "em",
				//errorPlacement: function(error, element) {
				//	error.appendTo(element.prev("label"));
				//},
				//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: "",
					email: "",
					comment: ""
				},
				submitHandler: function() {
					jQuery('.holder').hide();
					jQuery('#loading').show();
					jQuery.get('mail.php',{recipient:jQuery('#recipient').val(), subject:jQuery('#subject').val(), name:jQuery('#name').val(), email:jQuery('#email').val(), comment:jQuery('#comment').val()},
					function(data){
						jQuery('#loading').css({display:'none'});
						if( data == 'success') {
							jQuery('#callback').show().append(defaults.recievedMsg);
						} else {
							jQuery('#callback').show().append(defaults.notRecievedMsg);
						}
					});
				}
			});
		});
	};
	//end the plugin call 
})(jQuery);


