//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: 'Ваше имя',
			email: 'Email',
			message : 'Сообщение',
			subject : 'Сообщение с сайта ARTreactor.ru',
			recievedMsg : 'Спасибо!<br /> Мы свяжемся с вами в ближайшее время',
			notRecievedMsg : 'Произошла ошибка!<br /> Ваше сообщение не отправлено, попробуйте еще раз',
			disclaimer: 'Поля, отмеченные <span class="red"> * </span>, обязательны для заполнения',
			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('<form id="contactForm" method="" action=""><div id="loading"></div><div id="callback"></div><div class="holder"><p><label for="name">'+defaults.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">'+defaults.message+'<span class="red"> * </span></label><br /><textarea id="comment" name="comment" class="comment" rows="6" cols="30" ></textarea></p><p><input class="submit" type="submit" value="Отправить"/></p></div></form>');
			//show / hide function
			$('div#contactable').toggle(function() {
				$('#overlay').css({display: 'block'});
			}, 
			function() {
				$('#overlay').css({display: 'block'});
			});
			
			//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('mail.php',{subject:defaults.subject, name:$('#name').val(), email:$('#email').val(), comment:$('#comment').val()},
					function(data){
						$('#loading').css({display:'none'}); 
						if( data == 'success') {
							setTimeout(function(){
								$('#callback').show().append(defaults.recievedMsg);
												},1000);	
	
							if(defaults.hideOnSubmit == true) {


							}
						} else {
							$('#callback').show().append(defaults.notRecievedMsg);
						}
					});		
				}
			});
		});
	};
})(jQuery);


