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;

if (typeof window.console == 'undefined')
{
    window.console =
	{
        log: function() {},
        debug: function() {}
    };
}


$(function()
{
	function fadeButton(buttons, defaultOpacity, hoverOpacity)
	{
		$(buttons).css('opacity', defaultOpacity).hover(function()
		{
			$(this).stop().fadeTo('fast', hoverOpacity);
		}, function()
		{
			$(this).stop().fadeTo('fast', defaultOpacity);
		});
	}
	// Sociable and "follow" button fading.
	fadeButton('.sociable img', 0.4, 1);
	fadeButton('#affiliations #follow img', 0.65, 1)
	
	$.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');

    // Show Hide Buttons
    
    $('a.show-hide-menu').click(function(event) {
        $(this).next('ul').toggle("slow");
        event.preventDefault();
    });



	// Text replacement.
	/*$('h2').parents().show();
	$('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;
}

