function MenuEngine(pName, pProductList) {
	if (typeof(pProductList) != "undefined" && pProductList) {
		this.productMenu = true;
		this.textAlign = "align=\"right\" ";
		this.textStyle = "prodMenuText";
	} else {
		this.productMenu = false;
		this.textAlign = "align=\"left\" ";
		this.textStyle = "prodMenuText"
	}
	this.menuName = pName;
	this.menuItems = new MenuItemCollection();
	return this;
}

MenuEngine.prototype.initStyle = function(pLeft, pTop, pWidth, pZI, pInitHex, pActiveHex, pInitOpacity, pActiveOpacity) {
	menuLeft = (typeof(pLeft) == "undefined") ? 0 : pLeft;
	menuTop = (typeof(pTop) == "undefined") ? 0 : pTop;
	menuWidth = (typeof(pWidth) == "undefined") ? 0 : pWidth;
	menuZIstyle = (typeof(pZI) == "undefined") ? "" : " z-index:" + pZI;
	triggerZIstyle = (typeof(pZI) == "undefined") ? "" : " z-index:" + (pZI - 1);
	menuInitHex = (typeof(pInitHex) == "undefined") ? "#FFFFFF" : "#" + pInitHex;
	menuActiveHex = (typeof(pActiveHex) == "undefined") ? "#FFFFFF" : "#" + pActiveHex;
	menuInitOpacity = (typeof(pInitOpacity) == "undefined") ? 100 : pInitOpacity;
	menuActiveOpacity = (typeof(pInitOpacity) == "undefined") ? 100 : pActiveOpacity;
	
	this.menuLeft = menuLeft;	
	this.menuTop = menuTop;
	this.menuWidth = menuWidth;
	this.menuZI = (menuZIstyle == "") ? "No z-index specified" : pZI;
	this.menuInitHex = menuInitHex;
	this.menuActiveHex = menuActiveHex;
	this.menuInitOpacity = menuInitOpacity;
	this.menuActiveOpacity = menuActiveOpacity;
	
	outputStyle = "<style>";

	if (window.document.layers) {
		outputStyle += "#" + this.menuName + "Menu {position:absolute; left:" + menuLeft + "px; top:" + menuTop + "px; width:" + menuWidth + "px; visibility:hidden;" + menuZIstyle + "}";
	} else {
		outputStyle += "#" + this.menuName + "Menu {position:absolute; left:" + menuLeft + "px; top:" + menuTop + "px; width:" + menuWidth + "px; visibility:hidden; " + menuZIstyle + " background-color:" + this.menuInitHex + ";}";
		outputStyle += "." + this.menuName + "MenuStyle {background-color:" + this.menuInitHex + "; filter:alpha(Opacity=" + this.menuInitOpacity + ");}";
	}
	
	for (i=0; i < this.menuItems.getLength(); i++) {
		if (window.document.layers) {
			outputStyle += "#" + this.menuName + "MenuItem" + i + " {position:relative; layer-background-color:" + this.menuInitHex + ";}";
		} else {
			outputStyle += "#" + this.menuName + "MenuItem" + i + " {background-color:" + this.menuInitHex + "; filter:alpha(Opacity=" + this.menuInitOpacity + ");}";
		}
	}
	
	if (window.document.layers) {
		outputStyle += "#" + this.menuName + "Trigger {position:absolute; left:" + (menuLeft - 40) + "px; top:" + menuTop + "px; width:" + (this.menuWidth + 80) + "px; visibility:hidden;" + triggerZIstyle + "}";
	} else {
		outputStyle += "#" + this.menuName + "Trigger {position:absolute; left:" + (menuLeft - 40) + "px; top:" + menuTop + "px; width:" + (this.menuWidth + 80) + "px; visibility:hidden;" + triggerZIstyle + "}";
	}
	
	outputStyle += "</style>"

	this.style = outputStyle;
	return document.write(this.style);
}

MenuEngine.prototype.highlightItem = function(pId, pState) {
	if (document.layers) {
		if (pState == "on") {
			eval('document.' + this.menuName + 'Menu.document.' + pId + '.bgColor = \"' + this.menuActiveHex + '\"');
		} else if (pState == "off") {
			eval('document.' + this.menuName + 'Menu.document.' + pId + '.bgColor = \"' + this.menuInitHex + '\"');    
		}
	} else {
		if (pState == "on") {
			eval('document.all.' + pId + '.style.filter = "alpha(Opacity=' + this.menuActiveOpacity + ')"');
			eval('document.all.' + pId + '.style.backgroundColor = \"' + this.menuActiveHex + '\"');
		} else if (pState == "off") {
			eval('document.all.' + pId + '.style.filter = "alpha(Opacity=' + this.menuInitOpacity + ')"');
			eval('document.all.' + pId + '.style.backgroundColor = \"' + this.menuInitHex + '\"');
		}
	}
}


MenuEngine.prototype.writeMenuHTML = function() {
	outputHTML = "<div id=\"" + this.menuName + "Menu\">";
	if (window.document.layers) {
		outputHTML += "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"" + this.menuInitHex + "\">";
		outputHTML += "<tr><td><img src=\"images/spacer.gif\" height=\"3\" width=\"" + this.menuWidth + "\" border=\"0\"></a></td></tr>";
	} else {
		outputHTML += "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
		outputHTML += "<tr><td class=\"" + this.menuName + "MenuStyle\"><img src=\"images/spacer.gif\" height=\"3\" width=\"" + this.menuWidth + "\" border=\"0\"></a></td></tr>";
	}
	
	for (i=0; i < this.menuItems.getLength(); i++) {
		var indent = "";
		var rightSpace = "";
		if (this.menuItems.menuItemAt(i).level != 0) {
			if (window.document.layers) {
				var indent = (this.menuItems.menuItemAt(i).level == 1)? "<span class=\"" + this.textStyle + "\">&nbsp;&nbsp;&nbsp;</span>" : "<span class=\"" + this.textStyle + "\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
			} else {
				var indent = (this.menuItems.menuItemAt(i).level == 1)? "<span class=\"" + this.textStyle + "\">&nbsp;&nbsp;</span>" : "<span class=\"" + this.textStyle + "\">&nbsp;&nbsp;&nbsp;&nbsp;</span>";
			}
		} else {
			rightSpace = (window.document.layers)? "<span class=\"" + this.textStyle + "\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>" : "<span class=\"" + this.textStyle + "\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
		}
		var primeElement = (this.menuItems.menuItemAt(i).level == 1);
		this.menuItems.menuItemAt(i).displayName = this.menuItems.menuItemAt(i).displayName.replace(/<br>/g, rightSpace + "<br>" + indent);
		
		if (window.document.layers) {
			if ((primeElement) && (i != 0)) {
				outputHTML += "<tr><td><img src=\"images/spacer.gif\" height=\"6\" width=\"" + this.menuWidth + "\" border=\"0\"></td></tr>";
			}
			outputHTML += "<tr>";
			outputHTML += "<td  width=\"" + this.menuWidth + "\" " + this.textAlign + "valign=\"middle\"><div id=\"" + this.menuName + "MenuItem" + i + "\">" + indent;
			if (this.menuItems.menuItemAt(i).href != "") {
				outputHTML += "<a href=\"" + this.menuItems.menuItemAt(i).href + "\" onMouseOver=\"" + this.menuName + ".highlightItem(\'" + this.menuName + "MenuItem" + i + "\',\'on\');\" onMouseOut=\"" + this.menuName + ".highlightItem(\'" + this.menuName + "MenuItem" + i + "\',\'off\');\" class=\"" + this.textStyle + "\">";
			}
			outputHTML += "<span class=\"" + this.textStyle + "\">" + this.menuItems.menuItemAt(i).displayName + "</span>" + rightSpace +"<br><img src=\"images/spacer.gif\" height=\"1\" width=\"" + this.menuWidth + "\" border=\"0\">";
			if (this.menuItems.menuItemAt(i).href != "") {
				outputHTML += "</a>";
			}
			outputHTML += "</div></td>";
			outputHTML += "</tr>"
		} else {
			if ((primeElement) && (i != 0)) {
				//outputHTML += "<tr><td class=\"" + this.menuName + "MenuStyle\"><img src=\"images/spacer.gif\" height=\"6\" width=\"" + this.menuWidth + "\" border=\"0\"></td></tr>";
			}
			outputHTML += "<tr><td height=\"25\" " + this.textAlign + "id=\"" + this.menuName + "MenuItem" + i + "\" background='images/menuBackground.jpg'>" + indent;
			if (this.menuItems.menuItemAt(i).href != "") {
				outputHTML += "<a href=\"" + this.menuItems.menuItemAt(i).href + "\" onMouseOver=\"" + this.menuName + ".highlightItem(\'" + this.menuName + "MenuItem" + i + "\',\'on\');\" onMouseOut=\"" + this.menuName + ".highlightItem(\'" + this.menuName + "MenuItem" + i + "\',\'off\');\" class=\"" + this.textStyle + "\">"
			}
			if (this.menuItems.menuItemAt(i).href != "") {
				outputHTML += this.menuItems.menuItemAt(i).displayName + rightSpace + "<br>";
			} else {
				outputHTML += "<span class=\"" + this.textStyle + "\">" + this.menuItems.menuItemAt(i).displayName + rightSpace + "<br></span>";
			}
			if (this.menuItems.menuItemAt(i).href != "") {
				outputHTML += "</a>";
			}
			outputHTML += "</td></tr>";
		}
	}
	if (window.document.layers) {
		outputHTML += "<tr><td><img src=\"images/spacer.gif\" height=\"13\" width=\"" + this.menuWidth + "\" border=\"0\"></td></tr>";
		outputHTML += "</table>";
	} else {
		outputHTML += "<tr><td class=\"" + this.menuName + "MenuStyle\"><img src=\"images/spacer.gif\" height=\"13\" width=\"" + this.menuWidth + "\" border=\"0\"></td></tr>";
		outputHTML += "</table>";
	}
 	outputHTML += "</div>";
	
	outputHTML += "<div id=\"" + this.menuName + "Trigger\">";
	outputHTML += "<a href=\"JavaScript://\" onMouseOver=\"" + this.menuName + ".setVisible(false);\" onMouseOut=\"" + this.menuName + ".setVisible(false);\"><img src=\"images/spacer.gif\" height=\"500\" width=\"" + (this.menuWidth + 80) + "\" border=\"0\"></a>"
  	outputHTML += "</div>";
 
	this.html = outputHTML;
	return document.write(this.html);
}

MenuEngine.prototype.toString = function() {
	menuParams = "MENU PARAMETERS \r";
	menuParams += "menuLeft: " + this.menuLeft + ";\r";
	menuParams += "menuTop: " + this.menuTop + ";\r";
	menuParams += "menuWidth: " + this.menuWidth + ";\r";
	menuParams += "menuZI: " + this.menuZI + ";\r";
	menuParams += "menuInitHex: " + this.menuInitHex + ";\r";
	menuParams += "menuActiveHex: " + this.menuActiveHex + ";\r";
	menuParams += "menuInitOpacity: " + this.menuInitOpacity + ";\r";
	menuParams += "menuActiveOpacity: " + this.menuActiveOpacity + ";\r";
	menuParams += "\r";
	
	outputStyle = "OUTPUT STYLE \r";
	outputStyle += this.style + "\r";
	outputStyle += "\r";
	
	outputHTML = "OUTPUT HTML \r";
	outputHTML += this.html + "\r";
	outputHTML += "\r";
	
	toStringOutput = (menuParams + outputStyle + outputHTML)
	return toStringOutput;
}

MenuEngine.prototype.setVisible = function(bool) {
	div = this.menuName + "Menu";
	divTrigger = this.menuName + "Trigger";
	
	if (window.document.layers) {
		eval('document.' + div + '.visibility = (bool) ? "show" : "hide"');
		eval('document.' + divTrigger + '.visibility = (bool) ? "show" : "hide"');
		if (typeof(document.menuSafeDiv) != "undefined") {
			document.menuSafeDiv.visibility = (!bool) ? "visible" : "hidden";
		}
	} else {
		eval('document.all.' + div + '.style.visibility = (bool) ? "visible" : "hidden"');
		eval('document.all.' + divTrigger + '.style.visibility = (bool) ? "visible" : "hidden"');
		if (typeof(document.all.menuSafeDiv) != "undefined") {
			document.all.menuSafeDiv.style.visibility = (!bool) ? "visible" : "hidden";
		}
	}
}


MenuEngine.prototype.addMenuItem = function(pNavLevel, pDisplayName, pHREF) {
	mi = new MenuItem(pNavLevel, pDisplayName, pHREF);
	this.menuItems.addMenuItem(mi);
}

function MenuItemCollection() {
	this.menuItems = new Array();
}

MenuItemCollection.prototype.addMenuItem = function(pMenuItem) {
	this.menuItems[this.menuItems.length++] = pMenuItem;
}
  
MenuItemCollection.prototype.menuItemAt = function(index) {
	return (index >= 0 && index < this.menuItems.length) ? this.menuItems[index] : null;
}

MenuItemCollection.prototype.getLength = function() {
	return this.menuItems.length;
}

MenuItemCollection.prototype.toString = function() {
	return "[object MenuItemCollection]";
}

function MenuItem(pNavLevel, pDisplayName, pHREF) {
	this.level = pNavLevel; // int nav level (0 product item, 1 main item, 2 sub item)
	this.displayName = pDisplayName;
	this.href = pHREF; // empty string for no link
}


