$(document).ready(function() {
	// Do the home page slideshow.
	// Build the slider buttons.
	var cells = $("#main_banner ul.banners li");
	numCells = cells.length;
	var sliderButtons = $("<ul class=\"slider-buttons\"></ul>");
	if ( numCells > 1 ) {
		for ( i = 1; i <= cells.length; i ++ ) {
			var newButton = $("<li></li>").html("<a href=\"#null\"><span>"+i+"</span></a>");
			sliderButtons.append(newButton);
		}
	}
	$("#main_banner").append(sliderButtons);
	cells.css({zIndex:"1", display:"none"});
	$("#main_banner ul.banners li.banner:nth-child(1)").fadeIn({duration:800});
	$(".slider-buttons li:nth-child(1) a").addClass('active');
	// Add swapCell method to slider buttons.
	$(".slider-buttons li a").attr('href', "#null").click(
		function ()
		{
			swapCell( $(this).find("span").text() );
		}
	);
	// Swap cells after a few seconds
	if ( numCells > 1 ) {
		timeout = setTimeout( "swapCell("+2+")", transitionDuration );
	}
	
});

var effectDuration = 800;
var transitionDuration = 8000;
var currentCell = 1;
var nextCell = currentCell + 1;
var timeout = 0;
var numCells = 0;
// Funtion to transition between the cells.
function swapCell(n)
{
	n = new Number( n );
	clearTimeout( timeout );
	// Fade the current cell out.
	$("#main_banner ul.banners li.banner:nth-child("+currentCell+")").fadeOut({duration:effectDuration});
	$(".slider-buttons li:nth-child("+currentCell+") a").removeClass('active');
	// Fade the numbered cell in.
	$("#main_banner ul.banners li.banner:nth-child("+n+")").fadeIn({duration:effectDuration});
	$(".slider-buttons li:nth-child("+n+") a").addClass('active');
	currentCell = n;
	// Calculate what the next cell is goin to be.
	nextCell = n + 1;
	if ( nextCell > numCells ) {
		nextCell = 1;
	}
	timeout = setTimeout( "swapCell("+nextCell+")", transitionDuration );
}
