$(document).ready(function() {
	contact.initEventHandlers();
});
var contact = {
	initEventHandlers	: function() {
		/* clicking the submit form */
		$('#send').bind('click',function(event){
			$('#loader').show();
			setTimeout('contact.ContactFormSubmit()',500);
		});
		// prefill values
		$('.inplaceError').example(function() {
       return $(this).attr('title');
    });
		$('#message').example('Comments - 300 Characters Max -');

		/* remove messages when user wants to correct (focus on the input) */
		$('.inplaceError',$('#ContactForm')).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 
		$('#ContactForm input,#ContactForm 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		: 'POST',
			   url		: 'php/contact.php?ts='+new Date().getTime(),
			   dataType	: 'json',
			   data		: $('#ContactForm').serialize(),
			   success	: function(data,textStatus){
							  //hide the ajax loader
							  $('#loader').hide();
							  if(data.result == '1'){
								  //contact form disappears
								  $('#contact').stop().animate({'opacity':'0'}, "fast");
							    $('#contact').hide();
								  //reset all form fields
								  $('#ContactForm')[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'});
								  });
								  //success appears but still opacity is zero
							    $('.success').show(); 
									//opacity 1
								  $('.success').stop().animate({'opacity':'1'}, "slow" );
							      //show success message I include it in the graphic
								  //$('#success_message').empty().html('Message sent');
							  }
							  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('<span>'+data.errors[i].value+'</span>').fadeIn();
								  }
							  }						  
						  },
			   error	: function(data,textStatus){}
		});
	}  
};
