/*
 * 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 = {
			recievedMsg : 'Thank you for your message',
			notRecievedMsg : 'Sorry but your message could not be sent, try again later',
			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="post" action="/feedback/add"><fieldset style="display:none;"><input type="hidden" name="_method" value="POST" /></fieldset><div id="loading"></div><div id="callback"></div><div class="holder"><div class="input required"><label for="FeedbackName">Name <span class="red">*</span></label><input name="data[Feedback][name]" type="text" maxlength="100" value="" id="FeedbackName" /></div><div class="input required"><label for="FeedbackEmail">Email <span class="red">*</span></label><input name="data[Feedback][email]" type="text" maxlength="255" value="" id="FeedbackEmail" /></div><div class="input select required"><label for="FeedbackShowId">Show <span class="red">*</span></label><select name="data[Feedback][show_id]" id="FeedbackShowId"> <option value="">Please Select</option> <optgroup label="Current Season"> <option value="9">The Green Book</option> <option value="14">Chefs That Stir the Soul</option> <option value="10">Freud&#039;s Last Session</option> <option value="11">Red</option> <option value="12">A Wrinkle in Time</option> </optgroup> <optgroup label="Past Seasons"> <option value="19">Gee&#039;s Bend</option> <option value="22">Big River</option> <option value="23">A Lesson Before Dying</option> <option value="17">Going With Jenny</option> <option value="18">Tent Meeting</option> <option value="21">Driving Miss Daisy</option> <option value="16">Blood Knot (with True Colors Theatre Company)</option> <option value="2">Cotton Patch Gospel</option> <option value="1">Around the World in 80 Days</option> <option value="3">Amahl and the Night Visitors</option> <option value="4">Brownie Points</option> <option value="15">The Sunset Limited</option> <option value="5">Blues in the Night</option> <option value="6">A Confederacy of Dunces</option> <option value="20">A Christmas Memory</option> <option value="7">The Young Man From Atlanta</option> <option value="8">Travelin&#039; Black</option> <option value="13">Summer Concert Series</option> </optgroup> </select></div><div class="input required"><label for="FeedbackComments">Comments <span class="red">*</span></label><textarea name="data[Feedback][comments]" cols="30" rows="6" id="FeedbackComments" ></textarea></div></div><input type="submit" value="Send" class="submit" /></form>');
			//show / hide function
			$('div#contactable').toggle(function() {
				$('#overlay').css({display: 'block'});
				$(this).animate({"marginLeft": "-=5px"}, "fast"); 
				$('#contactForm').animate({"marginLeft": "-=0px"}, "fast");
				$(this).animate({"marginLeft": "+=387px"}, "slow"); 
				$('#contactForm').animate({"marginLeft": "+=390px"}, "slow"); 
			}, 
			function() {
				$('#contactForm').animate({"marginLeft": "-=390px"}, "slow");
				$(this).animate({"marginLeft": "-=387px"}, "slow").animate({"marginLeft": "+=5px"}, "fast"); 
				$('#overlay').css({display: 'none'});
			});
			
			//validate the form 
			$("#contactForm").validate({
				//set the rules for the fild names
				rules: {
					"data[Feedback][name]": {
						required: true,
						minlength: 2
					},
					"data[Feedback][email]": {
						required: true,
						email: true
					},
					"data[Feedback][show_id]": {
						required: true
					},
					"data[Feedback][comments]": {
						required: true
					}
				},
				//set messages to appear inline
					messages: {
						"data[Feedback][name]": "Name required",
						"data[Feedback][email]": {
							required: "Email required",
							email: "Please enter a valid email address"
						},
						"data[Feedback][show_id]": "Show required",
						"data[Feedback][comments]": "Comments required"
					},			

				submitHandler: function() {
					$('.holder').hide();
					$('#loading').show();
					$.post('/w_feedback',{name:$('#FeedbackName').val(), email:$('#FeedbackEmail').val(), show_id:$('#FeedbackShowId').val(), comment:$('#FeedbackComments').val()},
					function(data){
						$('#loading').css({display:'none'}); 
						if( data == 'success') {
							$('#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);
