// createMenus.js

ie = document.all ? 1 : 0;
ns4x = document.layers ? 1 : 0;
dom = document.getElementById ? 1 : 0;


// For NS4x need to pad string with spaces as in case display width
// is greater than string width. Since we are _clipping_ string beyond
// edge of submenu item cell by enclosing strings in <nobr>...</nobr>
// tags, then it doesn't matter how much padding we add...so add an
// arbitrarily large amount to accomodate large variations in menu
// item caption strings!?
//
// NOTE: Use string padding with IE as well. Seems if we use separators
// and don't pad then the menu links are not active to the rightmost edges
// of their cells!? Go figure!?
//
var strNbspPadding = "";
ns = 50;
while (ns > 0) {
	strNbspPadding += "&nbsp;";
	ns--;
}

// Optional menu items separator parameters.
//
var iSeparatorHeight = 0;
var strSeparatorUrl = "";

function setMenuSeparator(url,h) {
	iSeparatorHeight = h;
	strSeparatorUrl = String(url);
}

function haveSeparator() {
	return iSeparatorHeight > 0 && strSeparatorUrl.length != 0;
}

function getSeparatorHeight() {
	return iSeparatorHeight;
}

function getSeparatorUrl() {
	return strSeparatorUrl;
}


// General/miscellaneous parameters.
//
var strBorderColor = "#AFAF6C";
var strBackgroundColor = "#262626";


// ypSlideOutMenu array declaration and initialization.
//
var menus = null;

function resetYpSlideOutMenuArray() {
	menus = new Array();
}

	
function createContainerDiv(arrObj) {
	var strHtml = "";
	
	if (arrObj.length) {
		var id = arrObj[0].pid;
		var width = getMenuWidth(arrObj);
		
		strHtml += "<DIV id=\"m" + id + "Container\" class=\"ypcontainer\"";
		if (!ns4x)
			strHtml += " style=\"VISIBILITY: hidden\"";
		strHtml += ">\n";
		strHtml += createContentDiv(arrObj,width);
		strHtml += "</DIV>\n";
	}
	else {
		alert("FATAL ERROR: createContainerDiv() arrObj.length=0");
	}
	
	return strHtml;
}

function createContentDiv(arrObj,width) {
	var strHtml = "";
	
	strHtml += "<DIV id=\"m" + arrObj[0].pid + "Content\" class=\"ypcontents\"";
	if (!ns4x)
		strHtml += " style=\"VISIBILITY: inherit\"";
	strHtml += ">\n";
	strHtml += "<TABLE cellSpacing=0 cellPadding=0 bgColor=" + strBackgroundColor + " border=0 width=" + width + ">\n";
	//strHtml += "<TABLE cellSpacing=0 cellPadding=0 bgColor=" + strBackgroundColor + " border=3 borderColor=" + strBorderColor + " width=" + (width+6) + ">\n";
	if (!ns4x)
		strHtml += "<TBODY>\n";
	strHtml += "<TR>\n";
	strHtml += "<TD width=" + width + ">\n";
	strHtml += createMenuItems(arrObj,width);
	strHtml += "</TD></TR>";
	if (!ns4x)
		strHtml += "</TBODY>";
	strHtml += "</TABLE></DIV>\n";
	
	return strHtml;
}

function createMenuItems(arrObj,width) {
	var strHtml = "";
	
	var bSeparator = haveSeparator();
	for (var i = 0; i < arrObj.length; i++) {
		strHtml += createMenuDiv(arrObj[i],width,(bSeparator && i));
	}
	
	return strHtml;
}


function createMenuDiv(obj,width,bSeparator) {
	var strHtml = "";
	
	var bActuator = (obj.type.toLowerCase().indexOf("actuator") == 0);
	
	strHtml += "<DIV class=\"menu\"";
	if (bActuator)
		strHtml += " id=\"m" + obj.id + obj.type + "\"";
	
	if (ns4x) {
		strHtml += "><TABLE cellspacing=0 cellpadding=0 border=0 bgColor=" + strBackgroundColor + " width=" + width + " >\n";
		if (bSeparator)
			strHtml += menuDivSeparator(width);
		strHtml += "<TR><TD class=\"menu\"><A href=\"" + obj.href + "\"";
		
		if (bActuator)
			strHtml += " onMouseOver=\"ypSlideOutMenu.showMenu('m" + obj.id + "', event);\" onMouseOut=\"ypSlideOutMenu.hideMenu('m" + obj.id + "', event);\"";
		
		strHtml += "><NOBR>\n";
		strHtml += obj.caption;
		strHtml += strNbspPadding;
		strHtml += "</NOBR></A></TD></TR>\n";
	}
	else {
		strHtml += " style=\"VISIBILITY: inherit\"";
		
		if (bActuator)
			strHtml += " onMouseOver=\"ypSlideOutMenu.showMenu('m" + obj.id + "');\" onMouseOut=\"ypSlideOutMenu.hideMenu('m" + obj.id + "');\"";
		
		strHtml += "><TABLE cellspacing=0 cellpadding=0 border=0 bgColor=" + strBackgroundColor + " width=" + width + " >\n";

		if (bSeparator) {
			strHtml += menuDivSeparator(width);
		}
		
		strHtml += "<TR><TD class=\"menu\"><A href=\"" + obj.href + "\"><NOBR>\n";
		strHtml += obj.caption;
		strHtml += strNbspPadding;
		strHtml += "</NOBR></A></TD></TR>\n";
	}
	
	strHtml += "</TABLE></DIV>\n";
	
	//alert("createMenuDiv(" + obj.id + "," + width + ")\n" + strHtml); // debug

	return strHtml;
}

function menuDivSeparator(width) {
	return "<TR><TD width=" + width + "><img src=\"" + getSeparatorUrl() + "\" height=" + getSeparatorHeight() + "></TD></TR>\n";
}

function getMenuWidth(arrObj) {
	// find width of widest submenu item
	//
	var width = 0;
	if (arrObj.length) {
		for (var i = 0; i < arrObj.length; i++) {
			var wi = arrObj[i].getWidth();
			if (wi > width) {
				width = wi;
			}
		}
		
		// save display width to all
		for (var i = 0; i < arrObj.length; i++) {
			arrObj[i].wdsp = width;
		}
	}
	else {
		alert("FATAL ERROR: getMenuWidth() arrObj.length=0");
	}
	return width;
}


function getMenuHeight(arrObj) {
	// sum heights of all submenu items
	//
	var height = 0;
	if (arrObj.length) {
		for (var i = 0; i < arrObj.length; i++) {
			height += arrObj[i].getHeight();
		}
	}
	else {
		alert("FATAL ERROR: getMenuHeight() arrObj.length=0");
	}
	return height;
}


function getDirection(dir)
{
	if (dir) {
		var ldir = String(dir).toLowerCase();
		if ( ldir.indexOf("right") == 0 || ldir.indexOf("left") == 0 ||
			 ldir.indexOf("up") == 0 || ldir.indexOf("down") == 0 )
		{
			return ldir;
		}
	}

	return "down";
}


//============================================================================/
//============================================================================/

dbMenuItem.iFontSizeIndex = 0;
dbMenuItem.setFontSizeIndex = function(index)
{
	dbMenuItem.iFontSizeIndex = index;
	//alert("dbMenuItem.iFontSizeIndex=" + dbMenuItem.iFontSizeIndex); // debug
}

dbMenuItem.arrFontSize = null;
dbMenuItem.getFontSize = function()
{
	if (dbMenuItem.arrFontSize) {
		return dbMenuItem.arrFontSize[dbMenuItem.iFontSizeIndex];
	}
	return 0;
}


dbMenuItem.arrSpaceWidth = null;
dbMenuItem.getSpaceWidth = function()
{
	if (dbMenuItem.arrSpaceWidth) {
		return dbMenuItem.arrSpaceWidth[dbMenuItem.iFontSizeIndex];
	}
}


dbMenuItem.arrIndexByMenuId = null;
dbMenuItem.addIndexByMenuId = function(menuid,index)
{
	if (!dbMenuItem.arrIndexByMenuId)
		dbMenuItem.arrIndexByMenuId = new Array();

	eval('dbMenuItem.arrIndexByMenuId["m' + menuid + '"]=' + index);
}

dbMenuItem.getIndexFromMenuId = function(menuid)
{
	if (dbMenuItem.arrIndexByMenuId) {
		eval('dbMenuItem.arrIndexByMenuId["m' + menuid + '"]');
	}
}


function dbMenuItem (parentid,orderindex,type,id,caption,href,x,y,w,h,dir)
{
	this.pid = parseInt(parentid,10);
	this.order = parseInt(orderindex,10);
	this.type = String(type);
	this.id = parseInt(id,10);
	this.caption = String(caption);
	this.href = String(href);
	this.x = parseInt(x,10);
	this.y = parseInt(y,10);
	this.w = String(w);
	this.h = String(h);
	this.dir = getDirection(String(dir));
	
	this.wdsp = 0; // display width set in getMenuWidth() for an entire submenu group
}

dbMenuItem.prototype.toString = function()
{
	return String(this.pid+","+this.order+","+this.type+","+this.id+"\n"+this.caption+"\n"+this.href+"\n"+this.x+","+this.y+",\n'"+this.w+"',\n'"+this.h+"',\n"+this.dir);
}

dbMenuItem.prototype.getDisplayWidth = function()
{
	return this.wdsp;
}

dbMenuItem.prototype.getWidth = function(index)
{
	if (index)
		return parseInt(getItemForIndex(this.w,index),10);
	else
		return parseInt(getItemForIndex(this.w,dbMenuItem.iFontSizeIndex),10);
}

dbMenuItem.prototype.getHeight = function(index)
{
	if (index)
		return parseInt(getItemForIndex(this.h,index),10);
	else
		return parseInt(getItemForIndex(this.h,dbMenuItem.iFontSizeIndex),10);
}

function getItemForIndex(s,i)
{
	//alert("getItemForIndex('" + s + "'," + i + ")"); // debug
	
	if (s.length) {
		var arrItem = s.split(",");
		if (arrItem.length > i) {
			//alert("getItemForIndex('" + s + "'," + i + ") arrItem[i]=" + arrItem[i]); // debug
			return arrItem[i];
		}
		return arrItem[0];
	}
	return null;
}
