$(document).ready(function() {
	// Create slider for highlights.
	var highlights = $("#highlights ul.items > li.highlight");
	var numHighlights = highlights.length;
	// Show slider buttons for case studies.
	if ( numHighlights > 1 ) {
		// Hide all case studies.
		$("#highlights ul.items > li.highlight").hide();
		// Show the first case study.
		$("#highlights ul.items > li.highlight:nth-child(1)").show();
		// Create list for the slider buttons.
		var sliderButtons = $("<ul/>").attr("id", "slider-buttons").appendTo("#highlights");
		// Create number buttons.
		for ( var i = 1; i <= numHighlights; i ++ ) {
			var button = $("<li/>").addClass("marker").appendTo(sliderButtons);
			var link = $("<a/>").attr({"href":"#null", "rel":i}).click(
				function()
				{
					slider.slide($(this).attr("rel"));
				}
			).appendTo(button);
			
			link.append("<span/>").text(i);
		}
		// Create the 'next' button.
		var button = $("<li/>").addClass("stepper next").appendTo(sliderButtons);
		var link = $("<a/>").attr({"href":"#null"}).click(
			function()
			{
				slider.slide(slider.nextCell);
			}
		).appendTo(button);
		
		link.append("<span/>").html("&gt;");
		// Create the 'previous' button.
		var button = $("<li/>").addClass("stepper previous").prependTo(sliderButtons);
		var link = $("<a/>").attr({"href":"#null"}).click(
			function()
			{
				slider.slide(slider.previousCell);
			}
		).appendTo(button);
		
		link.append("<span/>").html("&lt;");
		// Setup the slider.
		slider.init( numHighlights );
	}

});

var slider = {
	"effectDuration":500,
	"transitionDuration":8000,
	"currentCell":0,
	"nextCell":0,
	"previousCell":0,
	"timeout":0,
	"numCells":0,
	"inTransition":false,
	"init":function( numCells )
	{
		this.numCells = numCells;
		this.currentCell = 1;
		this.nextCell = this.currentCell + 1;
		this.previousCell = numCells;
		if ( this.numCells > 1 ) {
			this.timeout = setTimeout( "slider.slide(2)", this.transitionDuration );
			$("ul#slider-buttons li.marker:eq(0)").addClass("active");
		}
	},
	"slide":function( n )
	{
		if ( this.inTransition == true ) {
			return;
		}
		n = parseInt( n );
		if ( n == this.currentCell ) {
			return;
		}
		this.inTransition = true;
		clearTimeout( this.timeout );
		// Show the relevant slider button as active.
		$("ul#slider-buttons li.marker").removeClass("active");
		var markerNumber = n-1;
		$("ul#slider-buttons li.marker:eq("+markerNumber+")").addClass("active");
		// Fade the current cell out.
		$('#highlights ul.items > li.highlight:nth-child(' + this.currentCell + ')').fadeOut({duration:this.effectDuration});
		// Fade the numbered cell in.
		$('#highlights ul.items > li.highlight:nth-child(' + n + ')').fadeIn(	this.effectDuration, 
														function() {
															slider.inTransition = false;
														} 
													);
		this.currentCell = n;
		// Calculate what the next cell is goin to be.
		this.nextCell = n + 1;
		if ( this.nextCell > this.numCells ) {
			this.nextCell = 1;
		}
		this.previousCell = n - 1;
		if ( this.previousCell < 1 ) {
			this.previousCell = this.numCells;
		}
		this.timeout = setTimeout( "slider.slide("+this.nextCell+")", this.transitionDuration );
	}
}
