var otherLoadFunction = null;
var maxLevels = 3; // this isn't as variable as it looks
// This is some of the skeletal HTML required to make the menus work.
var cellInternals = ['', 
'	<div id="secpd" style="visibility: hidden;">'+
'		<table cellpadding="0" cellspacing="0" align="left">'+
'			<tr>'+
'				<td><span style="background-position: bottom" class="popupMenusHorizLine"/></td>'+
'			</tr>'+
'			<tr>'+
'				<td id="secct" valign="top" class="mb" height="200"></td>'+
'			</tr>'+
'			<tr>'+
'				<td><span style="background-position: top" class="popupMenusHorizLine"/></td>'+
'			</tr>'+
'		</table>'+
'	</div>',
'	<div id="thrpd" style="visibility: hidden;">'+
'		<table cellpadding="0" cellspacing="0" align="left">'+
'			<tr>'+
'				<td><span style="background-position: bottom" class="popupMenusHorizLine"/></td>'+
'			</tr>'+
'			<tr>'+
'				<td id="thrct" valign="top" class="mb" height="200"></td>'+
'			</tr>'+
'			<tr>'+
'				<td><span style="background-position: top" class="popupMenusHorizLine"/></td>'+
'			</tr>'+
'		</table>'+
'	</div>'];
var firstMenuSetArea; // first level menus get placed in created table
var subMenuSetArea; // the rest of the menus get placed after the table
// These class names and ids have significance in popMenu.js and the stylesheet.
var cellClasses = new Array("bm", "htd", "htd"); // each of 3 cells gets a class name
var cellIds = new Array("", "second", "third"); // each cell gets a unique id
// These are required for the submenus.  If more submenus need to be supported,
// this array can simply be lengthened with more spelled out numbers.  The system
// will automatically use the longer array.
var spelledNumerals = new Array("first", "second", "third", "fourth", "fifth", 
								"sixth", "seventh", "eighth", "ninth", "tenth", 
								"eleventh", "twelvth", "thirteenth", "fourteenth", "fifteenth");
var classNamesForLinksToPopUpMenus = new Array("ll", "l2");
var subMenuCount = 0; // this is a static counter, since menu 1.5 is "fifth", and 2.1 is "sixth"
var tabIndexCount = 50; // this is a static counter, since no anchor tag should share a tabindex with another

// Builds the skeletal table to store the popup menus in
function buildMenuTable()
{
	var tbl, tbody, tr, td;
	tbl = document.createElement('table');
	tbody = tbl.getElementsByTagName("tbody")[0];
	tbl.cellPadding = 0;
	tbl.cellSpacing = 0;
	tr = tbl.insertRow(0);
	for( var i = 0; i < maxLevels; i++ )
	{
		tr.appendChild( td = document.createElement('td') );
		td.className = cellClasses[i];
		if( cellIds[i] ) td.id = cellIds[i];
		td.innerHTML = cellInternals[i];
		if( i == 0 ) firstMenuSetArea = td;
	}
	return tbl;
}

// Performs the transformation of the nice bulleted list
// into the ugly HTML tables that work the pop-up menus.
function onLoad()
{
	var layerMenus = document.getElementById('layerMenus');
	var bl = getImmediateChildrenByTagName(layerMenus, 'UL')[0];
	if( !bl ) return; // If no bulleted list was found, skip out.

	var targetArea = layerMenus.cloneNode(false);
	targetArea.appendChild( buildMenuTable() ); // build the skeletal table
	subMenuSetArea = targetArea; // the hidden menus appear below the table

	transformHtml(bl, 0, new Array()); // transform html

	// Swap the bulleted list for the newly generated HTML.
	layerMenus.parentNode.replaceChild(targetArea, layerMenus);
	
	preloadmenus();
	
	if( otherLoadFunction ) otherLoadFunction();
}

// Performs the grunge work of actually reading the bulleted list
// and producing the ugly html.
function transformHtml(ul, depth, path)
{
	// This could be the top-level of the bulleted list,
	// or an inner UL tag that signifies a hidden menu.  
	// The "ul" parameter reference the current UL tag being parsed.
	// The "depth" parameter tells us how deep we are in recursion.
	// The "path" parameter is an array of indexes in the menuing system
	// that tells us how we got to this hidden menu (if applicable).
	var targetArea;
	switch( depth )
	{
		case 0:
			targetArea = firstMenuSetArea;
			break;
		case 1:
			subMenuSetArea.appendChild( targetArea = document.createElement('div') );
			targetArea.className = 'hd';
			targetArea.id = spelledNumerals[path[0]] + '_menu';
			break;
		case 2:
			subMenuSetArea.appendChild( targetArea = document.createElement('div') );
			targetArea.className = 'hd';
			targetArea.id = spelledNumerals[subMenuCount++] + '_submenu';
			break;			
	}
	// Get all the immediate members of this particular menu
	var liList = getImmediateChildrenByTagName(ul, 'LI');
	for( var i = 0; i < liList.length; i++ )
	{
		var li = liList[i];
		// Prepare to add this LI element's children to the appropriate
		// table cell or div area.
		var div = document.createElement('div');
		targetArea.appendChild( div );
		// Look for sub-menus of this menu item.  If one is found, mark it for
		// recursion later, and meanwhile copy all other nodes into the new location.
		var ulSub = null;
		for( var j = 0; j < li.childNodes.length; j++ )
		{
			var liChild = li.childNodes[j];
			if( liChild.nodeName == "UL" ) 
			{
				ulSub = liChild;
				continue; // skip this
			}
			div.appendChild( liChild.cloneNode(true) );
		}
		div.className = ulSub ? classNamesForLinksToPopUpMenus[depth] : 'lt';
		var anchors = div.getElementsByTagName('A');

		// Make sure an anchor tag exists.  Create it if it doesn't.
		var primaryAnchor;
		if( anchors.length == 0 )
		{
			primaryAnchor = document.createElement('a');
			primaryAnchor.href = "#";
			while( div.childNodes.length )
				primaryAnchor.appendChild( div.childNodes[0] );
			div.appendChild( primaryAnchor );
		} 
		else
			primaryAnchor = anchors[0];
		
		primaryAnchor.className = ulSub ? 'navBranch' : 'navLink';
		primaryAnchor.tabIndex = tabIndexCount++;
		// If this LI element had a child UL element, recurse into that
		if( ulSub ) 
		{
			if( depth == 0 )
				div.id = 'u' + spelledNumerals[i] + '_menu';
			else // assert debug == 1
				div.id = 'u' + spelledNumerals[path[0]] + '_menu.' + spelledNumerals[subMenuCount] + '_submenu';

			var param = div.id.substr(1);

			// Just setting the onclick attribute or the click event
			// seems to cause problems in various browsers.
			// This works every time.
			primaryAnchor.href = "javascript:toggleMenu('" + param + "');";
			
			path[depth] = i;
			transformHtml(ulSub, depth+1, path);
		}
	}
}

otherLoadFunction = window.onload;
window.onload = onLoad;
