/*
 *	Author: 	Dieter Tabor
 *	Date:		30/03/2008
 *	Revision: 	1.1.0.2
*/

function CMenu(menuWidth, menuHeight, textColor, bkgColor, menuOrientation)
{
	this.m_nWidth = menuWidth;					// Default width
	this.m_nHeight = menuHeight;				// Default height
	this.m_nXPos = 0;
	this.m_nYPos = 0;
	this.m_nMenus = 1;							// Number menus created
	//this.m_nMenuItems = 0;						// Number of menu items created
	this.m_szTextColor = textColor;				// Default text color
	this.m_szBkgColor = bkgColor;				// Default background color
	this.m_szOrientation = menuOrientation;		// VERT or HORIZ
	this.m_aMenuItems = new Array();
	this.m_oDhtml = new CDHTMLAPI();
	this.m_aActiveMenus = new Array();
	this.m_bMenuActive = false;
	this.m_nActiveMenuCount = 0;
	this.m_nElem = 0;
}

function CMenuItem(parentID, menuID, menuLabel, menuUrl, textColor, bkgColor, ovrColor)
{
	this.m_szParentID = parentID;
	this.m_szID = menuID;
	this.m_szLabel = menuLabel;
	this.m_szURL = menuUrl;
	this.m_szTextColor = textColor;
	this.m_szBkgColor = bkgColor;
	this.m_szOvrColor = ovrColor;
	this.m_nXPos = 0;
	this.m_nYPos = 0;
	this.m_nIndex = 0;
	this.m_nSiblingMenu = 0;
	this.m_nCurrentMenu = 0;
	this.m_bHasSiblings = false;
}

CMenu.prototype.AddMenuItem = function(parentID, menuID, menuLabel, menuUrl, textColor, bkgColor, ovrColor)
{	
	if(textColor == undefined) {
		textColor = this.m_szTextColor;
	}
	
	if(bkgColor == undefined) {
		bkgColor = this.m_szBkgColor;
	}
	menuItem = new CMenuItem(parentID, menuID, menuLabel, menuUrl, textColor, bkgColor, ovrColor);
	this.m_aMenuItems.push(menuItem);
}

CMenu.prototype.CreateMenu = function(parentID)
{	
	// Set sibling state for menu items
	for(var i = 0; i < this.m_aMenuItems.length; ++i) {
		var nCount = this.m_aMenuItems.length;
		for(var j = 0; j < nCount; ++j) {
			if(this.m_aMenuItems[i].m_szID == this.m_aMenuItems[j].m_szParentID) {
				this.m_aMenuItems[i].m_bHasSiblings = true;
				++nCount;
				break;
			}
		}
	}
	
	// Create main menu
	this.CreateParent();
	//Create sub menu's
	for(var i = 0; i < this.m_aMenuItems.length; ++i) {
		if(this.m_aMenuItems[i].m_bHasSiblings == true) {
			this.m_aMenuItems[i].m_nSiblingMenu = this.CreateSibling(this.m_aMenuItems[i].m_szID, i);
			++nCount;
		}
	}
}

CMenu.prototype.CreateParent = function()
{
	//this.m_nMenuItems = 0;
	this.m_nElem = 0;
	document.write("<ul id=\"menu\" class=\"menuFrm\">");
	// Main menu
	this.GetElementPosition("menu");	
	for(var i = 0; i < this.m_aMenuItems.length; ++i) {
		if(this.m_aMenuItems[i].m_szParentID == "") {
			document.write(this.CreateMenuItem(i, "", true, ""));
		}
	}
	document.write("</ul>");
}

CMenu.prototype.CreateSibling = function(parentID, parentOffset)
{
	var posX = 0;
	var posY = 0;
	//this.m_nMenuItems = 0;
	this.m_nElem = 0;
	
	var oMenu = "<div class=\"flyout\" id=\"" + this.m_nMenus + "\"><ul id=\"menuSibling\" ";
	oMenu += "style=\"left:" + posX + "px; top:" + posY + "px;\">";
	document.write(oMenu);
	
	for(var i = 0; i < this.m_aMenuItems.length; ++i) {
		if(this.m_aMenuItems[i].m_szParentID == parentID) {
			this.m_aMenuItems[i].m_nCurrentMenu = this.m_nMenus;
			document.write(this.CreateMenuItem(i, parentID, false, this.m_nMenus));
		}
	}
	document.write("</ul></div>");
	++this.m_nMenus;
	return (this.m_nMenus - 1);
}

CMenu.prototype.GetItemCoord = function(nIndex, parentID)
{
	var nCount = 1;
	if(parentID != undefined && parentID != "") {
		for(var i = 0; i < this.m_aMenuItems.length; ++i) {
			if(this.m_aMenuItems[i].m_szID == parentID) {
				if(!this.m_oDhtml.m_bIsIE4 && !this.m_oDhtml.m_bIsIE6) {
					this.m_aMenuItems[nIndex].m_nXPos = this.m_aMenuItems[i].m_nXPos + this.m_nWidth + 3; 
					this.m_aMenuItems[nIndex].m_nYPos = this.m_aMenuItems[i].m_nYPos + ((this.m_nElem - 1) * (this.m_nHeight + 2));
				}
				else {
					this.m_aMenuItems[nIndex].m_nXPos = this.m_aMenuItems[i].m_nXPos + this.m_nWidth + 3; 
					this.m_aMenuItems[nIndex].m_nYPos = this.m_aMenuItems[i].m_nYPos + ((this.m_nElem - 1) * (this.m_nHeight + 2));
				}
			}
		}
	}
	else {
		if(nIndex > 0) {
			// Creates the main menu
			if(!this.m_oDhtml.m_bIsIE4 && !this.m_oDhtml.m_bIsIE6) {
				this.m_aMenuItems[nIndex].m_nXPos = this.m_nXPos;
				this.m_aMenuItems[nIndex].m_nYPos = this.m_nYPos + ((this.m_nElem - 1) * (this.m_nHeight + 2));
			}
			else {
				this.m_aMenuItems[nIndex].m_nXPos = this.m_nXPos;
				this.m_aMenuItems[nIndex].m_nYPos = this.m_nYPos + ((this.m_nElem - 1) * (this.m_nHeight + 2));
			}
		}
		else {
			// Creates the 1st element of the main menu
			if(!this.m_oDhtml.m_bIsIE4 || !this.m_oDhtml.m_bIsIE6) {
				this.m_aMenuItems[nIndex].m_nXPos = this.m_nXPos;
				this.m_aMenuItems[nIndex].m_nYPos = this.m_nYPos;
			}
			else {
				this.m_aMenuItems[nIndex].m_nXPos = this.m_nXPos;
				this.m_aMenuItems[nIndex].m_nYPos = this.m_nYPos;
			}
		}	
	}	
}

CMenu.prototype.CreateMenuItem = function(nIndex, parentID, isParent, menuID)
{
	//++this.m_nMenuItems;
	
	++this.m_nElem;
	this.GetItemCoord(nIndex, parentID);
	var oMenuItem = "<li ";  
	
	oMenuItem += "onmouseover=\"javascript:menu.UpdateMenu(" + nIndex + ", true); \" ";
	oMenuItem += "onmouseout=\"javascript:menu.UpdateMenu(" + nIndex + ", false); \" ";
	
	if(isParent != false) {
		oMenuItem += "style=\"visibility:visible; ";
	}
	else {
		oMenuItem += "id=\"menuSibling\" style=\"visibility:visible; ";
	}
	
	oMenuItem += "left:";
	oMenuItem += this.m_aMenuItems[nIndex].m_nXPos;
	oMenuItem += "px; top:";	
	oMenuItem += this.m_aMenuItems[nIndex].m_nYPos;
	oMenuItem += "px; width:";	
	oMenuItem += this.m_nWidth;
	oMenuItem += "px; height:";	
	oMenuItem += this.m_nHeight;
	oMenuItem += "px; background:";	
	oMenuItem += this.m_aMenuItems[nIndex].m_szBkgColor;
		
	if(this.m_aMenuItems[nIndex].m_bHasSiblings == true) {
		oMenuItem += "; background-image:url(/jscriptlib/arrow.jpg); background-repeat:no-repeat; background-position:right; ";
	}
	
	if(this.m_aMenuItems[nIndex].m_szURL != "") {
		oMenuItem += ";\">";
		oMenuItem += "<a style=\"color:" + this.m_aMenuItems[nIndex].m_szTextColor + "; a.hover {color:#00FF00; text-decoration: none; }; \" ";
		oMenuItem += "href=\"" + this.m_aMenuItems[nIndex].m_szURL + "\">";
		oMenuItem += this.m_aMenuItems[nIndex].m_szLabel;
		oMenuItem += "</a>";
	}
	else {
		oMenuItem += "; color:" + this.m_aMenuItems[nIndex].m_szTextColor + "; ";
		oMenuItem += "padding-top:2px; padding-bottom:0px; margin:0px; height:17px;\">";
		oMenuItem += this.m_aMenuItems[nIndex].m_szLabel;
	}
	
	oMenuItem += "</li>";

	return oMenuItem;
}

CMenu.prototype.GetElementPosition = function(elemID)
{
	var objVal = elemID.toString();
    var offsetTrail = this.m_oDhtml.GetObject(objVal)
    while(offsetTrail) {
		this.m_nXPos += offsetTrail.offsetLeft;
        this.m_nYPos += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
	
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof(document.body.leftMargin != "undefined")) {
        this.m_nXPos += document.body.leftMargin;
        this.m_nYPos += document.body.topMargin;
    }
}

CMenu.prototype.UpdateMenu = function(index, active)
{	
	this.m_bMenuActive = active;
	if(active != false) {
		if(this.m_aMenuItems[index].m_nSiblingMenu > 0) {
			this.m_aActiveMenus.push(this.m_aMenuItems[index].m_nSiblingMenu);
			++this.m_nActiveMenuCount;
			this.ShowMenu(this.m_aMenuItems[index].m_nSiblingMenu);
		}
		else {
			while((this.m_aMenuItems[index].m_nCurrentMenu != this.m_aActiveMenus[this.m_nActiveMenuCount - 1]) &&
					(this.m_aActiveMenus[this.m_nActiveMenuCount - 1] != undefined)) {
				var nVal = this.m_aActiveMenus.pop();
				--this.m_nActiveMenuCount;
				this.HideMenu(nVal);
			}
		}			
	}
}

CMenu.prototype.ClearMenu = function()
{
	if(this.m_bMenuActive != false) {
		// Do nothing
	}
	else {
		while(this.m_nActiveMenuCount > 0) {
			var nVal = this.m_aActiveMenus.pop();
			--this.m_nActiveMenuCount;
			this.HideMenu(nVal);
		}
	}
}

CMenu.prototype.ShowMenu = function(menuID)
{	
	this.m_oDhtml.ShowControl(menuID);
}

CMenu.prototype.HideMenu = function(menuID)
{
	this.m_oDhtml.HideControl(menuID);
}
