/*
 * 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 : 'A contactable message',
			recievedMsg : 'Thankyou',
			notRecievedMsg : 'Sorry but your message could not be sent, try again later',
			disclaimer: '',
			hideOnSubmit: false
		};

		//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) {
			var lang = null;
			//construct the form
			var lang = document.cookie.substr(9,2); // language from cookie
			
			if(lang != "en" && lang != "cy") { lang = "en"; } // if not selected
			
			if(lang=="en")
			{
			 $(this).html('<div id="contactable">Nifty Little Form</div><form id="contactForm" name="contactForm" method="" action=""><div id="loading"></div><div id="callback"></div><div class="holder"><p><label for="name">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">Your Message <span class="red"> * </span></label><br /><textarea id="comment" name="comment" class="comment" rows="4" cols="30" ></textarea></p><p><input class="submit" type="submit" value="Send"/></p><p class="disclaimer">'+defaults.disclaimer+'</p></div></form>');
			}
			if(lang=="cy")
      {
       $(this).html('<div id="contactable">Ffurflen Bach Cyfareddol</div><form id="contactForm" name="contactForm" method="" action=""><div id="loading"></div><div id="callback"></div><div class="holder"><p><label for="name">Enw <span class="red"> * </span></label><br /><input id="name" class="contact" name="name" /></p><p><label for="email">E-Bost <span class="red"> * </span></label><br /><input id="email" class="contact" name="email" /></p><p><label for="comment">Eich Neges <span class="red"> * </span></label><br /><textarea id="comment" name="comment" class="comment" rows="4" cols="30" ></textarea></p><p><input class="submit" type="submit" value="Anfon"/></p><p class="disclaimer">'+defaults.disclaimer+'</p></div></form>');
      }
			
			//show / hide function
			$('div#contactable').toggle(function() {
				$('#overlay').css({display: 'block'});
				$(this).animate({"marginTop": "-=5px"}, "fast"); 
				$('#contactForm').animate({"marginTop": "-=0px"}, "fast");
				$(this).animate({"marginTop": "+=220px"}, "slow"); 
				$('#contactForm').animate({"marginTop": "+=220px"}, "slow"); 
			}, 
			function() {
				$('#contactForm').animate({"marginTop": "-=220px"}, "slow");
				$(this).animate({"marginTop": "-=220px"}, "slow").animate({"marginTop": "+=5px"}, "fast"); 
				$('#overlay').css({display: 'none'});
			});
			
			//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: "",
						email: "",
						comment: ""
					},			

				submitHandler: function() {
					$('.holder').hide();
					$('#loading').show();
					$.post('http://www.stormfm.com/code/js/contactable/mail.php',{subject:defaults.subject, name:$('#name').val(), email:$('#email').val(), comment:$('#comment').val()},
					function(data){
						$('#loading').css({display:'none'}); 
						if( data == 'success') { 
							$('#callback').empty();
							$('#callback').show().append(defaults.recievedMsg);
//							setTimeout("this.contactForm.reset(); $('#callback').hide(); $('.holder').show();",3000)
							setTimeout("$('#contactForm')[0].reset(); $('#callback').hide(); $('.holder').show();",3000)
							if(defaults.hideOnSubmit == true) {
								//hide the tab after successful submition if requested
								
							}
						} else {
							$('#callback').show().append(defaults.notRecievedMsg);
						}
					});		
				}
			});
		});
	};
})(jQuery);


