function Menu(id, parentID, name, type, url, array)
{
	this.id = id;
	this.parentID = parentID;
	this.name = name;
	this.type = type;
	this.url = url;
	this.subArray = (type == "sub") ? array : null;
	this.show = false;
	
	if (typeof(allMenus) != "object") {
		allMenus = new Array();
	}
	allMenus[id] = this;
}

function write_menu_html()
{
	var e;
	var s = TABLE_OPEN_HTML + _menu_html(menuArray, 0, 0) + "</table>";
	if (document.getElementById) {
		if (e = document.getElementById('menuDiv')) {
			e.innerHTML = s;
		}
	}
	else if (document.all && document.all.menuDiv && document.all.menuDiv.innerHTML) {
		document.all.menuDiv.innerHTML = s;
	}
}


function _menu_html(mArray, id, indentLevel)
{
	var s = "";
	var indentStr = "";
	for (var i = 0; i < indentLevel; i++) {
		indentStr += INDENT_ADD;
	}

	for (x in mArray) {
		var m = mArray[x];
		g = currentOpen == id ? "G" : "";
		// Custom forgecraft start
		if (m.id==0) {
	 		s += "<tr><td width=\"100%\" background=\"" + IMG_URL + "/button" + g + ".jpg\" height=\"20\" class=\"menubutton\"><img src=\"product_images/spacer.gif\" width=25 height=1 border=0>";
		}
		else {
	 		s += "<tr><td width=\"100%\" background=\"" + IMG_URL + "/button" + g + ".jpg\" height=\"20\" class=\"menubutton\">" + MENU_INITIAL_INDENT;
		}
		// Custom forgecraft end
		s += indentStr;
		if (m.type == "url") {
			s += "<a href=\"" + add_query_variable(m.url, "parentMenuID", id) + "\" class=\"menubutton\" >" + m.name + "</a>";
		}
		else {
			// type = "sub"
			s += "<a href=\"" + add_query_variable(m.url, "parentMenuID", m.id) + "\" class=\"menubutton\" >" + m.name + "<b>&raquo;</b></a>";
//			s += "<a href=\"javascript:menu_click(" + m.id + ");\" class=\"menubutton\">" + m.name + "&nbsp;<b>&raquo;</b></a>";
		}
		s += "</td><td width=\"9\" height=\"20\"><img src=\"" + IMG_URL + "/buttonright" + g + ".jpg\" width=9 height=20 alt=\"\" border=\"0\" /></td></tr>";
		if (m.type == "sub" && m.show) {
			s += _menu_html(m.subArray, m.id, indentLevel + 1);
		}
	}
	return s;
}


function open_menu(menuID)
{
	currentOpen = menuID;
	_show_menu(menuID);
}


function open_menu_from_query(defaultID)
{
	qID = parseInt(get_query_variable("parentMenuID"));
	if (qID) {
		open_menu(qID);
	}
	else {
		open_menu(defaultID);
	}
}


function close_menu(menuID)
{
	if (allMenus[menuID].show && allMenus[menuID].type == "sub") {
		for (m in allMenus[menuID].subArray) {
			close_menu(allMenus[menuID].subArray[m].id);
		}
	}
	allMenus[menuID].show = false;
}


function switch_menu(menuID)
{
	if (allMenus[menuID].show) {
		close_menu(menuID);
	}
	else {
		_close_all();
		open_menu(menuID);
	}
	write_menu_html();
}


function open_only_menu(menuID)
{
	_close_all();
	open_menu(menuID);
	write_menu_html();
}


function _close_all()
{
	for (m in menuArray) {
		close_menu(menuArray[m].id);
	}
}	

function _show_menu(menuID)
{
	allMenus[menuID].show = true;
	var parentID;
	if (parentID = allMenus[menuID].parentID) {
		_show_menu(parentID);
	}
}
