	
	//Onload functions
	$(document).ready(function() {
	
		// Create navigation tracker
		$("#navigation").append("<div id='highlight-layout'><div id='highlight'></div><div id='highlight-overlay'></div></div>");
		$("#highlight").css("opacity","0.0");
		
		// Set navigation item font
		//Cufon.replace("#navigation ul li a");
	});
	
	//Initial highlight
	function initNavigation(target){
	
		//Add in aditional Nutrition navigation item
		$("#navigation ul").append("<li class='non-inc'><a href='/nutrition/' id='nav-nutrition' title='Nutrition'>Nutrition</a></li>");
	
		//Define navigation position
		posNavigation("#navigation");
		
		// Bind actions to navigation items
		$("#navigation ul li").each(function(itemcount){
			$(this).bind({mouseenter:function(e){
				currentItem = $(this);
				currentPosition = currentItem.position();
				currentWidth = $(this).width();
				currentOuterWidth = $(this).outerWidth();
				placeHighlightNotation("#highlight",currentPosition.left,currentOuterWidth);
			},mouseover:function(e){
				$("a",this).addClass("navselect");
				//Cufon.replace("#navigation ul li a");
			},mouseleave:function(e){
				if ($(this).not(".navigation-select")){$("a",this).removeClass("navselect");}
				//Cufon.replace("#navigation ul li a");
			},click:function(e){
				if($(this).attr("class") != "non-inc"){
					if(target === 1){e.preventDefault();}
					$("#navigation ul li").not(this).removeClass("navigation-select");
					$(this).addClass("navigation-select");
					//Cufon.replace("#navigation ul li a");
					if(target === 1){
		        		var swf = getSwf(swfID);
		        		//closeOverlay();
		        		var navURL = $("a",this).attr("id").replace("nav-","");
		        		swf.sendToSwf(navURL);
						return false;
					}
				}
			}});
		});
		
		if(document.getElementById("swfHolder") != null){
			initHighlight("nav-" + flashvars.startScreen);
		} else {
			var currentURL = document.URL;
			var baseDocument = "default.aspx";
			currentURL = currentURL.replace('http://localhost','')
			currentURL = currentURL.replace('http://www.marsbar.co.uk','').replace('http://www.marsbar.com','');
			currentURL = currentURL.replace('http://www.mars-bar.co.uk','').replace('http://www.mars-bar.com','');
			currentURL = currentURL.replace('http://marsbar.co.uk','').replace('http://marsbar.com','');
			currentURL = currentURL.replace('http://mars-bar.co.uk','').replace('http://mars-bar.com','');
			var locationURL = currentURL.replace(baseDocument,"");
			$("#navigation ul li a").each(function(itemcount){if($(this).attr("href") === locationURL){initHighlight($(this).attr("id"));}});
		}
	}
	
	//Position navigation
	function posNavigation(target){
		var leftOffset = Math.round(($(target).width() - $(target + " ul").width())/2);
		if ($.browser.msie && $.browser.version.substr(0, 1) <= 6) {
			$(target + " ul").css("margin-left",(leftOffset/2));
		} else {
			$(target + " ul").css("margin-left",leftOffset);
		}
	}
	
	//Initial navigation highlight
	function initHighlight(target){
		$("#" + target).parent().addClass("navigation-select");
		$("#navigation ul li").not($("#" + target).parent()).removeClass("navigation-select");
		currentItem = $("#" + target);
		currentPosition = currentItem.position();
		currentWidth = $("#" + target).width();
		currentOuterWidth = $("#" + target).outerWidth();
		placeHighlightNotation("#highlight",currentPosition.left,currentOuterWidth);
	}
	
	//Align navigation highligher 
	function placeHighlightNotation(target,targetpos,targetwidth){
		targetOpc = $(target).css("opacity");
		if (targetOpc == 0){
			$(target).css("opacity","1.0").css("left",targetpos).css("width",targetwidth);
		} else {
			//$(target).animate({"left":targetpos},{queue:false,duration:600,easing:"backEaseInOut"}).animate({"width":targetwidth},{queue:false,duration:400,easing:"sineEaseIn"});
			$(target).animate({left:targetpos,width:targetwidth});
		}
	}

	//Initial Full view
	function initIntro(value){
		if (value == 0){
			$("#layout").append("<div id='content-mask'></div>");
			$("#content-mask").css("width",$(window).width()).css("height",$(window).height());
			$("#footer-layout").hide();
			$("#facebook-like").hide(); //Hide the 'Like' button
		} else if (value == 1){
			//Check for either England or Scotland and hide relevant logo
			var currentURL = document.URL;
			if (currentURL.search(/england/i) != -1){$("#scotland-lockup").hide();}
			if (currentURL.search(/scotland/i) != -1){$("#england-lockup").hide();}
			//Remove mask
			$("#content-mask").fadeOut(250,function(){$(this).remove();});
			$("#footer-layout").show();
			$("#facebook-like").show(); //Show the 'Like' button
		}
	}
	
	//Initial Preview view
	function initPreview(value){
		if (value == 0){
			$("body").append("<div id='content-mask'></div>");
			$("#content-mask").css("width",$(window).width()).css("height",$(window).height());
		} else if (value == 1){
			$("#content-mask").fadeOut(250,function(){$(this).remove();});
		}
	}
	
	//Init scrollbar
	function initScroll(target,value) {
		$(target).addClass("jsscroll");
		var scrollBar = "<div class='scrollbar'><div class='track'><div class='thumb'><div class='end'></div></div></div></div>";
		$(target).prepend(scrollBar);
		if (value != null){
     		$(target).tinyscrollbar({size:value});
 		} else {
			$(target).tinyscrollbar();
		}
	}
	
	//Search array function
	function arrayMatch(targetArray,targetValue,targetLocation) {
		targetArrayLength = targetArray.length;
		for (var a = 0; a <= (targetArrayLength - 1); a++){
			if (targetArray[a].match(RegExp(targetValue, 'i'))) {
				//targetArray.push(a);
				$(targetLocation).append("<li>" + targetArray[a] + "</li>");
			}
		}
	}
	
	//Close bespoke overlay
	function closeOverlay() {
		var innerOverlay = document.getElementById('innerOverlay');
		var iContent = document.getElementById('iContent');
		iContent.src = '';
		innerOverlay.style.display = 'none';
	}
	
	
	// =========================================================== //
	// ====== Start: Flash functions ============================= //
	// =========================================================== //
	
	function fromSwfGetBase(){
		return swfBase;
	}
	
	function fromSwfShowContent(value, isClick) {
	    var swfAutoShow = [];  //['fa']; // any overlay screens you want to show automatically	
	    var isClick = (value.indexOf('#true') > -1);
	    var value = value.replace('#true','');
	    initHighlight("nav-" + value);

	    var found = false;
	    for (var n = 0; n < swfAutoShow.length; n++) {
	        if (swfAutoShow[n] == value) {
	            found = true;
	            break;
	        }
	    }

	    if (isClick || found) {
			swf = getSwf(swfID);
			swf.sendToSwfStop();
			var dir = document.getElementById('nav-' + value).href;
			var url = dir + 'frame.aspx';
			if(dir.substr(dir.length - 18, dir.length) == "fa-mars-just-play/" || dir.substr(dir.length - 24, dir.length) == "scotland-mars-just-play/"){
				$.fancybox({"width":942,"height":600,"padding":2,"margin":0,scrolling:"no","autoScale":false,"scrolling":"no","transitionIn":"fade","transitionOut":"none","type":"iframe","href":url,"hideOnOverlayClick":false});
				$("#fancybox-frame").focus();
				$("#fancybox-loading div").css("height","600px");
				$("#fancybox-frame").css("height","600px");
			} else {
	        	$.fancybox({"width":942,"height":460,"padding":2,"margin":0,scrolling:"no","autoScale":false,"scrolling":"no","transitionIn":"fade","transitionOut":"none","type":"iframe","href":url,"hideOnOverlayClick":false});
				$("#fancybox-frame").focus();
			}
	    }
	}	
	
	function getSwf(swfID){
		if (window.document[swfID]){return window.document[swfID];}
		if (navigator.appName.indexOf("Microsoft Internet")==-1){
			if (document.embeds && document.embeds[swfID]){
				return document.embeds[swfID]; 
			} else {
				return document.getElementById(swfID);
 			}
		}
	}
	
	function fromSwfNavEnable(screens){
		if (screens != null){
			var reverse = true;
			var str = screens;
			var navConfig = new Array(); 
			navConfig = str.split(",");
			var navDev = new Array();
			var tempNav;
			$("#nav-bar li").each(function(groupcount){
				navDev.push($(this).html());
				$(this).remove();
      		});
			if (navConfig.length != navDev.length){
				alert("Please update the HTML navigation to match the scenes within the Flash movie.");
			} else {
			 	if(!reverse){
					for(var a=0;a<(navConfig.length - 1);a++){
						searchStr = "nav-" + navConfig[a];
						arrayMatch(navDev,searchStr,"#nav-bar")
					}
				} else {
					for (var a=(navConfig.length + 1);a>0;a--) {
						searchStr = "nav-" + navConfig[(a-1)];
						arrayMatch(navDev,searchStr,"#nav-bar")
					}
				}
			}
		}
		initNavigation(1,null);
		initIntro(1);
	}
	
	// =========================================================== //
	// ====== End: Flash functions =============================== //
	// =========================================================== //
	
	// =========================================================== //
	// ====== Start: EBA 2011 functions ========================== //
	// =========================================================== //
	
	//var helpContent = "<p><strong>Why do I need to enter a security code?</strong><br />We ask you to enter the letters and numbers in a picture when we need to make sure that a person, not an automated program, is trying to enter the promotion. The characters are drawn so that it's possible for a person to recognize them, but very difficult for a program to. This helps us prevent automated programs from entering the promotion reducing your chance of winning a prize.</p><p>The letters are case-sensitive, so ensure you type them as displayed on the screen, uppercase or lowercase.</p><p>Don't type spaces between the characters.</p><p>If you type the characters incorrectly or find it difficult to read the security code then click on the circler arrow next to the security code. The code will then refresh and display an alternative code.</p>";
	
	function initHelp(){
		//Search target items and insert help feature
		$(".ub-target").remove();
		$(".reqHelper").each(function(itemcount){			
			helperItem = "<span id='ht"+itemcount+"' class='ub-target'>(Require help?)</span>"
			$(this).append(helperItem);
       	});
		
		//Bind helper function
		$(".ub-target").each(function(itemcount){
			$(this).hover(function(event) {
       			$(this)[ ((event.type == 'mouseenter') ? 'add' : 'remove') + 'Class' ]('ub-target-hover');
   			});
		});
		//Bind tooltip
		uiTooltip(".ub-target");
	}
	
	function uiTooltip(targetCollection){
		$(targetCollection).each(function(i){
			$(this).bind({
				mousemove:changeTooltipPosition,
				mouseenter:showTooltip,
				mouseleave:hideTooltip
			});
			$(this).data("title", $(this).attr("title")); 
			$(this).removeAttr("title");
		});
	}
	
	var changeTooltipPosition = function(event) {
		var tempWidth = $("div.tooltip-layout").outerWidth();
		var tempHeight = $("div.tooltip-layout").find(".tooltip").outerHeight();
		//var tooltipX = event.pageX - 30;
		//var tooltipY = (event.pageY - 15) - tempHeight;
		//$("div.tooltip-layout").css({top:tooltipY+"px",left:tooltipX+"px"});
		var tooltipX = event.pageX + 30;
		var tooltipY = event.pageY - 15;
		$("div.tooltip-layout").css({top:tooltipY+"px",left:tooltipX+"px"});
	};
	
	var showTooltip = function(event) {
		//alert('Enter');
		$("div.tooltip-layout").remove();
		var ttContent =  "<p><strong>Why do I need to enter a security code?</strong><br />We ask you to enter the letters and numbers in a picture when we need to make sure that a person, not an automated program, is trying to enter the promotion. The characters are drawn so that it's possible for a person to recognize them, but very difficult for a program to. This helps us prevent automated programs from entering the promotion and claiming a prize.</p><p>The letters are case-sensitive, so ensure you type them as displayed on the screen, uppercase or lowercase.</p><p>Don't type spaces between the characters.</p><p>If you type the characters incorrectly or find it difficult to read the security code then click on the circler arrow next to the security code. The code will then refresh and display an alternative code.</p>";
		var ttLayout = "<div id='help-tooltip' class='tooltip-layout'><div class='tooltip-wrap'><div class='tooltip-backing'></div><div class='tooltip'>"+ttContent+"</div></div></div>";
		$("#eba-container").append(ttLayout);
		var my_tooltip = $("div.tooltip-layout");
		my_tooltip.css("height",$(".tooltip").outerHeight()+8).css("width",$(".tooltip").outerWidth()+8);
		var tempWidth = $("#help-tooltip").outerWidth();
		var tempHeight = $("#help-tooltip").outerHeight();
		$(".tooltip-backing").css("width",tempWidth+"px").css("height",tempHeight+"px").css({opacity:0.5});
		changeTooltipPosition(event);
	};
	
	var hideTooltip = function(){$("div.tooltip-layout").remove();};
	
	// =========================================================== //
	// ====== End: EBA 2011 functions ============================ //
	// =========================================================== //
	
	// =========================================================== //
	// ====== Start: Generic functions =========================== //
	// =========================================================== //
	
	function scrollWin(){
		$("html, body").animate({scrollTop: $("#scrolltop").offset().top},"fast");
	}

	function switchClass(passTarget,passClass) {
		if (passTarget === undefined) {
		
		} else {
			document.getElementById(passTarget).className = passClass;
		};
	}
	
	function switchmultipleState(targetType,targetID, switchID){
		var itemCollection = document.getElementsByTagName(targetType);
		for (var i=0; i<itemCollection.length; i++) {
			if(itemCollection[i].className === targetID) {
				document.getElementById(itemCollection[i]).className = passClass;
			}
		}
	}

	function initMultiFade(targetID){
		$('' + targetID + '').fadeTo("slow", 0.45);
		$('' + targetID + '').hover(function(){$(this).fadeTo('' + targetID + '', 1.0);},function(){$(this).fadeTo('' + targetID + '', 0.3);});
	}

	function initFadeIn(targetID){
		$('' + targetID + '').css("opacity","0");
		$('' + targetID + '').stop().animate({opacity:1.0},1000);
	}
	
	function initFadeInDelay(targetID,delayLength){
		$('' + targetID + '').css("opacity","0");
		$('' + targetID + '').stop().delay(delayLength).animate({opacity:1.0},1000);
	}
	
	// =========================================================== //
	// ====== End: Generic functions ============================= //
	// =========================================================== //
