/* ================================================== */
/* FUNCTIONS
/* ================================================== */
var tk = function()
{
	noFormErrors = function()
	{
		if($.trim($('#name').val()) == '' || $.trim($('#email').val()) == '' || $.trim($('#subject').val()) == '' || $.trim($('#message').val()) == '')
		{
			return false;
		}
		else
		{
			return true;
		}
	};
	
	hideFormShowThanks = function()
	{
		$('#name, #email, #subject, #message').val('');
		$('#form-error-message').hide();
		$('#contact-form').slideUp(500);
		setTimeout(function(){ $('#contact-thanks').slideDown(200); }, 600);
		setTimeout(function(){ $('#contact-unless').slideDown(200); }, 2600);
		setTimeout(function(){ $('#contact-unless2').slideDown(200); }, 7600);
	};
	
	return {
		hover: function(elem)
		{
			$(elem).toggleClass('hover');
			$(elem).parents('.site-thumb').siblings('h3').toggleClass('hover');
		},
		
		sendContactForm: function()
		{
			if(noFormErrors() === true)
			{
				var form_data = 
				{
					name: $('#name').val(),
					email: $('#email').val(),
					subject: $('#subject').val(),
					message: $('#message').val()
				};
				
				$.ajax(
				{
					url: 'contact-ajax.php',
					type: 'POST',
					data: form_data,
					success: function(data, textStatus, jqXHR)
					{
						hideFormShowThanks();
					},
					error: function(jqXHR, textStatus, errorThrown) {}
				});
			}
			else
			{
				$('#form-error-message').slideDown(200);
			}
		}
	};
}();



/* ================================================== */
/* DOCUMENT READY
/* ================================================== */
$(document).ready(function()
{
	// external links
	$('a[rel="external"]').click(function(e)
	{
		e.preventDefault();
		window.open($(this).attr('href'));
	});
	
	// portfolio thumbs hover effect
	$('#portfolio .site-thumb img').hover(function()
	{
		tk.hover(this);
	});
	
	// contact form ajax
	$('#contact-form').submit(function(e)
	{
		e.preventDefault();
		tk.sendContactForm();
	});
});



/* ================================================== */
/* WINDOW.ONLOAD
/* ================================================== */
window.onload = function()
{
	// scrolls iPhone viewport to just below location bar
	setTimeout(function(){ window.scrollTo(0,0); }, 0);
}
