$(document).ready(function() {
	// Do the home page slideshow.
	$('#lightbox #lb-bg').css({height:$("body").height()});
	// Build the slider buttons.
	var sponsors = $("#sponsors ul li");
	numSponsors = sponsors.length;
	sponsors.css({zIndex:"1", display:"none"});
	$("#sponsors ul li:nth-child(1)").fadeIn({duration:800});
	// Swap sponsors after a few seconds
	if ( numSponsors > 1 ) {
		sponsorsTimeout = setTimeout( "swapSponsor("+2+")", spTransitionDuration );
	}
	// General effects.
	$("#header #link-home, dl.form dd.buttons button").mouseover(
		function ()
		{
			$(this).animate({opacity:0.6}, 200);
		}
	).mouseleave(
		function ()
		{
			$(this).animate({opacity:1}, 200);
		}
	);
	// Subscribe form.
	$("#col-subscribe input").focus(
		function ()
		{
			subscribeHasFocus = true;
			if ( $(this).val() == 'Enter email address ...' ) {
				$(this).val('');
			}
		}
	).blur(
		function ()
		{
			subscribeHasFocus = false;
			if ( $(this).val() == '' ) {
				$(this).val('Enter email address ...');
			}
			$(this).animate({opacity:0.6}, 100);
		}
	).mouseover(
		function ()
		{
			$(this).animate({opacity:1}, 100);
		}
	).mouseout(
		function ()
		{
			if (!subscribeHasFocus) {
				$(this).animate({opacity:0.6}, 100);
			}
		}
	);
	// Search form.
	$("#search input, input.searchField").focus(
		function ()
		{
			searchHasFocus = true;
			if ( $(this).val() == 'Type to search ...' ) {
				$(this).val('');
			}
		}
	).blur(
		function ()
		{
			searchHasFocus = false;
			if ( $(this).val() == '' ) {
				$(this).val('Type to search ...');
			}
			$(this).animate({opacity:0.6}, 100);
		}
	).mouseover(
		function ()
		{
			$(this).animate({opacity:1}, 100);
		}
	).mouseout(
		function ()
		{
			if (!searchHasFocus) {
				$(this).animate({opacity:0.6}, 100);
			}
		}
	);
	
});

var searchHasFocus = false;
var subscribeHasFocus = false;

var spEffectDuration = 700;
var spTransitionDuration = 5000;
var spCurrentCell = 1;
var spNextCell = spCurrentCell + 1;
var sponsorsTimeout = 0;
var numSponsors = 0;
// Funtion to transition between the sponsors.
function swapSponsor(n)
{
	n = new Number( n );
	clearTimeout( sponsorsTimeout );
	// Fade the current cell out.
	$("#sponsors ul li:nth-child("+spCurrentCell+")").fadeOut({duration:spEffectDuration});
	// Fade the numbered cell in.
	$("#sponsors ul li:nth-child("+n+")").fadeIn({duration:spEffectDuration});
	spCurrentCell = n;
	// Calculate what the next cell is goin to be.
	spNextCell = n + 1;
	if ( spNextCell > numSponsors ) {
		spNextCell = 1;
	}
	sponsorsTimeout = setTimeout( "swapSponsor("+spNextCell+")", spTransitionDuration );
}

function validateSubscribe()
{
	if ( ($("#email").val().match(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/) == null) ) {
		// Show a lightbox error message.
		showLightbox( 'Please provide a valid email address.' );
		return false;
	}
	// Email must be valid.
	return true;
}

function showLightbox( message )
{
	$("#lightbox").fadeIn({duration:250});
	$("#lightbox #lb-box #lb-content").html("<p class=\"message\">"+message+"</p>");
}

function closeLightbox()
{
	$("#lightbox").fadeOut({duration:250});
}
