/*
 * contactable 1.2 - 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-09-24 $
 *
 * Very modified by Malte Lenz
 *
 */
 
//extend the plugin
(function($){
	//define the new for the plugin and how to call it
	$.fn.contactable = function(options) {
		//set default options
		var defaults = {
			message : 'Message',
			recievedMsg : 'Thank you for your message.',
			disclaimer: 'Feel free to give any feedback. If you would like an answer, please write your email in the message.',
			helptext : 'No help available.',
			userid : ''
		};

		//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">\n\
				</div>\n\
				<form id="contactForm" method="" action="">\n\
					<a id="contactableHelp" href="javascript:void(null)">Help</a>\n\
					<a id="contactableContact" class="contactableContact" href="javascript:void(null)">Contact Us</a>\n\
					<div id="Help">'+defaults.helptext+'</div>\n\
					<div id="loading"></div>\n\
					<div id="callback"></div>\n\
					<div class="holder">\n\
						<input id="user" type="hidden" value="'+defaults.userid+'"/>\n\
						<p>\n\
							<textarea id="comment" name="comment" class="comment" rows="10" cols="30" ></textarea>\n\
						</p>\n\
						<p>\n\
							<input class="submit" type="submit" value="Send"/>\n\
						</p>\n\
						<p>'+defaults.disclaimer+'</p>\n\
					</div>\n\
				</form>');
			//show / hide function
			$("#contactableHelp").click(function(){
				$("#Help").slideDown();
				$(".holder").slideUp();
				$('#callback').hide();
			})
			$(".contactableContact").click(function(){
				$("#Help").slideUp();
				$(".holder").slideDown();
				$('#callback').hide();
			})
			
			$('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: {
					comment: {
						required: true
					}
				},
				//set messages to appear inline
				messages: {
					name: "",
					email: "",
					comment: ""
				},
				submitHandler: function() {
					$('.holder').hide();
					$('#loading').show();
					$.post(contact_url,{message:$('#comment').val(),user:$('#user').val()},
					function(data){
						$('#loading').css({display:'none'});
						$('#callback').show().append(defaults.recievedMsg);
					});		
				}
			});
		});
	};
	//end the plugin call 
})(jQuery);

