$(document).ready(function() {
	dillo_amico.initEventHandlers();
});

var dillo_amico = {
	initEventHandlers    : function() {
		/* clicking the submit form */
		$('#send').bind('click',function(event){
			$('#loader').show();
			setTimeout('dillo_amico.ContactFormSubmit()',500);
		});
		/* remove messages when user wants to correct (focus on the input) */
		$('.input',$('#form_dillo_amico')).bind('focus',function(){
			var $this        = $(this);
			var $error_elem  = $this.next();
			if($error_elem.length)
				$error_elem.fadeOut(function(){$(this).empty()});
			$('#success_message').empty();    
		});
		/* user presses enter - submits form */
		$('#form_dillo_amico input,#form_dillo_amico textarea').keypress(function (e) {
			if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {  
				$("#send").click();
				return false;  
			} 
			else  
				return true;  
		});
	},
	ContactFormSubmit    : function() {
		
		   $.ajax({
							   type        : 'GET',
							   url         : 'index.php?mod=dillo_amico',
							   dataType    : 'json',
							   data        : $('#form_dillo_amico').serialize(),
							   success     : function(data,textStatus){
											  
											  $("#loader").hide();  //hide the ajax loader
											  
											  if(data.result == '1'){
												  //show success message
												  $('#success_message').empty().html(data.data).show();
												  //reset all form fields
												  $('#form_dillo_amico')[0].reset();    
												  //envelope animation
												  $('#envelope').stop().show().animate({'marginTop':'-175px','marginLeft':'-246px','width':'492px','height':'350px','opacity':'0'},function(){
													  $(this).css({'width':'246px','height':'175px','margin-left':'-123px','margin-top':'-88px','opacity':'1','display':'none'});
												  });
												  
											  }
											  else if(data.result == '-1'){
												  for(var i=0; i < data.errors.length; ++i ){
													  if(data.errors[i].value!='')
														  $("#"+data.errors[i].name).next().html(data.errors[i].value).fadeIn();
												  }
											  }                                 
										  },
								 error    : function(data,textStatus){}               
					
						});
		
		
	}  
};										 
								  
								
