var Galler = Galler || {};

window.addEvent("domready", function() {
	$(document.body).removeClass("cssanim");
	
	// 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 closeEvent;
		var offsetEl = $("page-body");
		
		var products = Galler.menu || {};
		
		$$("#page-body .menu li").each(function(li) {
			// Check wether this node is a product leaf node.
			var product = $E("a", li).href.match(/pid=(\d+)/);
			var pid = product && product[1];
			
			product = products[pid];
			
			if (product) {
				// This is a product, so create the product profile.
				li.innerHTML += '<ul><li class="first"><div class="product-profile"><img src="' + product.image + '" alt="" /><h3>' + product.name + '</h3><p>' + product.blurb + '</p><a href="' + product.link + '">View</a></div></li></ul>';
			}
			
			var ul = $E("ul", li);
			
			// Allow click anywhere on product profile.
			if (ul) {
				ul.addEvent("click", function(e) {
					var e = new Event(e);
					if (product) {
						window.location = product.link;
					}
				});
			}
			
			// 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: "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);
			}
		});
	})();
	
	
	// 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 mini-basket.
	(function() {
		var closeDelay = 500;
		
		var basket = $E("#mini-cart");
		var closeEvent;
		
		basket.addEvent('mouseover', function(e) {
			var e = new Event(e);
			
			$clear(closeEvent);
			basket.addClass("active");
		});
		
		basket.addEvent('mouseout', function(e) {
			var e = new Event(e);
			
			if (!basket.hasChild(e.relatedTarget)) {
				closeEvent = (function() {
					basket.removeClass("active");
				}).delay(closeDelay);
			}
		});
	})();
});
