$(document).ready(function() {
	var cells = $("ul.cells li");
	numCells = cells.length;
	cells.css({zIndex:"1", display:"none"});
	$("ul.cells li:nth-child(1)").fadeIn({duration:800});
	// Add swapCell method to tabs.
	$("ul.tabs li a").attr('href', "#null").click(
		function ()
		{
			swapCell( $(this).attr('rel') );
		}
	);
	// 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.
	$("ul.cells li:nth-child("+currentCell+")").fadeOut({duration:effectDuration});
	$("ul.tabs li:nth-child("+currentCell+")").removeClass('active');
	// Fade the numbered cell in.
	$("ul.cells li:nth-child("+n+")").fadeIn({duration:effectDuration});
	$("ul.tabs li:nth-child("+n+")").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 );
}
