/*------------------------------------------------------------------------------
 * 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 _themeFolder  = "css";
var _themeFile    = "XPBlue.css";
var _imageFolder  = "images";
var _browser      = -1;
var _strCtxt      = "";
var _cpMsg        = "";
var panelBar_LinkMenu = false;

function MenuBand(pstrDesc, pstrTip, pstrImage, onClickHandle)
{
	// 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.
	this.iconSrc    = pstrImage;//Name of the image file.
	this.onClickHandle = onClickHandle;	// Un handle a una función utilizada para reportar un click
	//Methods
	this.addSubMenu = addSubMenu;	//function for item addition
	this.render     = drawMenu;		//function for rendering header
	this.find       = findSubMenu   //Busca por un subMenu con un ID especifico
}

function SubMenu(pstrDesc, pstrLink, pstrLinkData, pstrTip, pstrImage, pstrTarget, pstrUniqueID, onClickHandle)
{
	// 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.UniqueId      = pstrUniqueID;      //Un ID único para el menú
	this.isHeader      = false;
	this.isSelected    = false;		//future use
	this.iconSrc       = pstrImage;	        //Name of the image file.
	this.onClickHandle = onClickHandle;	//Un handle a una función utilizada para reportar un click

	//Methods
	this.render     = drawSubMenu;	//function for rendering sub item.
}

function findSubMenu(pstrUniqueID)
{
	for(var i = 0; i < this.submenu.length; i++)
	    if(this.submenu[i].UniqueId == pstrUniqueID)
	        return this;
	return null;
}

function addSubMenu(pstrDesc, pstrLink, pstrLinkData, pstrTip, pstrImage, pstrTarget, pstrUniqueID)
{
	var objTmp;

	objTmp = new SubMenu(pstrDesc, pstrLink, pstrLinkData, pstrTip, pstrImage, pstrTarget, pstrUniqueID);
	objTmp.parentId = this.id;
	this.submenu[this.smcount] =  objTmp;
	this.smcount++;
}

function drawMenu()
{
	var strImg = "";
	var iCntr = 0;
	var objMenu;
	var strId, strLbl;

//En esta parte se dibuja el Menu
	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%\">");
	if (this.iconSrc)
		strImg = "<img src=\"" + _strCtxt + _imageFolder + "/" +
					this.iconSrc + "\" border=\"0\"\">&nbsp;";
	document.write("<tr><td style=\"vertical-align: middle;\">"+ strImg + this.label + "</td></tr>");
	document.write("</table></div>");

//En esta parte se dibuja el subMenu
	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(this.id);
	}
	document.write("</table></div>");

}

function drawSubMenu(PsId)
{
	var strImg = "";

	document.write("<tr><td class=\"itemCell\" id=\"" + PsId + this.linkData.replace(/\w+[=]/i,'') +
								"_option\" onmouseover=\"setCls(this,'itemcellover')\" onmouseout=\"setCls(this,'itemcell')\">");
	if (this.iconSrc)
		strImg = "<img src=\"" + _strCtxt + _imageFolder + "/" +
					this.iconSrc + "\" border=\"0\"  style=\"display: none;\"\">&nbsp;";
	document.write("<span class=\"itemLink\"><a href=" + getLink(this.linkTarget, (_strCtxt + this.hlink), this.linkData) + "\">");
	document.write(strImg);
	document.write(this.label);
	document.write("</a></span>");
	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 setCls(PoCell,PsClsNm){
				PoCell.className = PsClsNm;
}

function mousehover(pobjSrc)
{
}

function mouseout(pobjSrc)
{
}

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 != null)
	{
		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;
	}

	var lMenu = findMenu(pobjSrc.id);
	if(lMenu != null && panelBar_LinkMenu == true && lMenu.submenu.length == 0)
	{
		var hRef = window.location.href;
		if(hRef != null)
		{
			var A = hRef.split("?");
			hRef = A[0];
		}
		var A = hRef + "?" + pobjSrc.id;
		window.location = A;
	}
	else (lMenu != null && panelBar_LinkMenu == false && lMenu.submenu.length == 0)
		lMenu.onClickHandle(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=\"" + _themeFolder + "/" + _themeFile + "\"" +
					" 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 class=\"separetor\">&nbsp;</span>");
	}
	document.write("</div></center>");
}

/*------------------------------------------------------------------------------
 * Public functions
 *----------------------------------------------------------------------------*/
function findMenu(menuId)
{
	for(var i = 0; i < _arrMenus.length; i++)
		if(_arrMenus[i].id == menuId)
			return _arrMenus[i];
	return null;
}

function createMenu(pstrDesc, pstrTip, pstrImage, pstrUniqeId, onClickHandle)
{
	var mnuRet;

	mnuRet = new MenuBand(pstrDesc, pstrTip, pstrImage, onClickHandle);
	if(pstrUniqeId == null)
		mnuRet.id = "mnu_" + _mnuIndex;
	else
		mnuRet.id = pstrUniqeId;
		_arrMenus[_mnuIndex] = mnuRet;
	_mnuIndex++;
	return mnuRet;
}

function createSubMenu(pMenu, pstrDesc, pstrLink, pstrLinkData, pstrTip, pstrImage, pstrTarget, pstrUniqueID, onClickHandle)
{
	if (pMenu)
		if (pMenu.isHeader)
			pMenu.addSubMenu(pstrDesc, pstrLink, pstrLinkData, pstrTip, pstrImage, pstrTarget, pstrUniqueID, onClickHandle);
}

/**
 * Si un menú esta abierto, entonces lo cierra, pero si esta cerrado lo abre.
 * @param pstrUniqueId. El ID único del menú. (Es el último valor pasado a la función createSubMenu)
 */
function openMenu(pstrUniqueId)
{
 	var LoCell;
	var Menu = getParentSubMenu(pstrUniqueId);
	if(Menu != null)
	{
		toggle(Menu);
		LoCell = document.getElementById(Menu.id + pstrUniqueId + "_option")
		if(LoCell != null)
		{
			LoCell.className = "itemCellSelected"
			LoCell.onmouseout = function(){setCls(this,'itemCellSelected')}
		}
	}
}

/**
 * Obtiene el objeto que representa al menú cuyo id único es el que pasan como parámetro
 * @param pstrUniqueId. El ID único del menú. (Es el último valor pasado a la función createSubMenu)
 */
function getParentSubMenu(pstrUniqueId)
{
	var SubMenu;
	for(var i = 0; i < _arrMenus.length; i++)
	{
		SubMenu = _arrMenus[i].find(pstrUniqueId);
		if(SubMenu != null)
		    return SubMenu;
	}
	return null;
}

function setTheme(pstrTheme, pstrThemeFolder, pstrImgFolder)
{
	if (pstrTheme != null)
		_themeFile =  pstrTheme;
	if (pstrThemeFolder != null)
		_themeFolder = pstrThemeFolder;
	if (pstrImgFolder != null)
		_imageFolder = pstrImgFolder;
}

/**
 * Esta función se asegura de abrir el menú. Si no esta abierto lo abre
 * @param pstrUniqueId. El ID único del menú. (Es el último valor pasado a la función createSubMenu)
 */
function EnsureOpenMenu(pstrUniqueId)
{
	var Menu = getParentSubMenu(pstrUniqueId);
	if(Menu != null && Menu.id != _currMenu)
		openMenu(pstrUniqueId);
}

