if (typeof DEBUG != "function") {
	function DEBUG(string) {
		try {
			console.log(string);
		}
		catch (e) {}
	}
}



var Ikebana = {};

Ikebana.Nav = function() {
	var _private = {};
	var _public = {
		/*-----------------------------------------------------------------------------------------
		 * add node graphics to a menu
		 *
		 * params:
		 *   aCfg:     an array containing objects structured like
		 *
		 *             {
		 *               id,
		 *               gfxBase: { url, width, height },
		 *               gfxHover: { url, width, height }
		 *             }
		 *
		 *   bPreload: flag indicating if hover graphics are to be preloaded (defaults to true) 
		 */
		addNodeGfx: function(aCfg) {
			if (   typeof aCfg != "object"
			    || typeof aCfg.length != "number") {
				return;
			}
			for (var i=0; i<aCfg.length; i++) {
				var oMenuItem;

				if (   typeof aCfg[i] != "object"
				    || typeof aCfg[i].id != "string"
				    || !(oMenuItem = YAHOO.util.Dom.get(aCfg[i].id))) {

					continue;
				}
				if (   typeof aCfg[i].gfxBase == "object"
				    && typeof aCfg[i].gfxBase.url == "string"
				    && typeof aCfg[i].gfxBase.width == "number"
				    && typeof aCfg[i].gfxBase.height == "number") {
					// hide link text and set initial oLink styles
					var oLink;
					var oText;
					
					if ((oLink = YAHOO.util.Dom.getElementsBy(function(elem) {
					            	return YAHOO.util.Dom.hasClass(elem, "yuimenubaritemlabel") || YAHOO.util.Dom.hasClass(elem, "yuiMenuItemlabel") || YAHOO.util.Dom.hasClass(elem, "menuLink");
					            }, "a", oMenuItem)[0])) {
						// styles depending on node graphic
						oLink.style.width = "" + aCfg[i].gfxBase.width + "px";
						oLink.style.height = "" + aCfg[i].gfxBase.height + "px";

						// independent styles
						oLink.style.display = "block";
						oLink.style.margin = "0";
						oLink.style.border = "0 none";
						oLink.style.padding = "0";

						oText = YAHOO.util.Dom.getElementsBy(function(elem) {
							return true;
						}, "span", oLink)[0];

						if (oText) {
							oText.style.display = "none";
						}
					}
					
					// set initial menu item styles
					//  -> styles depending on node graphic
					oMenuItem.style.backgroundImage = "url(" + aCfg[i].gfxBase.url + ")";
					oMenuItem.style.width = "" + aCfg[i].gfxBase.width + "px";
					oMenuItem.style.height = "" + aCfg[i].gfxBase.height + "px";

					//  -> independent styles
					oMenuItem.style.backgroundPosition = "0 0";
					oMenuItem.style.backgroundRepeat = "no-repeat";
					oMenuItem.style.padding = "0";

					if (   typeof aCfg[i].gfxHover == "object"
					    && typeof aCfg[i].gfxHover.url == "string"
					    && aCfg[i].gfxHover.url != aCfg[i].gfxBase.url
					    && typeof aCfg[i].gfxHover.width == "number"
					    && typeof aCfg[i].gfxHover.height == "number") {
						// prepare event handling in case base and hover graphics differ
						var oBase = {
							menuItem: oMenuItem,
							url: aCfg[i].gfxBase.url,
							width: aCfg[i].gfxBase.width,
							height: aCfg[i].gfxBase.height,
							link: oLink
						};
						var oHover = {
							menuItem: oMenuItem,
							url: aCfg[i].gfxHover.url,
							width: aCfg[i].gfxHover.width,
							height: aCfg[i].gfxHover.height,
							link: oLink
						};

						// register event handlers
						YAHOO.util.Event.removeListener(oMenuItem, "mouseover");
						YAHOO.util.Event.removeListener(oMenuItem, "mouseout");
						YAHOO.util.Event.addListener(oMenuItem, "mouseover", function() {
							this.menuItem.style.backgroundImage = "url(" + this.url + ")";
							this.menuItem.style.width = "" + this.width + "px";
							this.menuItem.style.height = "" + this.height + "px";
							this.link.style.width = "" + this.width + "px";
							this.link.style.height = "" + this.height + "px";
						}, oHover, true);
						YAHOO.util.Event.addListener(oMenuItem, "mouseout", function() {
							this.menuItem.style.backgroundImage = "url(" + this.url + ")";
							this.menuItem.style.width = "" + this.width + "px";
							this.menuItem.style.height = "" + this.height + "px";
							this.link.style.width = "" + this.width + "px";
							this.link.style.height = "" + this.height + "px";
						}, oBase, true);

						// clean-up
						delete oBase;
						delete oHover;
					}
				}
			}
			// preload hover graphics
			if (typeof bPreload != "boolean" || bPreload == true) {
				var aImages = [];

				for (var i=0; i<aCfg.length; i++) {
					if (   typeof aCfg[i].gfxBase == "object"
					    && typeof aCfg[i].gfxBase.url == "string"
					    && typeof aCfg[i].gfxBase.width == "number"
					    && typeof aCfg[i].gfxBase.height == "number"
					    && typeof aCfg[i].gfxHover == "object"
					    && typeof aCfg[i].gfxHover.url == "string"
					    && aCfg[i].gfxHover.url != aCfg[i].gfxBase.url
					    && typeof aCfg[i].gfxHover.width == "number"
					    && typeof aCfg[i].gfxHover.height == "number") {
						// Preload graphics if and only if they are really used as node graphics,
						// i.e. if they match the conditions tested above and if the image changes
						// on hover. To fix an issue with IE not caching images that are used as
						// background images only, the base images have to preloaded as well.
						aImages[aImages.length] = aCfg[i].gfxBase.url;
						aImages[aImages.length] = aCfg[i].gfxHover.url;
					}
				}
				if (aImages.length > 0) {
					var oContainer = document.createElement("div");

					oContainer.style.position = "absolute";
					oContainer.style.top = "-10000px";
					oContainer.style.left = "-10000px";

					for (var i=0; i<aImages.length; i++) {
						var oImage = document.createElement("img");

						oImage.setAttribute("src", aImages[i]);
						oImage.setAttribute("alt", "");
						oContainer.appendChild(oImage);
					}
					document.body.appendChild(oContainer);
				}
				delete aImages;
			}
		}
	};
	return _public;
}();

YAHOO.util.Event.onDOMReady(function() {
	try {
		for (var i = 0; i < Ikebana.generated.Nav.nodeGfx.length; i++) {
			Ikebana.Nav.addNodeGfx(Ikebana.generated.Nav.nodeGfx[i]);
		}
	} catch(e) {}
});
