/*
Author:		Topper Lu
Date:		2001-06-01
Version:	1.0
Require:	core.js
*/

//--- MenuNode ---//
function MenuNode(index, parent, key, description, href, target, data) {
	this.index = index;
	this.parent = parent || null;
	this.key = key || null;
	this.description = description || '';
	this.href = href || null;
	this.target = target || null;
	this.data = data || null;
	this.children = null;
	if (this.parent != null) {
		if (this.parent.children == null) this.parent.children = new Array();
		this.parent.children[this.parent.children.length] = this;
	}
	this.popup = null;
	this.container = null;
}

MenuNode.prototype.isLeaf = function() {
	return this.children == null;
}

MenuNode.prototype.isRoot = function() {
	return this.parent == null;
}

MenuNode.prototype.isLastChild = function() {
	return this.parent != null ? this.parent.children[this.parent.children.length-1] == this : true;
}

MenuNode.prototype.isNodeAncestor = function(anotherNode) {
	if (!anotherNode || anotherNode == this) return false;
	var ancestor = this.parent;
	while (ancestor != null) {
		if (ancestor == anotherNode) return true;
		ancestor = ancestor.parent;
	}
	return false;
}

MenuNode.prototype.isNodeDescendant = function(anotherNode) {
	if (!anotherNode || anotherNode == this) return false;
	return anotherNode.isNodeAncestor(this);
}

MenuNode.prototype.getLevel = function() {
	var ancestor = this;
	var level = 0;
	while ((ancestor = ancestor.parent) != null) {
		level++;
	}
	return level;
}

MenuNode.prototype.getPathToRoot = function() {
	var level = this.getLevel();
	var nodes = new Array();
	var node = this;
	for (var i = level; i >= 0; i--) {
		nodes[i] = node;
		node = node.parent;
	}
	return nodes;
}

//--- MenuTree ---//
function MenuTree(allMenuNodes, x0, y0, barItemWidth, barItemHeight, popupItemWidth, popupItemHeight) {
	this.allMenuNodes = allMenuNodes || null;
	this.x0 = x0 || 0;
	this.y0 = y0 || 0;
	this.barItemWidth = barItemWidth || 100;
	this.barItemHeight = barItemHeight || 24;
	this.popupItemWidth = popupItemWidth || 120;
	this.popupItemHeight = popupItemHeight || 24;
	//--- configuration datas ---//
	this.barItemBackgroundColorNormal = '#cccc99';
	this.barItemBackgroundColorHilight = '#aaaa77';
	this.barItemFontColorNormal = '#000000';
	this.barItemFontColorHilight = '#000000';
	this.popupItemBackgroundColorNormal = '#9999bb';
	this.popupItemBackgroundColorHilight = '#bbbbdd';
	this.popupItemFontColorNormal = '#000000';
	this.popupItemFontColorHilight = '#000000';
	this.menuBarStyle = 'border-width:0px;margin:0px;padding:0px;';
	this.popupStyle = 'border-width:0px;margin:0px;padding:0px;';
	this.itemStyle = 'margin:0px;cursor:pointer;cursor:hand;font-family:Arial,Helvetica,sans-serif;';
	this.barItemStyle = 'vertical-align:middle;text-align:center;padding:0px;border-style:solid;border-width:1px;border-color:#eeeeee #777777 #777777 #eeeeee;';
	this.popupItemStyle = 'vertical-align:middle;text-align:center;padding:0px;border-style:solid;border-width:1px;border-color:#eeeeee #777777 #777777 #eeeeee;';
	this.fontSize = 12;
	this.zIndex = 100;
	this.timeout = 200;
	//--- do not change datas below ---//
	this.oldPath = null;
	this.currPath = null;
	this.menuBar = null;
	this.indexTree = MenuTree._Instances.length;
	MenuTree._Instances[MenuTree._Instances.length] = this;
}

MenuTree.prototype.drawMenuBar = function() {
	if (!this.allMenuNodes) {
		return;
	}
	var menuNode = this.allMenuNodes[0];
	if (!menuNode.children) {
		return;
	}
	var w = this.barItemWidth*menuNode.children.length;
	var h = this.barItemHeight;
	document.write('<div id="menuBar_'+this.indexTree+'" style="position:absolute;left:'+this.x0+'px;top:'+this.y0+'px;width:'+w+'px;height:'+h+'px;z-index:'+this.zIndex+';'+this.menuBarStyle+'">');
	for (var i = 0; i < menuNode.children.length; i++) {
		this.drawBarItem(this.barItemWidth*i, 0, menuNode.children[i]);
	}
	document.write('</div>');
	this.menuBar = getObj('menuBar_'+this.indexTree, document);
	for (var i = 0; i < menuNode.children.length; i++) {
		var child = menuNode.children[i];
		if (child.children != null) {
			var x = this.x0 + this.barItemWidth*i;
			var y = this.y0 + this.barItemHeight;
			if ((x+this.popupItemWidth) > document.body.clientWidth) {
				x += this.barItemWidth - this.popupItemWidth;
			}
			this.drawPopupMenu(x, y, child);
		}
	}
}

MenuTree.prototype.drawPopupMenu = function(x0, y0, menuNode) {
	var w = this.popupItemWidth;
	var h = this.popupItemHeight*menuNode.children.length;
	document.write('<div id="popup_'+this.indexTree+'_'+menuNode.index+'" style="position:absolute;left:'+x0+'px;top:'+y0+'px;width:'+w+'px;height:'+h+'px;visibility:hidden;z-index:'+this.zIndex+';'+this.popupStyle+'">');
	for (var i = 0; i < menuNode.children.length; i++) {
		this.drawPopupItem(0, this.popupItemHeight*i, menuNode.children[i]);
	}
	document.write('</div>');
	menuNode.popup = getObj('popup_'+this.indexTree+'_'+menuNode.index, document);
	for (var i = 0; i < menuNode.children.length; i++) {
		var child = menuNode.children[i];
		if (child.children != null) {
			var x = x0 + this.popupItemWidth;
			var y = y0 + this.popupItemHeight*i;
			if ((x+this.popupItemWidth) > document.body.clientWidth) {
				x = x0 - this.popupItemWidth;
			}
			this.drawPopupMenu(x, y, child);
		}
	}
}

MenuTree.prototype.drawBarItem = function(x, y, menuNode) {
	var w = this.barItemWidth;
	var h = this.barItemHeight;
	if (isIE) {
		h -= 2;
	} else {
		w -= 2;
		h -= 2;
	}
	document.write('<div id="container_'+this.indexTree+'_'+menuNode.index+'" style="position:absolute;left:'+x+'px;top:'+y+'px;width:'+w+'px;height:'+h+'px;'+this.itemStyle+this.barItemStyle+'" onMouseOver="MenuTree.itemMouseOver('+this.indexTree+','+menuNode.index+');" onMouseOut="MenuTree.itemMouseOut('+this.indexTree+','+menuNode.index+');" onClick="MenuTree.itemClick('+this.indexTree+','+menuNode.index+');">');
	document.write(menuNode.description);
	document.write('</div>');
	menuNode.container = getObj('container_'+this.indexTree+'_'+menuNode.index, document);
	menuNode.container.style.backgroundColor = this.barItemBackgroundColorNormal;
	menuNode.container.style.color = this.barItemFontColorNormal;
	menuNode.container.style.fontSize = this.fontSize+'px';//lineHeight
	menuNode.container.style.lineHeight = h + 'px';
}

MenuTree.prototype.drawPopupItem = function(x, y, menuNode) {
	var w = this.popupItemWidth;
	var h = this.popupItemHeight;
	if (isIE) {
		h -= 2;
	} else {
		w -= 2;
		h -= 2;
	}
	document.write('<div id="container_'+this.indexTree+'_'+menuNode.index+'" style="position:absolute;left:'+x+'px;top:'+y+'px;width:'+w+'px;height:'+h+'px;'+this.itemStyle+this.popupItemStyle+'" onMouseOver="MenuTree.itemMouseOver('+this.indexTree+','+menuNode.index+');" onMouseOut="MenuTree.itemMouseOut('+this.indexTree+','+menuNode.index+');" onClick="MenuTree.itemClick('+this.indexTree+','+menuNode.index+');">');
	document.write(menuNode.description);
	if (menuNode.children) {
		document.write('<img src="/tools/images/rightward.gif" style="position:absolute;left:'+(this.popupItemWidth-18)+'px;top:'+((this.popupItemHeight-16)/2)+'px;">');
	}
	document.write('</div>');
	menuNode.container = getObj('container_'+this.indexTree+'_'+menuNode.index, document);
	menuNode.container.style.backgroundColor = this.popupItemBackgroundColorNormal;
	menuNode.container.style.color = this.popupItemFontColorNormal;
	menuNode.container.style.fontSize = this.fontSize+'px';
	menuNode.container.style.lineHeight = h + 'px';
}

MenuTree.prototype.itemClick = function(index) {
	var menuNode = this.allMenuNodes[index];
	if (menuNode.target && menuNode.href) {
		window.open(menuNode.href, menuNode.target);
		this.currPath = null;
		this.checkPath();
	}
}

MenuTree.prototype.itemMouseOver = function(index) {
	var menuNode = this.allMenuNodes[index];
	if (menuNode.parent.parent) {
		menuNode.container.style.backgroundColor = this.popupItemBackgroundColorHilight;
		menuNode.container.style.color = this.popupItemFontColorHilight;
	} else {
		menuNode.container.style.backgroundColor = this.barItemBackgroundColorHilight;
		menuNode.container.style.color = this.barItemFontColorHilight;
	}
	this.currPath = menuNode.getPathToRoot();
	this.checkPath();
}

MenuTree.prototype.itemMouseOut = function(index) {
	var menuNode = this.allMenuNodes[index];
	if (menuNode.parent.parent) {
		menuNode.container.style.backgroundColor = this.popupItemBackgroundColorNormal;
		menuNode.container.style.color = this.popupItemFontColorNormal;
	} else {
		menuNode.container.style.backgroundColor = this.barItemBackgroundColorNormal;
		menuNode.container.style.color = this.barItemFontColorNormal;
	}
	this.currPath = null;
	window.setTimeout('MenuTree.checkPath('+this.indexTree+')', this.timeout);
}

MenuTree.prototype.checkPath = function() {
	var newPath = this.currPath;
	var oldPath = this.oldPath;
	if (newPath == oldPath) {
		return;
	}
	var newLen = newPath ? newPath.length : 0;
	var oldLen = oldPath ? oldPath.length : 0;
	var minLen = newLen < oldLen ? newLen : oldLen;
	var commonLen = 0;
	while (commonLen < minLen) {
		if (oldPath[commonLen].index != newPath[commonLen].index) {
			break;
		}
		commonLen++;
	}
	for (var i = commonLen; i < oldLen; i++) {
		if (oldPath[i].popup) {
			oldPath[i].popup.style.visibility = 'hidden';
		}
	}
	for (var i = commonLen; i < newLen; i++) {
		if (newPath[i].popup) {
			newPath[i].popup.style.visibility = 'visible';
		}
	}
	this.oldPath = this.currPath;
}

MenuTree._Instances = new Array();

MenuTree.itemClick = function(indexTree, index) {
	MenuTree._Instances[indexTree].itemClick(index);
}

MenuTree.itemMouseOver = function(indexTree, index) {
	MenuTree._Instances[indexTree].itemMouseOver(index);
}

MenuTree.itemMouseOut = function(indexTree, index) {
	MenuTree._Instances[indexTree].itemMouseOut(index);
}

MenuTree.checkPath = function(indexTree) {
	MenuTree._Instances[indexTree].checkPath();
}

MenuTree.drawPopupMenus = function(indexTree) {
	MenuTree._Instances[indexTree].drawPopupMenus();
}
