
// ***************************************************************************
// DropMenu class - Bjjörn Morén, WM-data eSolutions 2001.
//
// - Handles dropdown menus with tree-structures
//
// ***************************************************************************


// ---------------------------------------------------------------------------
// Constructor
function DropMenu(
		menuList,						// List of menu items
		layerObj,						// Layer object
		isAutoClose)					// If true, closes all open nodes when a new node is opened
{
	this.menuList = menuList;
	this.layerObj = layerObj;
	this.menu = null;
	this.rootId = 0;
	this.currentId = 0;
	this.currentDropId = 0;
	this.isAutoClose = isAutoClose;

	this.click = DropMenu_click;
	this.drawNode = DropMenu_drawNode;
	this.parseMenu = DropMenu_parseMenu;
}


// ---------------------------------------------------------------------------
// Handles a click in a menu
function DropMenu_click(nodeId) {
	if (!this.menu) this.parseMenu();
	var objNode = this.menu[nodeId];
	if (!objNode) return;

	// Optionally close all open nodes
	var oldNodeState = objNode.isOpen;
	if (this.isAutoClose) for (var i in this.menu) this.menu[i].isOpen = false;

	// Trace path
	var parentId = nodeId;
	var dropId = nodeId;
	for (var i = 0; i < 100; i++) {
		if (!this.menu[parentId].parent) break;
		dropId = parentId;
		parentId = this.menu[parentId].parent;
		this.menu[parentId].isOpen = true;
	}
	
	objNode.isOpen = !oldNodeState;

	// Swap states
	if ((this.currentDropId) && (this.currentDropId != dropId)) this.menu[this.currentDropId].isOpen = false;
	//if (this.currentDropId == nodeId) this.menu[this.currentDropId].isOpen = false;
	
	
	if (!this.menu[dropId].isOpen) {
		this.layerObj.setVisibility(false);
		this.currentId = 0;
		this.currentDropId = 0;
	}
	else {
		// Draw menu
		var sHtml="";
		var menuBgColor = "#ffffcc";
		var menuWidth = "150";
		
		//if (document.layers) sHtml += '<link rel="stylesheet" href="include/OvakoStyle.css" type="text/css">\n';

		sHtml += "<table width='" +menuWidth +"' bgcolor='" +menuBgColor +"' border='0'><tr><td><table>\n";
		sHtml += this.drawNode(dropId, 0);
		sHtml += "</table></td></tr></table>\n";
		this.layerObj.setContent(sHtml);

		// Place menu
		var menuPos = getAnchorPos("menuAnchor" + dropId);
		this.layerObj.moveTo(menuPos.x, menuPos.y+19, true);

		this.currentId = nodeId;
		this.currentDropId = dropId;
	}
}


// ---------------------------------------------------------------------------
// Recursively draw all children for a node
function DropMenu_drawNode(nodeId, depth) {

	var s = "";
	var indent = 10;
	var pObj = this.menu[nodeId];
	for (var i=0; i < pObj.children.length; i++) {
		var obj = this.menu[pObj.children[i]];

		if (obj.children.length > 0) {		// Folder
			if (obj.isOpen) {
				s+="<tr><td><div class='mainmenuDep' style='margin-left:" + (depth*indent) + 
					"px;'><a href=\"javascript:menuClick('" + obj.id + "');\">-&nbsp;" + obj.name + "</a></div></td></tr>\n";
				s+= this.drawNode(obj.id, depth + 1);
			}
			else {
				s+="<tr><td><div class='mainmenuDep' style='margin-left:" + (depth*indent) + 
					"px;'><a href=\"javascript:menuClick('" + obj.id + "');\">+&nbsp;" + obj.name + "</a></div></td></tr>\n";
			}
		}
		else {								// Item
			s+="<tr><td><div class='mainmenuDep' style='margin-left:" + (depth*indent) + 
				"px;'><a href=\"index.asp?r=" + obj.id + "\">&#149;&nbsp;" + obj.name + "</a></div></td></tr>\n";
		}
	}
	return s;
}


// ---------------------------------------------------------------------------
// Reconstruct the menu into a new structure
function DropMenu_parseMenu() {
	this.menu = new Array();
	for (var i=0; i < this.menuList.length/4; i++) {
		var newObj = new Array();
		newObj.id = this.menuList[i*4+0];
		newObj.parent = this.menuList[i*4+1];
		newObj.type = this.menuList[i*4+2];
		newObj.name = this.menuList[i*4+3];
		newObj.isOpen = false;
		newObj.children = new Array();
		this.menu[newObj.id] = newObj;
		if (newObj.parent) {
			var parentObj = this.menu[newObj.parent];
			parentObj.children[parentObj.children.length] = newObj.id;
		}
		else this.rootId = newObj.id;
	}
}