$(document).ready(function() {
	obj.init();
	$('#messageform')[0].reset();
});
var obj = {
		init: function () {
			$('#send').click(function(e){
				$('#error').empty();
				$('#loader').show();
				setTimeout('obj.formsubmit()',500);
			});
			$('.inplaceError',$('#messageform')).focus(function(){
				$('#error').empty();
			});
			$('#nav_hm').click(function(e){
				$('html, body').stop().animate({scrollTop: $("#home").offset().top}, 1000);
				e.preventDefault();
			});
			$('#nav_wd').click(function(e){
				$('html, body').stop().animate({scrollTop: $("#wd").offset().top}, 1000);
				e.preventDefault();
			});
			$('#nav_gd').click(function(e){
				$('html, body').stop().animate({scrollTop: $("#gd").offset().top}, 1000);
				e.preventDefault();
			});
			$('#nav_id').click(function(e){
				$('html, body').stop().animate({scrollTop: $("#id").offset().top}, 1000);
				e.preventDefault();
			});
			$('#nav_wd1').click(function(e){
				$('html, body').stop().animate({scrollTop: $("#wd").offset().top}, 1000);
				e.preventDefault();
			});
			$('#nav_gd1').click(function(e){
				$('html, body').stop().animate({scrollTop: $("#gd").offset().top}, 1000);
				e.preventDefault();
			});
			$('#nav_id1').click(function(e){
				$('html, body').stop().animate({scrollTop: $("#id").offset().top}, 1000);
				e.preventDefault();
			});
		},
		formsubmit: function () {
			var url = 'website/ajax.php?ts='+new Date().getTime();
			$.ajax({
				   type		: 'POST',
				   url		: url,
				   timeout 	: 30000,
				   dataType	: 'json',
				   data		: $('#messageform').serialize(),
				   success	: obj.onsubmitsuccess,
				   error	: obj.onsubmiterror
			});
		},
		onsubmitsuccess : function(data,textStatus){	
			if(data.result == '1'){
				$('#mail').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'});
				});
				$('#loader').hide();
				$('#messageform')[0].reset();
			}
			else if(data.result == '-1'){
				$('#error').empty().html('Oops, something went wrong. Please check your input.');
				$('#loader').hide();
			}
			else if(data.result == '-2'){
				$('#error').empty().html('Oops, something went wrong. Please check your input.');
				$('#loader').hide();
			}
			else{
				$('#loader').hide();
			}
		},
		onsubmiterror :  function(XMLHttpRequest, textStatus, errorThrown){
			if(textStatus=='error'){
				$('#loader').hide();
			}
			else{//TODO: other possible errors
				$('#loader').hide();
			}
		}
};
