window.addEvent("domready", function() {
	$(document.body).removeClass("cssanim");
	
	// Start up search box.
	(function() {
		var search = $E("#page-header .search .text-box");
		if (!search) {
			return;
		}
		
		search.addEvent('focus', function() {
			if (search.value === "search") {
				search.value = "";
				search.addClass("active");
			}
		});
		search.addEvent('blur', function() {
			if (search.value === "") {
				search.value = "search";
				search.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 closeEvent;
		var offsetEl = $("page-body");
		
		var products = {};
		
		$$("#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: "140px"
						});
					}
				}
			});
		});
		
		// 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 mini-basket.
	(function() {
		var closeDelay = 500;
		
		var basket = $E("#mini-cart");
		var closeEvent;
		
		if (!basket) {
			return;
		}
		
		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);
			}
		});
	})();
});

/** This function should make the png transparent in IE 6*/
function MakePNGTransparent(){
	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])
	
	if ((version >= 5.5) && (version <7) && (document.body.filters)) 
	{
	   for(var i=0; i<document.images.length; i++)
	   {
	      var img = document.images[i]
	      var imgName = img.src.toUpperCase()
	      if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
	      {
	         var imgID = (img.id) ? "id='" + img.id + "' " : ""
	         var imgClass = (img.className) ? "class='" + img.className + "' " : ""
	         var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
	         var imgStyle = "display:inline-block;" + img.style.cssText 
	         if (img.align == "left") imgStyle = "float:left;" + imgStyle
	         if (img.align == "right") imgStyle = "float:right;" + imgStyle
	         if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
	         var strNewHTML = "<span " + imgID + imgClass + imgTitle
	         + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
	         + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
	         + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
	         img.outerHTML = strNewHTML
	         i = i-1
	      }
	   }
	}
}
