/*
 * 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',
			comment_detail : 'Message',
			location : $('#search_box h2 select :selected').text(),
			subject : 'A contactable message',
			recievedMsg : 'Thanks for your feedback.<br> We will take your comments into account for serving your needs.',
			notRecievedMsg : 'Thanks for your feedback.<br> We will take your comments into account for serving your needs.',
			disclaimer: 'Please input your feedback',
			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
			var location = $('#search_box h2 select :selected').text();
			$(this).html('<div id="contactable"></div><form id="contactForm" method="" action=""><div id="loading"></div><div id="callback"></div><div class="holder"><h3 id="comment_location">Comment location: '+location+'</h3><div id="box"><label for="name">Name </label>  <input id="location" type="hidden" name="location" value="" />    <br>    <input id="name" class="contact" name="name" /><br><label for="email">E-Mail</label><br /><input id="email" class="contact" name="email" /></div><p id="rating"><br>How do you rate this site overall?  <br>Worst <input name="rate" type="radio" value="1">1  <input name="rate" type="radio" value="2">2  <input name="rate" type="radio" value="3">3  <input name="rate" type="radio" value="4">4  <input name="rate" type="radio" value="5">5 Best</p><p id="message"><label for="comment">Your Feedback <span class="red"> * </span></label><br /><textarea id="comment_detail" name="comment_detail" class="comment" rows="4" cols="30" ></textarea></p><input class="submit" type="submit" value="Send"/><br><span class="disclaimer">'+defaults.disclaimer+'</span></div></form>');
			//show / hide function
			$('div#contactable').toggle(function() {
				$('#overlay').css({display: 'block'});
				$(this).animate({"marginRight": "-=5px"}, "fast"); 
				$('#contactForm').animate({"marginRight": "-=0px"}, "fast");
				$(this).animate({"marginRight": "+=387px"}, "slow"); 
				$('#contactForm').animate({"marginRight": "+=390px"}, "slow");
				$('h3#comment_location').html('Comment location: '+$('#search_box h2 select :selected').text());
				$('input#location').val($('#search_box h2 select :selected').text());
			}, 
			function() {
				$('#contactForm').animate({"marginRight": "-=390px"}, "slow");
				$(this).animate({"marginRight": "-=387px"}, "slow").animate({"marginRight": "+=5px"}, "fast"); 
				$('#overlay').css({display: 'none'});
			});
			
			//validate the form 
			$("#contactForm").validate({
				//set the rules for the fild names
				rules: {
					name: {
						required: false,
						minlength: 2
					},
					email: {
						required: false,
						email: true
					},
					comment_detail: {
						required: true
					},
					location: {
						required: true
					},
					rate: {
						required: false
					}
				},
				//set messages to appear inline
					messages: {
						name: "",
						email: "",
						comment_detail: "",
						rate: "",
						location: ""
					},			

				submitHandler: function() {
					$('.holder').hide();
					$('#loading').show();
					$.post('send_comment.php',{
						   subject:defaults.subject,
						   name:$('#name').val(),
						   email:$('#email').val(),
						   comment_detail:$('#comment_detail').val(),
							rate:$("input[@name='rate']:checked").val(),
						   location:$('#location').val()
						   },
					function(){
						$('#loading').css({display:'none'}); 
						data='success';
						if( data == 'success') {
							//alert($("input[@name='rate']:checked").val());
							$('#callback').show().append(defaults.recievedMsg);
							if(defaults.hideOnSubmit == true) {
								//hide the tab after successful submition if requested
								$('#contactForm').animate({dummy:1}, 2000).animate({"marginLeft": "-=450px"}, "slow");
								$('div#contactable').animate({dummy:1}, 2000).animate({"marginLeft": "-=447px"}, "slow").animate({"marginLeft": "+=5px"}, "fast"); 
								$('#overlay').css({display: 'none'});	
							}
						} else {
							$('#callback').show().append(defaults.notRecievedMsg);
						}
					});		
				}
			});
		});
	};
})(jQuery);


