var defaultTheme;

// Slide show animation.
var slideShowItemDuration = 4000;
var slideShowFadeDuration = 2000;

// Contact scroll animation.
var contactScrollSpeed = 2000;
var contactBlinkColour = '#da5352';
var contactBlinkCount = 2;

// Theme animation.
var slideSpeed = 1000;
var backgroundChangeDelay = 0;
var backgroundFadeSpeed = 1000;

var body;
var fauxBody;


$(function()
{
	$.cookie.defaults.path = '/';
	
	$('#slide .slide-show').crossFade({ itemDuration: slideShowItemDuration, fadeDuration: slideShowFadeDuration });
	
	// Set up background-fading apparatus.
	body = $('body');
	body.prepend('<div id="faux-body"></div>');
	fauxBody = $('#faux-body');
	
	// Text replacement.
	$('h2').textReplace(
	{
		scriptUrl: '/wp-content/themes/helencarter/resources/scripts/textReplace.php',
		scriptParameters:
		{
			font: 'constan.ttf',
			sampleText: 'M',
			height: 17
		}
	});
	
	// Set the current theme to whatever it was before, using cookies.
	var theme = $.cookie('theme');
	theme = theme ? theme : defaultTheme;
	if (theme)
	{
		setBackground(theme, true);
	}
	
	// Set up 'Get in Touch' scrolling and blinking.
	var contactDetails = $('#footer p#contact')
	var contactText = contactDetails.add('#footer p#contact a');
	$().localScroll({ onAfter: function()
	{
		var colour = contactDetails.css('color');
		for (var i = 0; i < contactBlinkCount; i++)
		{
			contactText.animate({ color: contactBlinkColour });
			contactText.animate({ color: colour }, function()
			{
				contactText.css('color', '');
			});
		}
	} });
});

// Sets the page background according to the currently viewed theme.
function setBackground(theme, immediate)
{
	var background = getBackgroundUrl(theme, true);
	if (immediate)
	{
		body.css('background-image', background);
	}
	else
	{
		animateBackground(background)
	}
}

// Animates a new background into the page.
function animateBackground(newBackground)
{
	var oldBackground = body.css('background-image');
	if (newBackground != oldBackground)
	{
		fauxBody.css('background-image', oldBackground);
		fauxBody.css('opacity', 1);
		body.css('background-image', newBackground);
		fauxBody.animate({ opacity: 0 }, backgroundFadeSpeed);
	}
}

// Gets the background image URL for the specified theme, with optional CSS url(...) wrapper.
function getBackgroundUrl(theme, includeCssWrapper)
{
	var url = '/wp-content/themes/helencarter/resources/graphics/backgrounds/bg-body-' + theme + '.jpg';
	if (includeCssWrapper)
	{
		url = 'url(' + url + ')';
	}
	
	return url;
}
