/*
 * 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 $
 *
 */
 
//extend the plugin
(function($){

	//define the new for the plugin ans how to call it	
	$.fn.contactable = function(options) {
		//set default options  
		var defaults = {
			recipient : 'test@test.co.uk',
            name:'Ime',
            email:'Email adresa',
            message:'Poruka',
            subject:'Feedback sa Astma.rs',
            recievedMsg:'Hvala Vam na poruci. I dalje ćemo se truditi da radimo na našem osnovnom cilju - edukacija i podrška svima koji se součavaju sa astmom. \r \n  \r \n        Pozdrav od Maje i Vekija', 
            notRecievedMsg:'Izvinite! Vaša poruka nije poslana. Molim vas probajte kasnije ili pošaljite mejl na veroljub@astma.rs',
            disclaimer:'Pošaljite na poruku. Želeli bismo da čujemo vaše mišljenje kako da unapredimo sajt. Slobodno ostavite i kritiku i pohvalu! \r \n  Pozdrav od Maje i Vekija.'
		};

		//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="" 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">Ime <span class="red"> * </span></label><br /><input id="name" class="contact" name="name" /></p><p><label for="email">Email adresa <span class="red"> * </span></label><br /><input id="email" class="contact" name="email" /></p><p><label for="commentFeedback">Vaša poruka <span class="red"> * </span></label><br /><textarea id="commentFeedback" name="commentFeedback" class="comment" rows="4" cols="30" ></textarea></p><p><input class="submit" type="submit" value="Pošalji"/></p><p>'+defaults.disclaimer+'</p></div></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: {
					name: {
						required: true,
						minlength: 2
					},
					email: {
						required: true,
						email: true
					},
					commentFeedback: {
						required: true
					}
				},
				//set messages to appear inline
				messages: {
					name: "",
					email: "",
					commentFeedback: ""
				},
				submitHandler: function() {
					$('.holder').hide();
					$('#loading').show();
					$.get('http://www.astma.rs/wp-content/themes/wp-polaroid/mail.php',{recipient:$('#recipient').val(), subject:$('#subject').val(), name:$('#name').val(), email:$('#email').val(), commentFeedback:$('#commentFeedback').val()},
					function(data){
						$('#loading').css({display:'none'}); 
						if( data == 'success') {
							$('#callback').show().append(defaults.recievedMsg);
						} else {
							$('#callback').show().append(defaults.notRecievedMsg);
						}
					});		
				}
			});
		});
	};
	//end the plugin call 
})(jQuery);

