var sCurrentOpen = "";
var iCurrentMenu = "";


function MenuSet() {
	this.menus = new Array();
	this.preloads = new Array();
}

function _MenuSet_Add(sKey, sRoot, sParent, sCaption, sAction, sPage) {
	var iIndex
	this.menus[sKey] = new MenuItem(sKey, sRoot, sParent, sCaption, sAction, sPage)
	iIndex = this.preloads.length;
	this.preloads[iIndex] = new Image()
	this.preloads[iIndex].src = "images/menus/" + sRoot + "-1.png"
	iIndex = this.preloads.length;
	this.preloads[iIndex] = new Image()
	this.preloads[iIndex].src = "images/menus/" + sRoot + "-2.png"
}
MenuSet.prototype.add = _MenuSet_Add;

function _MenuSet_Draw(iNewCurrent) {
	if (iNewCurrent) iCurrentMenu = iNewCurrent;
	var sBuffer = ""
	for (var a in this.menus) {
		if (this.menus[a].parent == "") {
			sBuffer += this.menus[a].drawmain()
		}
		else if(this.menus[a].parent == sCurrentOpen) {
			sBuffer += this.menus[a].drawsub()
		}
	}
	setInnerHTML("menusec", sBuffer)
}
MenuSet.prototype.draw = _MenuSet_Draw;

function MenuItem(sKey, sRoot, sParent, sCaption, sAction, sPage) {
	this.key     = sKey;
	this.root    = sRoot;
	this.parent  = sParent;
	this.caption = sCaption;
	this.action  = sAction;
	this.page    = sPage;
}

function _MenuItem_DrawMain() {
	var sBuffer = "<div><img src='images/menu/" + this.root
	var sTitle = ""
	if (this.caption != "") sTitle = " title=\"" + this.caption + "\""
	if (this.key == iCurrentMenu) {
		sBuffer += "-2.png' class='link'"
		sBuffer += "onmouseover=\"hilite(this, 'images/menu/" + this.root + "-2.png')\" "
		sBuffer += "onmouseout=\"hilite(this, 'images/menu/" + this.root + "-2.png')\" "
	}
	else if (this.root == sCurrentOpen) {
		sBuffer += "-1.png' class='link'"
		sBuffer += "onmouseover=\"hilite(this, 'images/menu/" + this.root + "-2.png')\" "
		sBuffer += "onmouseout=\"hilite(this, 'images/menu/" + this.root + "-1.png')\" "
	}
	else {
		sBuffer += "-0.png' class='link' "
		sBuffer += "onmouseover=\"hilite(this, 'images/menu/" + this.root + "-1.png')\" "
		sBuffer += "onmouseout=\"hilite(this, 'images/menu/" + this.root + "-0.png')\" "
	}
	sBuffer += "onmouseup=\"menuOpen('" + this.root + "', '" + this.key + "')\"" + sTitle + ">"
	sBuffer += "</div>"
	return sBuffer;
}
MenuItem.prototype.drawmain = _MenuItem_DrawMain;


function _MenuItem_DrawSub() {
	var sBuffer = "<div><img src='images/menu/" + this.root
	var sTitle = ""
	if (this.caption != "") sTitle = " title=\"" + this.caption + "\""
	if (this.key == iCurrentMenu) {
		sBuffer += "-2.png' class='link' "
		sBuffer += "onmouseup=\"menuSelect('" + this.key + "')\"" + sTitle + ">"
		sBuffer += "</div>"
	}
	else {
		sBuffer += "-0.png' class='link' "
		sBuffer += "onmouseover=\"hilite(this, 'images/menu/" + this.root + "-1.png')\" "
		sBuffer += "onmouseout=\"hilite(this, 'images/menu/" + this.root + "-0.png')\" "
		sBuffer += "onmouseup=\"menuSelect('" + this.key + "')\"" + sTitle + ">"
		sBuffer += "</div>"
	}
	return sBuffer;
}
MenuItem.prototype.drawsub = _MenuItem_DrawSub;

// ==================================
// HANDLERS
// ==================================
function hilite(obj, src) {
	obj.src = src
}

function menuOpen(sOpen, sKey) {
	sCurrentOpen = sOpen;
	iCurrentMenu = sKey
	oMenu.draw()
	ProcessMenuClick()
}

function menuClose() {
	sCurrentOpen = "";
	iCurrentMenu = -1
	oMenu.draw()
	ProcessMenuClick()
}


function menuSelect(sKey) {
	oMenu.draw(sKey)
	ProcessMenuClick()
}


function ProcessMenuClick() {
	if (iCurrentMenu < 0) return
	obj = oMenu.menus[iCurrentMenu]
//	alert(obj.action + "\n" + obj.page)
	if (obj.action == "") return;
	switch (obj.action) {
		case "ECHO":	
			top.main.location = "/echo.asp?root=" + obj.root + "&action=" + obj.action + "&ksy=" + iCurrentMenu
			break;
		case "URL":
			top.main.location = String(obj.page)
			break;
	}
}

