/*------------------------------------------------------------------------------
 * 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  = "_blank";
TARGET_SELF   = "_self";
TARGET_PARENT = "_parent";
TARGET_TOP    = "_top";

// 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        = "";

function MenuBand(pstrDesc, pstrTip, pstrImage)
{
	// 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      = true;		//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.isSelected = false;		//future use
	//Methods
	this.addSubMenu = addSubMenu;	//function for item addition
	this.render     = drawMenu;		//function for rendering header
}

function SubMenu(id, pstrDesc, pstrLink, pstrLinkData, pstrTip, pstrImage, pstrTarget)
{
	// Properties
	this.id         = id;            
	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(this.smcount, 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;

	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;\">") ;
	document.write("<table border=\"0\" cellspacing=\"0\"" +
					" cellpadding=\"0\" width=\"100%\">");
    document.write("<tr><td width=25px>");	
	if (this.iconSrc)
		strImg = "<img src=\"" + _strCtxt + _imageFolder + "/" + 
					this.iconSrc + "\" border=\"0\"\">";
	document.write(strImg + "</td>");				
	document.write("<td>"+ this.label + "</td></tr></table>");
	
	document.write("</td></tr></table></div>");

	//start drawing sub menu
	if (this.open){
	    document.write("<div style=\"display: block; 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%\" class=\"menuItemTable\">");
	for (iCntr = 0; iCntr < this.smcount; iCntr++)
	{
		this.submenu[iCntr].render();
	}
	document.write("</table></div>");
	
	
}

function drawSubMenu()
{
	var strImg = "";
	document.write("<tr><td>");
	document.write("<table border=\"0\" cellspacing=\"0\"" +
					" cellpadding=\"0\" width=\"100%\">");
    document.write("<tr><td width=25px>")	
	if (this.iconSrc)
		strImg = "<img src=\"" + _strCtxt + _imageFolder + "/" + 
					this.iconSrc + "\" border=\"0\"\">";
	document.write("<a href=\"" + 
					getLink(this.parentId, this.id, this.linkTarget, (_strCtxt + this.hlink), this.linkData) + 
					"\">");
	document.write(strImg);
	document.write("</a>");
	document.write("</td>");
	
	document.write("<td>")
	document.write("<a href=\"" + 
					getLink(this.parentId, this.id, this.linkTarget, (_strCtxt + this.hlink), this.linkData) + 
					"\">");
	document.write(this.label);
	document.write("</a>");
	document.write("</tr>");
	document.write("</table>");
	document.write("</td></tr>");
}

function getLink(menuParentId, menuId, pTarget, pstrLink, pstrLinkData) 
{
	var strRet = "";
	var strTmp;
	strTmp = pstrLink;
	if (pstrLinkData != null)
		strTmp = pstrLink + "?" + pstrLinkData;
	strRet="javascript:openLink(" + menuParentId.substr(4) + "," + menuId + ",'" + strTmp + "','" + pTarget + "')";
	return strRet;
}
function getOpenMenuItems(){
	var s="";
	for (iCntr = 0; iCntr < _mnuIndex; iCntr++)
	{
		if(!_arrMenus[iCntr].open)
	        s =s + "," + iCntr;
	}
	s=s.substr(1);
    return s;
}
function openLink(menuParentId, menuId, url, pTarget){
    //keep the menu expanding state
	var s=getOpenMenuItems();
    url = url + "&selectedTab=" + menuParentId + "&selectedMenuId=" + menuId + "&menuState=" + s;
    
	if (pTarget == TARGET_BLANK) 
		window.open(url);
	else if (pTarget == TARGET_SELF)
		document.location=url;
	else
		document.location=url;
}
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";
		idx=pobjSrc.id.substr(4);
		_arrMenus[idx].open = true;
		_arrMenus[idx].selected = true;
	}
	else
	{
		pobjSrc.className = "menuHeaderCollapsedOver";
		child.style.visibility = "hidden";
		child.style.display = "none";
		idx=pobjSrc.id.substr(4);
		_arrMenus[idx].open = false;
		_arrMenus[idx].selected = false;
	}
	_currMenu = pobjSrc.id;
	var s=getOpenMenuItems();
	setCookie("openMenuItems", s);
}

function loadMenuState(){
    var s=getCookie("openMenuItems");
    if(!s){
        for(i=0; i<_mnuIndex;i++){
            _arrMenus[i].isOpen=true;
        }
    }else{
        var items = s.split(",");
        if (!items) return;
        for(i=0; i<items.length;i++){
            if(_arrMenus[items[i]].open) 
                _arrMenus[items[i]].open=false;
            else
                _arrMenus[items[i]].open=true;
        }
   }
}

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 + "\">");
	loadMenuState();
	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, pstrImage)
{
	var mnuRet;

	mnuRet = new MenuBand(pstrDesc, pstrTip, pstrImage);
	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, pstrThemeFolder, pstrImgFolder)
{
	if (pstrTheme != null)
		_themeFile =  pstrTheme;
	if (pstrThemeFolder != null)
		_themeFolder = pstrThemeFolder;
	if (pstrImgFolder != null)
		_imageFolder = pstrImgFolder;
}

