// *****************************
// Dynamic HTML Popout Menus
// REQUIRES "dhtml_api_v4.js"
// *****************************
// (c) 2006 Jason Christy - www.jchristy.com
// *****************************

//Global Functions/Objects
function popStyleObj(iINDENT, iPAD, iSPACE, sTBG, sCBGA, sCBGB, sSTYLEA, sSTYLEB) {
	this.indent = iINDENT;
	this.padding = iPAD;
	this.spacing = iSPACE;
	this.tableBG = sTBG;
	this.cellBG = sCBGA;
	this.overBG = sCBGB;
	
	this.styleA = sSTYLEA;
	this.styleB = sSTYLEB;
}
function addPopMenu(sID, iX, iY, iW, iH, iZ, oPOPSTYLEOBJ) {
	popMenu[sID] = new popMenuObj(sID, iX, iY, iW, iH, iZ, oPOPSTYLEOBJ);
}
function addPopMenuItem(sMenuID, sID, sLABEL, sURL, sTYPE, sSTYLEA, sSTYLEB, sTARGET) {
	popMenu[sMenuID].addMenuItem(sID, sLABEL, sURL, sTYPE, sSTYLEA, sSTYLEB, sTARGET);
}
function setPopMenuSub(sMENU, sMENUITEM, sSUBMENU) {
	popMenu[sMENU].menuItem[sMENUITEM].setSub(popMenu[sSUBMENU]);
}
function initPopMenu(oWINDOW, DEBUG) {
	for (var m in popMenu) {
		if (!popMenu[m].init) { 
			popMenu[m].createLayer(oWINDOW, DEBUG);	
			popMenu[m].init = true;
		}
	}
}
function showPopMenu(sMENU) {
	for (var m in popMenu) {
			popMenu[m].hide();
			popMenu[m].clearHideTimer();
	}
	popMenu[sMENU].show();
	popMenu[sMENU].clearHideTimer();
}
function hidePopMenu(sMENU) {
	popMenu[sMENU].setHideTimer();
}
function showSubMenu(sMENU) {
	window.clearTimeout(popMenu[sMENU].subTimer);
	popMenu[sMENU].subTimer = window.setTimeout("showPopMenu('" + sMENU + "');", popSubTimeout);	
}
function hideSubMenu(sMENU) {
	window.clearTimeout(popMenu[sMENU].subTimer);
	popMenu[sMENU].subTimer = window.setTimeout("hidePopMenu('" + sMENU + "');", popSubTimeout);	
}
function closeAllMenus() {
	for (var m in popMenu) {
		popMenu[m].forceHide();
	}	
}
function popMoveMenu(sMENU, sDIR) {
	var layer = popMenu[sMENU].layer;
	switch (sDIR) {
		case "UP" :
			layer.moveBy(0, popMoveInc * -1);
			break;
		case "DOWN" :
			layer.moveBy(0, popMoveInc);
			break;
		case "LEFT" :
			layer.moveBy(popMoveInc * -1, 0);
			break;
		case "RIGHT" :
			layer.moveBy(popMoveInc, 0);
			break;
	}
	window.status = "x: " + layer.getX() + ", y: " + layer.getY();
}
function setMoveInc(iINC) {
	popMoveInc = iINC;
}
//Global Variables
var popDebug = false;
var popMenu = new Object();
var popHideTimeout = 400;
var popActiveTimeout = 100;
var popSubTimeout = 100;
var popBaseZIndex = 1;
var popMoveInc = 1;
var popDefaultStyle = new popStyleObj(6, 3, 1, "#000000", "#FFFFFF", "#CCCCCC");
//Menu Objects
function popMenuObj(sID, iX, iY, iW, iH, iZ, oPOPSTYLEOBJ) {
		this.parent = this;
		this.id = sID;
		this.x = iX;
		this.y = iY;
		this.width = iW;
		this.height = iH;
		this.zIndex = (iZ) ? iZ : popBaseZIndex;
		this.styleObj = (oPOPSTYLEOBJ) ? oPOPSTYLEOBJ : popDefaultStyle;
		
		this.menuItem = new Object();
		this.hideTimer;
		this.activeTimer;
		this.subTimer;
		this.isActive = false;
		this.isOpen = false;
		this.openSub;
		this.init = false;
}
popMenuObj.prototype.addMenuItem = function(sID, sLABEL, sURL, sTYPE, sSTYLEA, sSTYLEB, sTARGET) {
		var styleA = (sSTYLEA) ? sSTYLEA : this.styleObj.styleA;
		var styleB = (sSTYLEB) ? sSTYLEB : this.styleObj.styleB;
		this.menuItem[sID] = new menuItemObj(this, sID, sLABEL, sURL, sTYPE, styleA, styleB, sTARGET);
}
popMenuObj.prototype.setActive = function(bSTATE) {
	window.clearTimeout(this.activeTimer);
	if (bSTATE) { 
		this.isActive = true; 
	}
	else {
		this.activeTimer = window.setTimeout("popMenu." + this.id + ".isActive = false", popActiveTimeout);
	}
}
popMenuObj.prototype.setHideTimer = function() {
	this.clearHideTimer();
	this.hideTimer = window.setTimeout("popMenu." + this.id + ".hide();", popHideTimeout);
}
popMenuObj.prototype.clearHideTimer = function() {
	window.clearTimeout(this.hideTimer);
}
popMenuObj.prototype.createLayer = function(oWINDOW, DEBUG) {
		this.layer = new dynLayer(oWINDOW, this.id + "Layer", this.x, this.y, this.width, this.height, this.zIndex, DEBUG);
		this.layer.custEvents = "onmouseover='popMenu." + this.id + ".setActive(true); popMenu." + this.id + ".clearHideTimer();' onmouseout='popMenu." + this.id + ".setActive(false); popMenu." + this.id + ".setHideTimer();'";
		this.layer.outputStyle(false, DEBUG);
		this.layer.outputLayer(this.genHTML());
		this.layer.init();
}

popMenuObj.prototype.genHTML = function() {
		var html =  '<table width="' + this.width + '" border="0" cellpadding="' + this.styleObj.padding + '" cellspacing="' + this.styleObj.spacing + '" bgcolor="' + this.styleObj.tableBG + '">\n';
		for (var i in this.menuItem) {
			var itemStyle = (this.menuItem[i].style) ? this.menuItem[i].style : "none";
			var overClass = (this.menuItem[i].overStyle) ? this.menuItem[i].overStyle : itemStyle;
			var overBackground = (this.styleObj.overBG) ? this.styleObj.overBG : this.styleObj.cellBG;
			var mouseOverCode = "this.className=\'" + overClass + "\'; this.style.backgroundColor=\'" + overBackground + "\'; this.style.cursor=\'pointer\';"; 
			var mouseOutCode = "this.className=\'" + itemStyle + "\'; this.style.backgroundColor=\'" + this.styleObj.cellBG + "\'; this.style.cursor=\'default\';"; 
			var clickCode;
			switch (this.menuItem[i].type) {
				case "URL" :
					clickCode = "window.location='" + this.menuItem[i].url + "';";
					break;
				case "SCRIPT" :
					if (this.menuItem[i].url.charAt(this.menuItem[i].url.length - 1) != ";") { this.menuItem[i].url += ";" };
					clickCode = this.menuItem[i].url;
					break;
				case "NEW" :
					clickCode = "window.open('" + this.menuItem[i].url + "');";
					break;
			}
			clickCode += " closeAllMenus();"
			var subMenuGlyph = "&nbsp;";
			
			if (this.menuItem[i].hasSub) {
				subMenuGlyph = "&raquo;";
				clickCode = "void 0;";
				mouseOverCode += " showSubMenu(\'" + this.menuItem[i].subMenu.id + "\');";
				mouseOutCode += " hideSubMenu(\'" + this.menuItem[i].subMenu.id + "\');";
			}
			
			var itemHTML =  '<table border="0" cellpadding="0" cellspacing="0" width="100%">\n';
				itemHTML += '<tr>\n';
				itemHTML += '<td>' + this.menuItem[i].label + '</td>\n';
				itemHTML += '<td align="right">' + subMenuGlyph + '</td>\n';
				itemHTML += '</tr>\n';
				itemHTML += '</table>\n';	
			
			html += '<tr >\n';
			html += '<td class="' + itemStyle + '" style="padding-left:' + this.styleObj.indent + 'px;" onclick="' + clickCode + '" onmouseover="' + mouseOverCode + '" onmouseout="' + mouseOutCode + '" bgcolor="' + this.styleObj.cellBG + '">' + itemHTML + '</td>\n';
			html += '</tr>\n';
		}
		html += '</table>';
		
		if (popDebug) {
			html += '\n';
			html += '<table width="' + this.width + '" cellpadding="0" cellspacing="0" border="0" bgcolor="#CCCCCC">\n';
			html += '<tr>\n';
			html += '<td style="font-family: Verdana; font-size: 9px; color: #FFFFFF;" colspan="2">' + this.id + '</td>';
			html += '</tr>\n';
			html += '<tr>\n';
			html += '<td style="font-family: Webdings; font-size: 9px;" align="left"><a href="javascript:popMoveMenu(\'' + this.id + '\',\'LEFT\');" style="text-decoration:none">3</a>&nbsp;&nbsp;&nbsp;<a href="javascript:popMoveMenu(\'' + this.id + '\',\'RIGHT\');" style="text-decoration:none">4</a>&nbsp;&nbsp;&nbsp;<a href="javascript:popMoveMenu(\'' + this.id + '\',\'UP\');" style="text-decoration:none">5</a>&nbsp;&nbsp;&nbsp;<a href="javascript:popMoveMenu(\'' + this.id + '\',\'DOWN\');" style="text-decoration:none">6</a></td>\n';
			html += '<td style="font-family: Verdana; font-size: 9px;" align="right"><a href="javascript:setMoveInc(1);" style="text-decoration:none">1</a>&nbsp;&nbsp;&nbsp;<a href="javascript:setMoveInc(10);" style="text-decoration:none">10</a></td>\n';
			html += '</tr>\n';
			html += '</table>';
			
		}
		return html;
}
popMenuObj.prototype.show = function() {
	this.isOpen = true;
	this.layer.show();
	this.onShow();
	if (popDebug) window.status = "Last Shown: " + this.id;
}
popMenuObj.prototype.hide = function() {
	if (this.isActive) { return; }
	if (this.openSub && this.openSub.isActive) { return; }
	if (this.openSub) { this.openSub.forceHide(); }
	this.isOpen = false;
	this.layer.hide();
	this.onHide();
	if (popDebug) window.status = "Last Closed: " + this.id;
}
popMenuObj.prototype.forceHide = function() {
	this.isOpen = false;
	this.layer.hide();	
	this.clearHideTimer();
}
popMenuObj.prototype.onHide = function() {void 0;}
popMenuObj.prototype.onShow = function() {void 0;}

function menuItemObj(oPARENT, sID, sLABEL, sURL, sTYPE, sSTYLEA, sSTYLEB, sTARGET) {
		this.parent = oPARENT;
		this.id = sID;
		this.label = sLABEL;
		this.url = sURL;
		this.type = (sTYPE) ? sTYPE : "URL";
		this.style = sSTYLEA;
		this.overStyle = sSTYLEB;
		this.target = sTARGET;
		this.hasSub = false;
		if (this.target) { alert("ERROR: Target property currently not supported!\n\nMenu Item: " + this.id); }
}
menuItemObj.prototype.setSub = function(oMENUOBJ) {
	this.hasSub = true;
	this.subMenu = oMENUOBJ;
	this.subMenu.parent = this.parent;
	this.subMenu.zIndex = this.parent.zIndex + 1;
	this.subMenu.onShow = function() {
		this.parent.openSub = this;
	}
	this.subMenu.onHide = function() {
		if (this.parent.openSub == this) {
			if(!this.parent.isActive) { this.parent.hide(); }
		}
	}
}