var currentPage = location.href.indexOf('.php') == -1 ? 'index.php' : location.href.substring(location.href.lastIndexOf('/'));
var loading = $('<div style="text-align:center;margin-top:88px;"><img src="images/loading.gif" alt="Loading..." /></div>');
var modal = null;
$(document).ready(function(){
	$('#nav-home').click(SwapContents);
	$('#nav-update').click(SwapContents);
	
	var headerEmail = $('#header-email');
	headerEmail.css({ background: 'url(images/head_email.gif) no-repeat top left' });
	headerEmail.focus(function(){
		$(this).css({ background: '#fff' });
	});
	headerEmail.blur(function(){
		var sender = $(this);
		if(sender.val() == '')
			sender.css({ background: 'url(images/head_email.gif) no-repeat top left' });
	});
	
	modal = $('#modal');
	modal.css({ cursor: 'pointer' }).click(function(){
		modal.fadeOut();
	});
	modal.find('.body').css({ cursor: 'default' }).click(function(){
		return false;
	});
	modal.find('input[type="button"]').click(function(){
		var error = 0;
		var temp = null;
		
		temp = $('#modal-name');
		if(!temp.val().match(/^[\w\s]+$/))
		{
			if(!temp.attr('error'))
			{
				temp.addClass('error');
				$('<div class="error"></div>').html('Please enter a name.').css({ display: 'none' }).insertAfter(temp).slideDown('slow');
				temp.attr('error', true);
			}
			++error;
		}
		else
			temp.removeClass('error').removeAttr('error').siblings().slideUp('slow', function(){$(this).remove();});
		
		temp = $('#modal-email');
		if(!temp.val().match(/^\w+([-._]\w+)*@\w+([-._]\w+)*\.\w{2,6}$/))
		{
			if(!temp.attr('error'))
			{
				temp.addClass('error');
				$('<div class="error"></div>').html('Please enter a valid email address.<br />(ex. address@example.com)').css({ display: 'none' }).insertAfter(temp).slideDown('slow');
				temp.attr('error', true);
				
			}
			++error;
		}
		else
			temp.removeClass('error').removeAttr('error').siblings().slideUp('slow', function(){$(this).remove();});
		
		temp = $('#modal-phone');
		if(!temp.val().match(/^\d{3}\-?\d{3}\-?\d{4}$/))
		{
			if(!temp.attr('error'))
			{
				temp.addClass('error');
				$('<div class="error"></div>').html('Please enter a valid phone number.<br />(ex. 555-555-5555)').css({ display: 'none' }).insertAfter(temp).slideDown('slow');
				temp.attr('error', true);
				
			}
			++error;
		}
		else
			temp.removeClass('error').removeAttr('error').siblings().slideUp('slow', function(){$(this).remove();});
		
		temp = $('#modal-address');
		if(!temp.val().match(/^.+$/))
		{
			if(!temp.attr('error'))
			{
				temp.addClass('error');
				$('<div class="error"></div>').html('Please enter an address.').css({ display: 'none' }).insertAfter(temp).slideDown('slow');
				temp.attr('error', true);
				
			}
			++error;
		}
		else
			temp.removeClass('error').removeAttr('error').siblings().slideUp('slow', function(){$(this).remove();});
		
		temp = $('#modal-city');
		if(!temp.val().match(/^.+$/))
		{
			if(!temp.attr('error'))
			{
				temp.addClass('error');
				$('<div class="error"></div>').html('Please enter a city.').css({ display: 'none' }).insertAfter(temp).slideDown('slow');
				temp.attr('error', true);
				
			}
			++error;
		}
		else
			temp.removeClass('error').removeAttr('error').siblings().slideUp('slow', function(){$(this).remove();});
		
		temp = $('#modal-state');
		if(!temp.val().match(/^[\w\s]{2,}$/))
		{
			if(!temp.attr('error'))
			{
				temp.addClass('error');
				$('<div class="error"></div>').html('Please enter a valid state or state abbreviation.<br/>(ex. New York or NY)').css({ display: 'none' }).insertAfter(temp).slideDown('slow');
				temp.attr('error', true);
				
			}
			++error;
		}
		else
			temp.removeClass('error').removeAttr('error').siblings().slideUp('slow', function(){$(this).remove();});
		
		temp = $('#modal-zip');
		if(!temp.val().match(/^\d{5}$/))
		{
			if(!temp.attr('error'))
			{
				temp.addClass('error');
				$('<div class="error"></div>').html('Please enter a valid zip code.<br />(ex. 12345)').css({ display: 'none' }).insertAfter(temp).slideDown('slow');
				temp.attr('error', true);
				
			}
			++error;
		}
		else
			temp.removeClass('error').removeAttr('error').siblings().slideUp('slow', function(){$(this).remove();});
		
		if(error == 0)
		{
			$('#modal-form').submit();
			modal.fadeOut();
		}
		return false;
	});
	var headerForm = $('#header-form');
	headerForm.submit(function(){
		var email = $('#header-email');
		if(email.val().match(/^\w+([-._]\w+)*@\w+([-._]\w+)*\.\w{2,6}$/))
		{
			$.post('signup.php', { email: email.val() });
			$('#modal-email').val(email.val());
			$('#header-row').replaceWith($('<tr><td colspan="2"><img src="images/email_thankyou.gif" width="262" height="21" title="Thank you for signing up!" alt="Thank you for signing up!" /></td></tr>'));
			$('#footer-container').replaceWith($('<tr><td colspan="2"><img src="images/email_thankyou.gif" width="262" height="21" title="Thank you for signing up!" alt="Thank you for signing up!" /></td></tr>'));
			modal.fadeIn();
		}
		else
			email.addClass('error');
		return false;
	});
});
function SwapContents()
{
	var sender = $(this);
	if(currentPage != sender.attr('href'))
	{
		var placeholder = $('#placeholder');
		currentPage = sender.attr('href');
		placeholder.empty();
		placeholder.append(loading);
		placeholder.load(sender.attr('href') + '?async');
	}
	return false;
}
