var ISUK = ISUK || {};

window.addEvent("domready", function() {
	// Start up search box.
	(function() {
		var search = $E("#page-header .search .text-box");
		
		search.addEvent('focus', function() {
			if (search.value === "SEARCH") {
				search.value = "";
			}
		});
		search.addEvent('blur', function() {
			if (search.value === "") {
				search.value = "SEARCH";
			}
		});
	})();
    //
	// Start up newsletter box.
	(function() {
		var email = $E("#newsletter .email");
		
		email.addEvent('focus', function() {
			if (email.value === "Email address here") {
				email.value = "";
			}
		});
		email.addEvent('blur', function() {
			if (email.value === "") {
				email.value = "Email address here";
			}
		});
	})();
	// Start up 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 closeEvent;
		var offsetEl = $("page-body");
		$$("#page-body .menu li").each(function(li) {
			var ul = $E("ul", li);
			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: "142px"
						});
					}
				}
			});
		});
		// Exiting the menu entirely it treated as a special case with
		// a delay.
		var menu = $E("#page-body .menu");
		var rootUL = $E("#page-body .menu ul");
		
		menu.addEvent("mouseout", function(e) {
			var e = new Event(e);
			
			if (!menu.hasChild(e.relatedTarget)) {
				closeEvent = (function() {
					closeTree(rootUL);
				}).delay(mouseOutCloseDelay);
			}
		});
	})();
});

function add_to_favourites(){

		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel("Archive Film Shop", "http://www.archivefilmshop.co.uk","");
		} else if( window.external ) { // IE Favorite
			window.external.AddFavorite( "http://www.archivefilmshop.co.uk","Archive Film Shop"); 
		} else if(window.opera && window.print) { // Opera Hotlist
			return true; 
		}

      window.external.AddFavorite("http://www.archivefilmshop.co.uk","Archive Film Shop");
}
