var menuBoxTop = 0;			// position of menu box within menu container
var rootMenuItem = null;	// the root menu

// define menu item object
function MenuItem( id, parentId, label, href )
{
	this.id = id;
	this.label = label;
	this.href = href;
	this.subMenu = [];
	this.expanded = false;
	this.parentItem = null;
	if (parentId != null)
	{
		eval( "this.parentItem = menuItem" + parentId );
		this.parentItem.subMenu[ this.parentItem.subMenu.length ] = this;
		this.depth = this.parentItem.depth + 1;
	}
	else
	{
		rootMenuItem = this;
		this.depth = 0;
	}
}

// when user clicks on scroll button...
function scrollMenuBox( direction )
{
	if (direction == 'up')	// scroll up => move box down
	{
		if (menuBoxTop < 0)
			menuBoxTop += scrollAmount;
	}
	else	// scroll down => move box up
	{
		if (menuBoxTop + getMenuBoxHeight() > menuHeight)
			menuBoxTop -= scrollAmount;
	}
	setMenuBoxTop();
}

var scrollTimer = null;

function startScrolling( direction )
{
	stopScrolling();
	scrollMenuBox( direction );
	scrollTimer = setInterval( "scrollMenuBox('" + direction + "')", scrollInterval );
}

function stopScrolling()
{
	if (scrollTimer != null)
	{
		clearInterval( scrollTimer );
		scrollTimer = null;
	}
}

function getMenuBoxHeight()
{
	if (document.getElementById)
		return document.getElementById( 'menuBox' ).offsetHeight;
	else if (document.all)
		return document.all.menuBox.offsetHeight;
	else
		return document.menuContainer.document.menuBox.document.height;
}

function setMenuBoxTop()
{
	if (document.getElementById)
		document.getElementById( 'menuBox' ).style.top = menuBoxTop;
	else if (document.all)
		document.all.menuBox.style.top = menuBoxTop;
	else
		document.menuContainer.document.menuBox.top = menuBoxTop;
}

function expandMenu( menuItemNumber )
{
	var menuItem = eval( 'menuItem' + menuItemNumber );
	if (smartExpand)
	{
		collapseMenuRecurse( rootMenuItem, menuItem );
		menuItem.expanded = true;
		while ((menuItem = menuItem.parentItem) != null)
			menuItem.expanded = true;
	}
	else
		menuItem.expanded = true;
	drawMenu();
	setMenuBoxTop();
}

function collapseMenu( menuItemNumber )
{
	if (smartCollapse)
		eval( 'collapseMenuRecurse( menuItem' + menuItemNumber + ' )' );
	else
		eval( 'menuItem' + menuItemNumber + '.expanded = false' );
	drawMenu();
	setMenuBoxTop();
}

function collapseMenuRecurse( menuItem, ignoreItem )
{
	if (menuItem == ignoreItem)
		return;
	menuItem.expanded = false;
	for (var i = 0; i < menuItem.subMenu.length; i++)
		collapseMenuRecurse( menuItem.subMenu[i], ignoreItem );
}

function drawMenu()
{
	var menuBoxText = drawMenuRecurse( rootMenuItem );
	if (document.getElementById)
		document.getElementById( 'menuBox' ).innerHTML = menuBoxText;
	else if (document.all)
		document.all.menuBox.innerHTML = menuBoxText;
	else if (document.layers)
	{
		document.menuContainer.document.open();
		document.menuContainer.document.write( '<layer name="menuBox">' + menuBoxText + '</layer>' );
		document.menuContainer.document.close();
	}
	else
		window.alert( 'Sorry... your browser does not support advanced dynamic HTML' );
}

function drawMenuRecurse( menuItem )
{
	// start table for menu items in this menu
	var menuBuffer =
			'<table border="0" cellpadding="0" cellspacing="0" width="' +
			(menuWidth - menuItem.depth * (menuPlusBoxWidth + menuInsetWidth)) + '">';

	// loop thru menu items
	for (var i in menuItem.subMenu)
	{
		with (menuItem.subMenu[i])
		{
			// cell for plus/minus image
			menuBuffer += '<tr valign="top"><td width="' + menuPlusBoxWidth + '">';
			// if it has a sub menu, and the sub menu is expanded...
			if (subMenu.length > 0 && expanded)
			{
				menuBuffer +=
						'<a class="menu" href="javascript:collapseMenu(' + id + ');">' + menuMinusImage + '</a>';
			}
			// if it has a sub menu, and the sub menu is collapsed...
			else if (subMenu.length > 0)
			{
				menuBuffer += '<a class="menu" href="javascript:expandMenu(' + id + ');">' + menuPlusImage + '</a>';
			}
			menuBuffer += '</td>' +
			// cell for label
			'<td colspan="2" width="' +
			(menuWidth - menuItem.depth * (menuPlusBoxWidth + menuInsetWidth) - menuPlusBoxWidth) + '" class="menu">';

			// if there's an href, use it, else use the expand/collapse option
			if (href != '')
				menuBuffer += '<a class="menu" href="' + href + '" target="' + targetFrame + '">' + label + '</a>';
			else if (subMenu.length > 0 && expanded)
				menuBuffer += '<a class="menu" href="javascript:collapseMenu(' + id + ');">' + label + '</a>';
			else if (subMenu.length > 0)
				menuBuffer += '<a class="menu" href="javascript:expandMenu(' + id + ');">' + label + '</a>';
			else
				menuBuffer += label;

			menuBuffer += '</td></tr>';
			if (expanded)
			{
				// make a cell for the sub menu's table
				menuBuffer +=
						'<tr>' +
						'<td width="' + menuPlusBoxWidth + '"></td><td width="' + menuInsetWidth + '"></td><td>' +
						// draw sub menu
						drawMenuRecurse( menuItem.subMenu[i] ) +
						'</td>' +
						'</tr>';
			}
		}
	}
	menuBuffer += '</table>';
	return menuBuffer;
}

function writeMenu()
{
	if (document.layers)
	{
		if (!disableScrollBars)
		{
			document.write( '<layer name="scrollup" top="' + (menuTop - scrollBoxHeight) + '" left="' + (menuLeft + menuWidth - scrollBoxWidth) + '" height="' + scrollBoxHeight + '" width="' + scrollBoxWidth + '"><a href="javascript:scrollMenuBox(\'up\');" onMouseOver="startScrolling(\'up\')" onMouseOut="stopScrolling()">' + scrollUpImage + '</a></layer>' );
			document.write( '<layer name="scrolldown" top="' + (menuTop + menuHeight) + '" left="' + (menuLeft + menuWidth - scrollBoxWidth) + '" height="' + scrollBoxHeight + '" width="' + scrollBoxWidth + '"><a href="javascript:scrollMenuBox(\'down\');" onMouseOver="startScrolling(\'down\')" onMouseOut="stopScrolling()">' + scrollDownImage + '</a></layer>' );
		}
		document.write( '<layer name="menuContainer" top="' + menuTop + '" left="' + menuLeft + '" height="' + menuHeight + '" width="' + menuWidth + '" clip="' + menuWidth + ',' + menuHeight + '"><layer name="menuBox" width="' + menuWidth + '">' + drawMenuRecurse( rootMenuItem ) + '</layer></layer>' );
	}
	else
	{
		if (!disableScrollBars)
		{
			document.write( '<div id="scrollup" style="position: absolute; top: ' + (menuTop - scrollBoxHeight) + 'px; left: ' + (menuLeft + menuWidth - scrollBoxWidth) + 'px; height: ' + scrollBoxHeight + 'px; width: ' + scrollBoxWidth + 'px;"><a href="javascript:scrollMenuBox(\'up\');" onMouseOver="startScrolling(\'up\')" onMouseOut="stopScrolling()">' + scrollUpImage + '</a></div>' );
			document.write( '<div id="scrolldown" style="position: absolute; top: ' + (menuTop + menuHeight) + 'px; left: ' + (menuLeft + menuWidth - scrollBoxWidth) + 'px; height: ' + scrollBoxHeight + 'px; width: ' + scrollBoxWidth + 'px;"><a href="javascript:scrollMenuBox(\'down\');" onMouseOver="startScrolling(\'down\')" onMouseOut="stopScrolling()">' + scrollDownImage + '</a></div>' );
		}
		document.write( '<div id="menuContainer" style="position: absolute; top: ' + menuTop + 'px; left: ' + menuLeft + 'px; height: ' + menuHeight + 'px; width: ' + menuWidth + 'px; clip:rect(0,' + menuWidth + ',' + menuHeight + ',0); overflow: hidden;"><div id="menuBox" style="position: absolute; top: 0px; left: 0px; width: ' + menuWidth + 'px;">' + drawMenuRecurse( rootMenuItem ) + '</div></div>' );
	}
}

