/*------------------------------------------------------------------------------
 * FILE    : PanelBarOrig.js
 * PURPOSE : Render Panelbar like cross-browser DHTML control.
 * REQUIRES: JavaScript 1.3, IE 6.0, FireFox 1.0
 * AUTHOR  : Khan Information Systems (KIS)
 *------------------------------------------------------------------------------
 * Copyright (c) 2005. Khan Information Systems. All Rights Reserved
 * The contents of this file are subject to the KIS Public License 1.0
 * (the "License"); you may not use this file except in compliance with the 
 * License. You should have received a copy of the KIS Public License along with 
 * this library; if not, please ask your software vendor to provide one.
 * 
 * YOU AGREE THAT THE PROGRAM IS PROVIDED AS-IS, WITHOUT WARRANTY OF ANY KIND
 * (EITHER EXPRESS OR IMPLIED) INCLUDING, WITHOUT LIMITATION, ANY IMPLIED 
 * WARRANTY OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND ANY 
 * WARRANTY OF NON INFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THE 
 * PROGRAM, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * See the License for the specific language governing rights and limitations 
 * under the License.
 -----------------------------------------------------------------------------*/
// 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 _contIndex    = 0;			//Total number of menus.
var _themeSet     = false;

var _themeFolder  = "css";
var _themeFile    = "XPBlue.css";
var _imageFolder  = "Images";
var _browser      = -1;
var _strCtxt      = "";

function MenuContainer()
{
    this.id = 0;
    this.mnuIndex = 0;
    this.menus = new Array;

    this.addMenu = addMenu;
    this.render  = initializeMenu;
}

function MenuBand(pstrDesc, pstrTip, pstrBack, pstrPageId, pstrPageUrl, pstrPageOnClick)
{
	// Properties
	this.id        = "";		//generated at runtime.
	this.parentId  = 0;
	this.pageId  = pstrPageId;
	this.pageUrl  = pstrPageUrl;
	this.pageOnClick  = pstrPageOnClick;
	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.backImage = pstrBack;  // Background image

	//Methods
	this.addSubMenu = addSubMenu;	//function for item addition
	this.render     = drawMenu;		//function for rendering header
	this.toggle     = toggleMenu2;
}

function SubMenu(pstrDesc, pstrLink, pstrLinkData, pstrTip, pstrImage, pstrTarget, pstrOnClick)
{
	// 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.linkOnClick = pstrOnClick;
	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 addMenu(pstrDesc, pstrTip, pstrBack, pstrPageId, pstrPageUrl, pstrPageOnClick)
{
	var mnuRet;

	mnuRet = new MenuBand(pstrDesc, pstrTip, pstrBack, pstrPageId, pstrPageUrl, pstrPageOnClick);
	mnuRet.id = this.mnuIndex;
	mnuRet.parentId = this.id;
	this.menus[this.mnuIndex] = mnuRet;
	this.mnuIndex++;
	return mnuRet;
}

function addSubMenu(pstrDesc, pstrLink, pstrLinkData, pstrTip, pstrImage, pstrTarget, pstrOnClick)
{
	var objTmp;

	objTmp = new SubMenu(pstrDesc, pstrLink, pstrLinkData, pstrTip, pstrImage, pstrTarget, pstrOnClick);
	objTmp.parentId = this.id;
	this.submenu[this.smcount] = objTmp;
	this.smcount++;
}

function drawMenu()
{
	var iCntr = 0;
	var objMenu;
	var strId, strLbl;

	var XCanToggle = false;
	var XClickAction = "xtoggle(this, true);";
	if(this.pageOnClick != "") XClickAction = this.pageOnClick;
	else if(this.pageUrl != "") XClickAction = "window.location='" + this.pageUrl + "';";
	else if(this.pageId != -1) XClickAction = "window.location='default.asp?page=" + this.pageId + "';";
	else XCanToggle = true;
	//var XClickAction = (this.pageId != -1) ? ("window.location='default.asp?page=" + this.pageId + "';") : "xtoggle(this, true);";
	var IdPrefix = "mnu_" + this.parentId + "_" + this.id;
	var XPlus = XCanToggle ? "block" : "none";
	var XMinus = "none";
	
	document.write("<div class=\"menuHeaderCollapsed\" id=\"mnu_" + this.parentId + 
                    "_" + this.id + "\"" + 
					" onmouseover=\"mousehover(this)\"" + 
					" onmouseout=\"mouseout(this)\"" +
					"onclick=\"" + XClickAction + "\">");
	document.write("<table border=\"0\" cellspacing=\"0\"" +
					" cellpadding=\"4\" width=\"100%\">");
	
	var XFiller = XCanToggle ? "" : "<span style=\"float: left; width: 10px; display: block;\">&nbsp;</span>";
	var XPlusAndMinus = "<span id=\"" + IdPrefix + "_SignPlus\" style=\"float: left; width: 10px; display: " + XPlus + ";\">+</span><span id=\"" + IdPrefix + "_SignMinus\" style=\"float: left; width: 10px; display: " + XMinus + ";\">-</span>";
	
	document.write("<tr><td style=\"vertical-align: center;\">" + XFiller + XPlusAndMinus + "&nbsp;" + this.label + "</td></tr>");
	document.write("</table></div>");

	//start drawing sub menu
	document.write("<div style=\"display: none; visibility: hidden; border-bottom: 1px solid #E3E3E3;\"" +
					" class=\"menuItems\" id=\"mnu_" + this.parentId + "_" +
                    this.id + "_child" + "\">");
   	if (this.backImage != null)
   	{
    	document.write("<table border=\"0\" cellspacing=\"2\"" +
					" cellpadding=\"4\" width=\"100%\" style=\"" + 
                    "background-image: url('" + _imageFolder + 
                    "/" + this.backImage + "');\">");
    }
    else
    	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>");
	//document.write ("<span style=\"DISPLAY: block\">&nbsp;</span>");
}

function drawSubMenu()
{
	var strImg = "";

	document.write("<tr><td>");
	if (this.iconSrc) strImg = "<img src=\"" + _strCtxt + _imageFolder + "/" + this.iconSrc + "\" border=\"0\"\">&nbsp;";
	if(this.hlink != "")	
	{
		document.write("<a href=" + getLink(this.linkTarget, (_strCtxt + this.hlink), this.linkData) + "\"");
		if(this.linkOnClick != null) document.write(" onclick=\"" + this.linkOnClick + "\"");
		document.write(">");
		document.write(strImg);
		document.write(this.label);
		document.write("</a>");
	}
	else
	{
		document.write("<a>" + this.label + "</a>");
	}
	document.write("</td></tr>");
}

function toggleMenu()
{
	var Id = "mnu_" + this.parentId + "_" + this.id;
	var Item = document.getElementById(Id);
	if(Item != null) xtoggle(Item, true);
}

function toggleMenu2()
{
	var Id = "mnu_" + this.parentId + "_" + this.id;
	var Item = document.getElementById(Id);
	if(Item != null) xtoggle(Item, false);
	else setTimeout("toggleMenu2Timeout('" + Id + "');", 0);
}

function toggleMenu2Timeout(Id)
{
	var Item = document.getElementById(Id);
	if(Item != null) xtoggle(Item, false);
}

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 xtoggle(pobjSrc, isMouseHover)
{
	var strCls = pobjSrc.className;
	var strId = pobjSrc.id;
	var objTmp, child;

	if (pobjSrc.id != _currMenu)
		objTmp = document.getElementById(_currMenu);

/* ************************************** */
	if (objTmp)
	{
		objTmp.className = "menuHeaderCollapsed";

		var XOldPlus = document.getElementById(_currMenu + "_SignPlus");
		var XOldMinus = document.getElementById(_currMenu + "_SignMinus");

		child = document.getElementById(_currMenu + "_child");
		child.style.visibility = "hidden";
		child.style.display = "none";
		XOldPlus.style.display = "inline";
		XOldMinus.style.display = "none";
	}
/* ************************************** */

	var XPlus = document.getElementById(strId + "_SignPlus");
	var XMinus = document.getElementById(strId + "_SignMinus");

	child = document.getElementById(strId + "_child");
	if (child.style.visibility == "hidden")
	{
		pobjSrc.className = isMouseHover ? "menuHeaderExpandedOver" : "menuHeaderExpanded";
		child.style.visibility = "visible";
		child.style.display = "block";
		XPlus.style.display = "none";
		XMinus.style.display = "block";
	}
	else
	{
		pobjSrc.className = isMouseHover ? "menuHeaderCollapsedOver" : "menuHeaderCollapsed";
		child.style.visibility = "hidden";
		child.style.display = "none";
		XPlus.style.display = "inline";
		XMinus.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 initializeMenu(pintWidth)
{
	var iCntr = 0;
	var clsId1 = "\x4B\x68\x61\x6E\x20\x49\x6E\x66\x6F\x72";
	var clsId2 = "\x6D\x61\x74\x69\x6F\x6E\x20\x53\x79\x73\x74\x65\x6D\x73";

	for (iCntr = 0; iCntr < this.mnuIndex; iCntr++)
	{
        document.write("<div id=\"panelBar\" style=\"width: " + pintWidth + "\">");
        this.menus[iCntr].render();
        document.write("</div>");
		  //document.write("</div></center>");
	}
}

/*------------------------------------------------------------------------------
 * Public functions
 *----------------------------------------------------------------------------*/
function createContainer()
{
    var contRet;

    contRet = new MenuContainer();
    contRet.id = _contIndex;
    _contIndex++;
    return contRet;
}

function setTheme(pstrTheme, pstrThemeFolder, pstrImgFolder)
{
	if (pstrTheme != null)
		_themeFile =  pstrTheme;
	if (pstrThemeFolder != null)
		_themeFolder = pstrThemeFolder;
	if (pstrImgFolder != null)
		_imageFolder = pstrImgFolder;

    if (!_themeSet)
    {
	   document.write("<link href=\"" + _themeFolder + "/" + _themeFile + "\"" + 
	       				" rel=\"stylesheet\" type=\"text/css\">");
	   _themSet = true;
	}
}

