/*------------------------------------------------------------------------------
 * FILENAME : PanelBar.js
 * PURPOSE  : Create a XP like panelbar control
 * AUTHOR   : Prasad P. Khandekar
 * CREATED  : 10/02/2005
 * COPYRIGHT: (c) 2005, Prasad P. Khandekar
 *------------------------------------------------------------------------------ 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *----------------------------------------------------------------------------*/
// link target constants
TARGET_OTHER  = 0;
TARGET_BLANK  = 1;
TARGET_SELF   = 2;
TARGET_PARENT = 3;
TARGET_TOP    = 4;

// borwser constants
BROWSER_IE    = 1;
BROWSER_GECKO = 2;
BROWSER_OTHER = -1;

var _currMenu     = "";
var _arrMenus     = new Array;	//Holds top level menus
var _mnuIndex     = 0;			//Total number of menus.

var _themeUrl  = "";
var _imageFolder  = "shared/images";
var _browser      = -1;
var _strCtxt      = "";
var _cpMsg        = "";

function MenuBand(pstrDesc, pstrTip)
{
	// Properties
	this.id        = "";		//generated at runtime.
	this.label     = pstrDesc;	//Text to be displayed in header
	this.microHelp = pstrTip;	//Tooltip text
	this.isHeader  = true;
	this.open      = false;		//future use
	this.submenu   = new Array;	//array containing link items.
	this.smcount   = 0;			//count of sub items.

	//Methods
	this.addSubMenu = addSubMenu;	//function for item addition
	this.render     = drawMenu;		//function for rendering header
}

function SubMenu(pstrDesc, pstrLink, pstrLinkData, pstrTip, pstrImage, pstrTarget)
{
	// Properties
	this.parentId   = "";			//populated at runtime
	this.label      = pstrDesc;		//Item text to be displayed
	this.hlink      = pstrLink;		//Link url
	this.linkTarget = pstrTarget;	//Target window and or frame identifier
	this.linkData   = pstrLinkData;	//Exrta data to be passed through querystring.
	this.microHelp  = pstrTip;		//Tooltip text.
	this.isHeader   = false;
	this.isSelected = false;		//future use
	this.iconSrc    = pstrImage;	//Name of the image file.

	//Methods
	this.render     = drawSubMenu;	//function for rendering sub item.
}

function addSubMenu(pstrDesc, pstrLink, pstrLinkData, pstrTip, pstrImage, pstrTarget)
{
	var objTmp;

	objTmp = new SubMenu(pstrDesc, pstrLink, pstrLinkData, pstrTip, pstrImage, pstrTarget);
	objTmp.parentId = this.id;
	this.submenu[this.smcount] =  objTmp;
	this.smcount++;
}

function drawMenu()
{
	var iCntr = 0;
	var objMenu;
	var strId, strLbl;

	if (this.id == 'mnu_0') {
		document.write("<div class=\"menuHeaderExpanded\" id=\"" + this.id + "\"" +
						" onmouseover=\"mousehover(this)\"" +
						" onmouseout=\"mouseout(this)\"" +
						"onclick=\"toggle(this)\">");
			_currMenu = this.id;
	} else {
		document.write("<div class=\"menuHeaderCollapsed\" id=\"" + this.id + "\"" +
						" onmouseover=\"mousehover(this)\"" +
						" onmouseout=\"mouseout(this)\"" +
						"onclick=\"toggle(this)\">");
	}
	document.write("<table border=\"0\" cellspacing=\"0\"" +
					" cellpadding=\"4\" width=\"100%\">");
	document.write("<tr><td style=\"vertical-align: center;\">" + this.label + 
					"</td></tr>");
	document.write("</table></div>");

	//start drawing sub menu
	if (this.id == 'mnu_0') {
		document.write("<div style=\"display: black; visibility: visible;\"" +
						" class=\"menuItems\" id=\"" + this.id + "_child" + "\">");
	} else {
		document.write("<div style=\"display: none; visibility: hidden;\"" +
						" class=\"menuItems\" id=\"" + this.id + "_child" + "\">");	       	
	}

	document.write("<table border=\"0\" cellspacing=\"0\"" +
					" cellpadding=\"4\" width=\"100%\">");
	for (iCntr = 0; iCntr < this.smcount; iCntr++)
	{
		this.submenu[iCntr].render();
	}
	document.write("</table></div>");
}

function drawSubMenu()
{
	var strImg = "";

	document.write("<tr><td>");
	if (this.iconSrc)
		strImg = "<img src=\"" + _strCtxt + _imageFolder + "/" + 
					this.iconSrc + "\" border=\"0\"\">&nbsp;";
	document.write("<a href=" + 
					getLink(this.linkTarget, (_strCtxt + this.hlink), this.linkData) + 
					"\">");
	document.write(strImg);
	document.write(this.label);
	document.write("</a>");
	document.write("</td></tr>");
}

function getLink(pTarget, pstrLink, pstrLinkData) 
{
	var strRet = "";
	var strTmp;

	strTmp = pstrLink;
	if (pstrLinkData != null)
		strTmp = pstrLink + "?" + pstrLinkData;

	if (pTarget == TARGET_BLANK) 
		strRet = "\"" + strTmp + "\" TARGET=\"_blank\"";
	else if (pTarget == TARGET_SELF)
		strRet = "\"" + strTmp + "\" TARGET=\"_self\"";
	else if (pTarget == TARGET_PARENT)
		strRet = "\"" + strTmp + "\" TARGET=\"_parent\"";
	else if (pTarget == TARGET_TOP)
		strRet = "\"" + strTmp + "\" TARGET=\"_top\"";
	else if (pTarget != null)
		strRet = "\"" + strTmp + "\" TARGET=\"" + pTarget + "\"";
	else
		strRet = "\"" + strTmp + "\"";
	return strRet;
}

function mousehover(pobjSrc)
{
	var strCls = pobjSrc.className;
	if (strCls == "menuHeaderExpanded")
		pobjSrc.className = "menuHeaderExpandedOver";
	else
		pobjSrc.className = "menuHeaderCollapsedOver";
}

function mouseout(pobjSrc)
{
	var strCls = pobjSrc.className;
	if (strCls == "menuHeaderExpandedOver")
		pobjSrc.className = "menuHeaderExpanded";
	else
		pobjSrc.className = "menuHeaderCollapsed";
}

function toggle(pobjSrc)
{
	var strCls = pobjSrc.className;
	var strId = pobjSrc.id;
	var objTmp, child;

	if (pobjSrc.id != _currMenu)
		objTmp = document.getElementById(_currMenu);

	if (objTmp)
	{
		objTmp.className = "menuHeaderCollapsed";

		child = document.getElementById(_currMenu + "_child");
		child.style.visibility = "hidden";
		child.style.display = "none";
	}

	child = document.getElementById(strId + "_child");
	if (child.style.visibility == "hidden")
	{
		pobjSrc.className = "menuHeaderExpandedOver";
		child.style.visibility = "visible";
		child.style.display = "block";
	}
	else
	{
		pobjSrc.className = "menuHeaderCollapsedOver";
		child.style.visibility = "hidden";
		child.style.display = "none";
	}
	_currMenu = pobjSrc.id;
}

function detectBrowser()
{
	switch(navigator.family)
	{
		case 'ie4':
			_browser = BROWSER_IE;
			break;
		case 'gecko':
			_browser = BROWSER_GECKO;
			break;
		default:
			_browser = BROWSER_OTHER;
			break;
	}
}

function detectContext()
{
	var strProto, strHost, strPath;
	var strPort, strUrl, strBase;
	var strRemain;
	var intLen, intPos;

	// determine the context
	strProto  = window.location.protocol;
	if (strProto.indexOf("http") != -1)
	{
		strHost   = window.location.hostname;
		strPath   = window.location.pathname;
		strPort   = window.location.port;
		strUrl    = window.location.href;
		strBase   = strProto + "/" + "/" + strHost + ":" + strPort;
		intLen    = strBase.length;
		strRemain = strUrl.substr(intLen + 1);
		intPos    = strRemain.indexOf("/");
		_strCtxt  = strRemain.substr(0, intPos);
		if (_strCtxt.length > 0)
			_strCtxt = "/" + _strCtxt + "/";
	}
}

function initialize(pintWidth)
{
	var iCntr = 0;
	document.write("<link href=\"" + _themeUrl + "\"" +
					" rel=\"stylesheet\" type=\"text/css\">");

	if (_cpMsg != null && _cpMsg.length > 0)
		document.write("<center><font style=\"color: " + document.bgColor + 
						";font-size: 4pt;\">" + _cpMsg + "</font>");
	document.write("<div id=\"panelBar\" style=\"width: " + pintWidth + "\">");
	for (iCntr = 0; iCntr < _mnuIndex; iCntr++)
	{
		_arrMenus[iCntr].render();
		document.write("<span style=\"display: block;\">&nbsp;</span>");
	}
	document.write("</div></center>");
}

/*------------------------------------------------------------------------------
 * Public functions
 *----------------------------------------------------------------------------*/
function createMenu(pstrDesc, pstrTip)
{
	var mnuRet;

	mnuRet = new MenuBand(pstrDesc, pstrTip);
	mnuRet.id = "mnu_" + _mnuIndex;
	_arrMenus[_mnuIndex] = mnuRet;
	_mnuIndex++;
	return mnuRet;
}
 
function createSubMenu(pMenu, pstrDesc, pstrLink, pstrLinkData, pstrTip, 
						pstrImage, pstrTarget)
{
	if (pMenu)
		if (pMenu.isHeader)
			pMenu.addSubMenu(pstrDesc, pstrLink, pstrLinkData, pstrTip, pstrImage, 
								pstrTarget);
}

function setTheme(pstrTheme, pstrImgFolder)
{
	if (pstrTheme != null)
		_themeUrl =  pstrTheme;
	if (pstrImgFolder != null)
		_imageFolder = pstrImgFolder;
}