$(function()
{
	// Pre-load background images.
	if (document.images)
	{
		var images = [];
		$('#slide li').each(function()
		{
			var theme = $(this).attr('id');
			images.push(getBackgroundUrl(theme));
		});
		
		$.preload(images);
	}
	
	if (!$.browser['msie'])
	{
		// Mouse-over effects.
		var images = $('#slide-navigation a, #testimonial-navigation a');
		images.fadeTo(1, 0.5);
		images.hover(function()
		{
			$(this).fadeTo('fast', 1);
		}, function()
		{
			$(this).fadeTo('fast', 0.5);
		});
	}
	
	// Add testimonial slide-window.
	var testimonialWindow = $('#testimonials ul');
	testimonialWindow.find('li').css('display', 'block');	
	testimonialWindow.slideWindow({ vertical: false, slideSpeed: slideSpeed });
	$('#testimonial-navigation .slide-left').click(function()
	{
		testimonialWindow.slidePrevious();
		return false;
	});
	$('#testimonial-navigation .slide-right').click(function()
	{
		testimonialWindow.slideNext();
		return false;
	});
	
	// Get the appropriate start item according to the theme specified in the cookie.
	var boardWindow = $('#slide ul');
	var infoWindow = $('#slide-info ul');
	boardWindow.find('li').css('display', 'block');
	infoWindow.find('li').css('display', 'block'); // For some reason, "show" doesn't work here.
	var theme = $.cookie('theme');
	theme = theme ? theme : defaultTheme;
	var startIndex = boardWindow.find('li').index(boardWindow.find('li#' + theme));
	
	// Configure image board navigation lights.
	var lights = $('#slide-navigation ul li');
	lights.css('opacity', 0.3);
	lights.filter('.' + theme).css('opacity', 1);
	
	// Add theme description and image board slide-windows.
	boardWindow.slideWindow({ vertical: true, slideSpeed: slideSpeed, startItem: startIndex });
	infoWindow.slideWindow({ vertical: false, slideSpeed: slideSpeed, startItem: startIndex });
	var animationCallback = function()
	{
		var theme = getThemeFromSlideWindow();
		setTimeout(function()
		{
			setBackground(theme)
		}, backgroundChangeDelay);
	};
	$('#slide-navigation .slide-up').click(function()
	{
		infoWindow.slidePrevious();
		boardWindow.slidePrevious(animationCallback);
		var theme = getThemeFromSlideWindow();
		lights.not('.' + theme).fadeTo(slideSpeed, 0.3);
		lights.filter('.' + theme).fadeTo(slideSpeed, 1);
		$.cookie('theme', theme);
		return false;
	});
	$('#slide-navigation .slide-down').click(function()
	{
		infoWindow.slideNext();
		boardWindow.slideNext(animationCallback);
		var theme = getThemeFromSlideWindow();
		lights.not('.' + theme).fadeTo(slideSpeed, 0.3);
		lights.filter('.' + theme).fadeTo(slideSpeed, 1);
		$.cookie('theme', theme);
		return false;
	});
	
	// Gets the theme identifier from the currently active slide window.
	function getThemeFromSlideWindow()
	{
		var currentItem = boardWindow.currentItem();
		if (currentItem && currentItem.length > 0)
		{
			return currentItem.attr('id');
		}
		else
		{
			return null;
		}
	}
});

