window.addEvent("domready", function() {
	/*
		body.cssanim is used to animate menus etc. by CSS alone.
		Once JS is loaded we can overwrite CSS animations an improve with JS. 
	*/
	$(document.body).removeClass("cssanim");
	
	// Start up search box.
	(function() {
		var search = $$("input.focus-replace");
		if (!search) {
			return;
		}
		
		search.each(function(el) {
			var initial = el.value;
			el.addEvent('focus', function() {
				if (el.value == initial) {
					el.value = "";
					el.addClass("active");
				}
			});
			el.addEvent('blur', function() {
				if (el.value == "") {
					el.value = initial;
					el.removeClass("active");
				}
			});
		});
	})();

	// Start up javascript menu.
	(function() {
		var mouseOutCloseDelay = 1000;
		
		function closeTree(root) {
			var all = $ES("ul", root);
			for (var i=all.length-1; i>=0; i--) {
				all[i].setStyle("display", "none");
			};
		}
		
		function closeSiblings(self) {
			self.parentNode.getChildren().each(function(child) {
				if (child !== self && child.tagName === "LI") {
					closeTree(child);
				}
			});
		}
		
		// Stores the delayed close event. Will be cancelled if mouse
		// re-enters the menu before it closes.
		var offsetEl = $("page-body");
		
		var products = {};
		
		$$('.flyout').each(function(flyout){
			var closeEvent;
	
			$ES("li", flyout).each(function(li) {
				var ul = $E("ul", li);
				
				// Now onto the actual dropdown menu code.
				li.addEvent("mouseover", function(e) {
					var e = new Event(e);
					e.stop();
					
					$clear(closeEvent);
					
					closeSiblings(li);
					if (ul) {
						ul.setStyle("display", "block");
						
						var pos = ul.getPosition();
						var oePos = offsetEl.getCoordinates();
						
						if (pos.x-oePos.left+140 > oePos.width) {
							// Open to the left if it would go past the end of the screen.
							ul.setStyles({
								left: "-138px",
								top: "4px",
								width: "140px"
							});
						}
					}
				});
			});
			
			// Exiting the menu entirely it treated as a special case with
			// a delay.
			var menu = flyout;
			var rootUL = menu;
			
			if (!menu) {
				return;
			}
			
			menu.addEvent("mouseout", function(e) {
				var e = new Event(e);
				
				if (!menu.hasChild(e.relatedTarget)) {
					closeEvent = (function() {
						closeTree(rootUL);
					}).delay(mouseOutCloseDelay);
				}
			});
		});
	})();
});

window.addEvent("domready", function() {
	var updateClock = function(){
	
	var element = $E('#page-body #breadcrumbs .clock');
	if(element == null){return;}
	var currentTime = new Date ();
	var currentHours = currentTime.getHours ()+EVENT_HOURS_OFFSET;
	var currentMinutes = currentTime.getMinutes ()+EVENT_MINUTES_OFFSET;
	var currentSeconds = currentTime.getSeconds ()+EVENT_SECONDS_OFFSET;
	
	 // Pad the valueswith leading zeros, if required
	currentHours = ( currentHours < 10 ? "0" : "") + currentHours;
	currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
	currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
	
	// Compose the string for display
	var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds;
	
	element.innerHTML = "UK TIME: " + EVENT_DAY + ' ' + currentTimeString; //currentTimeString
	}
	//updateClock.attempt();
	//updateClock.periodical(1000);
});

