$(document).ready(function() {
	// Append placeholders for screen overlay and contest dialog
	$('body').append('<div class="overlay" style="display: none;"></div>');
	$('body').append('<div class="contestDialog" style="display: none;"></div>');
	$('div.contestDialog').load('contest-dialog.html?' + Math.random(), function() {
		$('div.overlay, div.contestDialog div.close a').bind('click', function() {
			$('div.overlay').fadeOut(250);
			$('div.contestDialog').fadeOut(250);
			return false;
		});
		$('div.contestDialog form').bind('submit', function() {
			// Validate the form
			if (!objValidateFocus(document.contestEntry.firstName, "notEmpty")) {
				alert("Please enter your first name.");
				return false;
			}
			if (!objValidateFocus(document.contestEntry.lastName, "notEmpty")) {
				alert("Please enter your last name.");
				return false;
			}
			if (!objValidateFocus(document.contestEntry.email, "notEmpty")) {
				alert("Please enter your email address.");
				return (false);
			} else {
				if (!objValidateFocus(document.contestEntry.email, "emailStd")) {
					alert("The email address entered does not appear to be valid.  Please check the address and try again.");
					return (false);
				}
			}			
			if (!objValidateFocus(document.contestEntry.zip, "notEmpty")) {
				alert("Please enter your zip or postal code.");
				return false;
			}
			// Process the form
			$('div.contestDialog form .submit').hide();
			$('div.contestDialog form .loading').show();
			$.post($(this).attr('action'), $(this).serialize(), function(data) {
				$('div.contestDialog form .loading').hide();
				$('div.contestDialog .formContainer').slideUp();
				$('div.contestDialog .messageContainer').slideDown();
			});
			return false;
		});
	});
	$('#header .cta a').bind('click', function() {
		$('div.overlay').css({ 'width': $(document).width(), 'height': $(document).height() });
		$('div.overlay').show().css('opacity', 0).fadeTo(250, .65, function() {
			$('div.contestDialog').center({ 'vertical': false }).fadeIn(250);
		});
		return false;
	});
});