//**********************************************************************
// MegaBitz Web Site Index
// Copyright © 2002-2004, MegaBitz Engineering. All rights reserved.
// December 12, 2004
//**********************************************************************

function fStartUp()
//**********************************************************************
// Called by the HTML document once loaded,
//**********************************************************************
{
	fLoadData();
	fDrawTree();
	return;
}

function fDrawTree()
//**********************************************************************
// Starts the recursive tree drawing process by first writing the root
// node, and then all subordinate branches.
// These Work: parent, top - or parent if not the top window!
//**********************************************************************
{
	var zstr = "";
	xFrame = parent.eval(xDefIndex).window.document;
	xFrame.open("text/html");
	zstr  = "<HTML>\n<HEAD>\n<STYLE><!--\n";
	zstr += "span.index	{clear:none; line-height:16px; margin:0em; padding:0em; letter-spacing:0em; word-spacing:0em; "
		  + "vertical-align:middle; text-align:left; font-family:Arial; font-size:9pt; font-weight:normal; font-style:normal; "
		  + "color:#000000; background:#ffffff; cursor:pointer; cursor:hand;}";
	zstr += "\n--></STYLE>\n</HEAD>\n";
	zstr += "<BODY BGCOLOR='" + xBGColor + "' BACKGROUND='" + xBGImage;
	zstr += "'>\n";
	xFrame.write(zstr);

	zstr  = "<span class='index' onMouseOver=\"window.status='" + xt[1].tip + "'; return true;\" onClick=\"parent.loadPage(\'" + xt[1].url + "\');\">";
	zstr += "<IMG SRC='" + xt[1].icon + "' WIDTH=16 HEIGHT=18 ALIGN=TOP BORDER=0 ALT='" + xt[1].tip + "'>";
	zstr += "<b>&nbsp;" + xt[1].name + "</b></span><br>\n";
	xFrame.write(zstr);

	fDrawBranch("root","");
	zstr = "</BODY>\n</HTML>";
	xFrame.write(zstr);
	xFrame.close();
	window.status="MegaBitz Engineering";
	return;
}

function fDrawBranch(pStartNode,pStructString)
//**********************************************************************
// Used by the fDrawTree() function to recursively draw all visable nodes
// in the tree structure.
//**********************************************************************
{
	var xc = fGetChildNodes(pStartNode);
	var i  = 0;
	while(i < xc.length)
		{
		i++;
		xFrame.write(pStructString);
		if (xc[i].type == 'link')
			{
			var zstr = "";
			if (xc[i].target == "_blank")
				zstr = "<span class='index' onMouseOver=\"window.status='" + xc[i].tip + "'; return true;\" onClick=\"parent.loadSite('" + xc[i].url + "');\">";
			else
				zstr = "<span class='index' onMouseOver=\"window.status='" + xc[i].tip + "'; return true;\" onClick=\"parent.loadPage('" + xc[i].url + "');\">";
			if (i != xc.length)
				zstr += "<IMG SRC='" + xBrnchCont + "' vspace=0 WIDTH=19 HEIGHT=18 ALIGN=TOP BORDER=0 ALT='" + xc[i].tip + "'>";
			else
				zstr += "<IMG SRC='" + xBrnchEnd  + "' vspace=0 WIDTH=19 HEIGHT=18 ALIGN=TOP BORDER=0 ALT='" + xc[i].tip + "'>";
			zstr += "<IMG SRC='" + xc[i].icon + "' vspace=0 WIDTH=16 HEIGHT=18 ALIGN=TOP BORDER=0 ALT='" + xt[i].tip + "'>";
			zstr += "&nbsp;" + xc[i].name + "</span><br>\n";
			xFrame.write(zstr);
			}
		else
			{
			var zstr = "";
			var zcl  = "";
			var zpm;
			var zic;
			var zlt;
			if (xc[i].open)
				zcl = "0";
			else
				zcl = "1";
			if (xc[i].open)
				zic = xc[i].iconOpen;
			else
				zic = xc[i].iconClosed;
			if (i < xc.length)
				zlt = xVertLine;
			else
				zlt = xBlank;
			if ((i < xc.length) && (xc[i].open))
				zpm = xMinusCont;
			else if (i < xc.length)
				zpm = xPlusCont;
			else if (xc[i].open)
				zpm = xMinusEnd;
			else
				zpm = xPlusEnd;
			zstr = "";
			zstr = "<span class='index' onMouseOver=\"window.status='" + xc[i].tip + "'; return true;\" onClick=\"javascript:parent.fToggleFolder('" + xc[i].id + "'," + zcl + ")\">";
			zstr += "<IMG SRC='" + zpm + "' vspace=0 WIDTH=19 HEIGHT=18 ALT='" + xc[i].tip + "' ALIGN=TOP BORDER=0>";
			zstr += "<IMG SRC='" + zic + "' vspace=0 WIDTH=16 HEIGHT=18 ALT='" + xc[i].tip + "' ALIGN=TOP BORDER=0>";
			if (xc[i].url == "")
				zstr += "&nbsp;" + xc[i].name + "</span><br>\n";
			else
				{
				zstr += "</span>";
				zstr += "<span class='index' onMouseOver=\"window.status='" + xc[i].tip + "'; return true;\" onClick=\"parent.loadPage('" + xc[i].url + "');\">";
				zstr += "&nbsp;" + xc[i].name + "</span><br>\n";
				}
			xFrame.write(zstr);
			if (xc[i].open)
				{
				zstr = pStructString + "<IMG SRC='" + zlt + "' vspace=0 WIDTH=19 HEIGHT=18 ALIGN=TOP BORDER=0>";
				fDrawBranch(xc[i].id,zstr); 
				}
			}
		}
	return;
}

function fToggleFolder(pId,pStatus)
//**********************************************************************
// Opens/Closes folder nodes.
//**********************************************************************
{
	var i = fGetNodeIndex(pId); 
	xt[i].open = pStatus; 
	fDrawTree();
	return;
}

function fGetNodeIndex(pId)
//**********************************************************************
// Finds the index in the xt Collection of the node with the given id.
//**********************************************************************
{
	var i = 0;
	while(i < xt.length)
		{
		i++;
		if ((xt[i].type == 'root') || (xt[i].type == 'folder'))
			if (xt[i].id == pId) return(i);
		}
	return(-1);
}

function fGetChildNodes(pNode)
//**********************************************************************
// Extracts and returns a Collection containing all of the node's
// immediate child nodes.
//**********************************************************************
{
	var xc = new Collection();
	var i  = 0;
	while(i < xt.length)
		{
		i++;
		if ((xt[i].type == 'folder') || (xt[i].type == 'link'))
			{
			if (xt[i].parent == pNode)
				xc.add(xt[i]);
			}
		} 
	return(xc);
}

function Collection()
//**********************************************************************
// OBJECT : A dynamic storage structure similar to an Array.
//**********************************************************************
{
	this.length = 0; 
	this.add    = add; 
	return(this);
}

function add(pObject)
//**********************************************************************
// METHOD of Collection
// Adds an object to a Collection.
//**********************************************************************
{
	this.length++; 
	this[this.length] = pObject;
	return;
}

function RootNode(pId,pName,pUrl,pTip)
//**********************************************************************
// OBJECT : Represents the top-most node of the hierarchial tree.
//**********************************************************************
{
	this.type   = 'root';
	this.id     = pId;
	this.name   = pName;
	this.url    = pUrl;
	this.target = '';
	this.icon   = '';
	this.tip    = pTip;
	if (this.target == "")
		this.target = xDefFrame;
	if (this.icon == "")
		this.icon   = 'mbxroot.gif';
	if (this.tip == "")
		this.tip    = pName;
	this.icon = xImageURL + this.icon;
	return(this);
}

function FolderNode(pId,pParent,pName,pUrl,pTip)
//**********************************************************************
// OBJECT : Represents a node which branches to contain other nodes.
//**********************************************************************
{
	this.type       = 'folder';
	this.id         = pId;
	this.parent     = pParent;
	this.name       = pName;
	this.url        = pUrl;
	this.iconClosed = '';
	this.iconOpen   = '';
	this.open       = 0;
	this.tip        = pTip;
	if (this.iconClosed == "")
		this.iconClosed = "mbxfldrc.gif";
	if (this.iconOpen == "")
		this.iconOpen   = "mbxfldro.gif";
	if (this.tip == "")
		this.tip = pName;
	this.iconClosed = xImageURL + this.iconClosed;
	this.iconOpen   = xImageURL + this.iconOpen;
	return(this);
}

function LinkNode(pParent,pName,pUrl,pTip,pTarget)
//**********************************************************************
// OBJECT : A node that represents a link using a URL.
//**********************************************************************
{
	this.type   = 'link';
	this.parent = pParent;
	this.name   = pName;
	this.url    = pUrl;
	this.tip    = pTip;
	this.target = pTarget;
	this.icon   = '';
	if (this.tip == "")
		this.tip    = pName;
	if (this.target == "")
		this.target = xDefFrame;
	if (this.icon == "")
		this.icon   = xLinkIcon;
	this.icon = xImageURL + this.icon;
	return(this);
}

function loadPage(p_url)
//=================================================================================================
// Open the specified URL in the standard body frame
//=================================================================================================
{
	zframe = open(p_url,xDefFrame);
	zframe.close();
	return;
}

function loadSite(p_url)
//=================================================================================================
// Open the specified URL in an entirely new window
//=================================================================================================
{
	winRef = window.open(p_url,"_blank");
	return;
}

function openPopup(strURL,iWidth,iHeight)
//=================================================================================================
// Display something in a popup window
// eg.
// <a href="javascript:void(0);"
//    onClick="javascript:openPopup('images/ftpics/muma.jpg',800,600);">
//    Muma's Picture</a>
//=================================================================================================
{
	var iScreenHeight = 480; // default window height
	var iScreenWidth  = 640; // default window width
	if (parseInt(navigator.appVersion) > 3) // get screen height
		{
		iScreenHeight = screen.availHeight;
		if (!iScreenHeight)
			iScreenHeight = screen.height;
		}
	else if ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) == 3) && (navigator.javaEnabled()))
		{
		var jToolkit    = java.awt.Toolkit.getDefaultToolkit();
		var jScreenSize = jToolkit.getScreenSize();
		iScreenHeight   = jScreenSize.height;
		}
	if (parseInt(navigator.appVersion) > 3) // get screen width
		{
		iScreenWidth = screen.availWidth;
		if (!iScreenWidth)
			iScreenWidth = screen.width;
		}
	else if ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) == 3) && (navigator.javaEnabled()))
		{
		var jToolkit    = java.awt.Toolkit.getDefaultToolkit();
		var jScreenSize = jToolkit.getScreenSize();
		iScreenWidth    = jScreenSize.width;
		}
	if (iHeight > iScreenHeight)
		iHeight = iScreenHeight - 80;
	if (iWidth  > iScreenWidth)
		iWidth  = iScreenWidth  - 80;
	var openPopup = window.open(strURL,'popup','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=' + iWidth + ',height=' + iHeight);
	openPopup.focus();
	return;
}

