function animateBackground()
{
	$("#backgroundScroller").animate({backgroundPositionX: "1000px"}, 10000, "linear", function() {
		$("#backgroundScroller").css({"backgroundPositionX":"0"});
		animateBackground();
	});
}

function showNavArrow(side)
{
	var ele = $('#turner'+side+' > div.arrow');
	if (side == "Left")
	{
		ele.css({"left":"-20px"}).show();
		ele.animate({left:"-100px"}, 300);
	} else {
		ele.css({"right":"-20px"}).show();
		ele.animate({right:"-100px"}, 300);
	}
}

function hideNavArrow(side)
{
	$('#turner'+side+' > div.arrow').fadeOut();
}

function resetForm()
{
	$('.errRequiredField').removeClass('errRequiredField');
	
	$('#clientName').val('');
	$('#clientEmail').val('');
	$('#clientMsg').val('');
	
	$('#successForm').hide();
	$('#failureForm').hide();
	$('#loadingForm').hide();
	$('#contactForm').show();
}

$(document).ready(function()
{
	$("#backgroundScroller").css({"backgroundPositionX": parseInt(Math.random()*1000)+"px"});
	animateBackground();
	
	//Only show certain nav items when javascript is enabled
	$('#indexSelect').show();
	$('.turner').show();
	
	//Handle mouseover navigation
	$('#turnerLeft').hover(function() { showNavArrow("Left"); },
	function() { hideNavArrow("Left"); });
	
	$('#turnerRight').hover(function() { showNavArrow("Right"); },
	function() { hideNavArrow("Right");	});

	$('#turnerLeft').click(function() {
		if (prevPageHref != null) {	document.location.href = prevPageHref+'.html'; }
	});
	
	$('#turnerRight').click(function() {
		if (nextPageHref != null) {	document.location.href = nextPageHref+'.html'; }
	});

	
	//Handle left navigation (navbar)
	$('img#leftNav').hover(function() {
		$('img#leftNav').attr("src", "liddiard_img/left_arrow_over.png");
		showNavArrow("Left");
	}, function() {
		$('img#leftNav').attr("src", "liddiard_img/left_arrow.png");
		hideNavArrow("Left");
	});
	
	//Handle right navigation (navbar)
	$('img#rightNav').hover(function() {
		$('img#rightNav').attr("src", "liddiard_img/right_arrow_over.png");
		showNavArrow("Right");
	}, function() {
		$('img#rightNav').attr("src", "liddiard_img/right_arrow.png");
		hideNavArrow("Right");
	});
	
	//Handle index selector animation
	$('img#indexSelect').hover(function() {
		$('img#indexSelect').attr("src", "liddiard_img/index_select_over.png");
	}, function() {
		$('img#indexSelect').attr("src", "liddiard_img/index_select.png");
	});
	
	//Show index selector
	$('#indexSelect').click(function() {
		$('#selectorFade').show();
		$('#selectorContainer').show();
	});
	
	$('img#selectorClose').hover(function() {
		$('img#selectorClose').attr("src", "liddiard_img/index_selector_close_over.png");
	}, function() {
		$('img#selectorClose').attr("src", "liddiard_img/index_selector_close.png");
	});
	
	$('img#selectorClose').click(function() {
		$('#selectorFade').hide();
		$('#selectorContainer').hide();
	});
	
	$('#sendAnotherButton').click(resetForm);
	$('#tryAgainButton').click(resetForm);
	
	$('input#formSubmitButton').click(function() {
		
		//Validate form
		var formValidates = true;
		$('.errRequiredField').removeClass('errRequiredField');
		
		if ($('#clientName').val() == '')
		{
			$('#clientNameLabel').addClass('errRequiredField');
			formValidates = false;
		}
		
		if ($('#clientEmail').val() == '')
		{
			$('#clientEmailLabel').addClass('errRequiredField');
			formValidates = false;
		}
		
		if ($('#clientMsg').val() == '')
		{
			$('#clientMsgLabel').addClass('errRequiredField');
			formValidates = false;
		}
		
		if (formValidates)
		{
			$('#contactForm').hide();
			$('#loadingForm').show();
			
			var dataToSend = {
				clientName:$('#clientName').val(),
				clientEmail:$('#clientEmail').val(),
				clientMsg:$('#clientMsg').val()
			}
			
			$.post("inc/mailer.php",
				dataToSend, 
				function(data)
				{
					$('#loadingForm').hide();
					
					if (data == "success")
					{
						$('#successForm').show();
					} else {
						$('#failureForm').show();
					}
				}
			);
		}
	});

});