

var baseURL = 'http://www.dearcadbury.com';
var emailPageURL = 'http://www.cadbury.com/Pages/EmailPagetoFriend.aspx?PageURL={URL}&PageTitle={TITLE}';

// Called from flash - used to update printer and email page links with current URL of flash file
function changePage(page) {
	page = page.replace(/\.swf/, '');
	$('li#html-version a').attr({ 'href': page + '.aspx?format=html' });
	$('li#accessible-version a').attr({ 'href': page + '.aspx?format=html' });
	var url = emailPageURL.replace(/{URL}/, escape(baseURL + ((page.substring(0, 1) != '/') ? '/': '') + page + '.aspx'));
	url = url.replace(/{TITLE}/, escape('Dear Cadbury'));
	$('li#email-page a').attr({ 'href': url });
}

// JQuery is ready
$(document).ready(function() {

	// Set up search box watermark
	function switchText()
	{
		if ($(this).val() == $(this).attr('title'))
			$(this).val('').removeClass('exampleText');
		else if ($.trim($(this).val()) == '')
			$(this).addClass('exampleText').val($(this).attr('title'));
	}
	
	$('input[type=text][title!=""]').each(function() {
		if ($.trim($(this).val()) == '') $(this).val($(this).attr('title'));
		if ($(this).val() == $(this).attr('title')) $(this).addClass('exampleText');
	}).focus(switchText).blur(switchText);

	$('form').submit(function() {
		$(this).find('input[type=text][title!=""]').each(function() {
			if ($(this).val() == $(this).attr('title')) $(this).val('');
		});
	});
	// End search box watermark set up
	
	// Set up opening external pages in a new browser window
	$("a[@rel$='external']").click(function() { this.target = "_blank"; });
	$("a[@rel$='external']").each(function() {
			if ($(this).attr('title').length == 0)
				$(this).attr({title: "Open page in a new browser window"});
		}
	);
	
	// Replace HTML content, if appropriate
	function returnFileName()
	{
		// Function to get the flash version of this html page
		var pathname = document.location.pathname.replace(/\.aspx/, '.swf');
		pathname = (pathname == '/') ? '/index.swf': pathname;
		return (pathname.indexOf('.swf') == -1) ? pathname + 'index.swf': pathname;
	}
	
	// Set format back to interactive, if required
	if ($.query.get('format') == 'interactive') $.cookie('dc-format', 'interactive', { path: '/' });
	
	// Only replace the HTML if the format=html query string is not present (format=html exists so we can display printer friendly pages)
	if (($.query.get('format') == 'html') || ($.cookie('dc-format') == 'html')) {
		
		// Only show the flash version link if they have flash available
		$.cookie('dc-format', 'html', { path: '/' });
		if (swfobject.hasFlashPlayerVersion("8.0.0")) $('li#interactive-version').show();
		
	} else if ($('#contentContainer').length > 0) {
	
		// Replace HTML content with flash
		var d = new Date();
		var flashvars = { startPage: returnFileName() };
		var params = {};
		var attributes = {};
		
		swfobject.embedSWF("/main.swf", "contentContainer", "830", "490", "8.0.0", "/expressInstall.swf", flashvars, params, attributes);
		
		$('li#html-version').show();
		$('li#accessible-version').show();
		
	}
	$('#contentContainer').show();
	// End HTML content replacement
});