/////////////////////////////////////////////////////////////////////////////
// Function : topUpperMenu
// Comments : 
/////////////////////////////////////////////////////////////////////////////

function TopUpperMenu(showHome, menuWidth, buttonWidth, tableId, tableClassName,
		rowClassName, cellClassName, selectedCellClassName, spacerImage, doDebug)
{
	try {
		this.m_TextColor  = '';
		this.m_HoverColor = '';
		this.m_FocusColor = '';
		this.m_menuWidthValue = '';
		this.m_menuWidthUnit = ''; // we want the unit so we can use the same for button width
		this.m_buttonWidth = '';
		this.m_tableId = '';
		this.m_tableClassName  = '';
		this.m_rowClassName = '';
		this.m_cellClassName = '';
		this.m_selectedCellClassName = '';
		this.m_spacerImageUrl = '';
		this.m_debug = false;
		
		this.m_showHome   = false;
		this.m_NavPath    = g_navNode_Path;
				
		TopUpperMenu.prototype.display = topUpperMenu_Display;
		
		
		if (buttonWidth != '') {
			this.m_buttonWidth = buttonWidth;
			this.m_menuWidth = ''; // button width overrides menu width
		}
		else {
			if (menuWidth != '') {
				this.m_menuWidthUnit = menuWidth.replace(/[\d\s\.]/g, ""); // get px, pt, %, whatever
				this.m_menuWidthValue = parseInt(menuWidth, 10); // get value
			}		
		}
		
		if (tableId != '')
			this.m_tableId = tableId;
		
		if (tableClassName != '')
			this.m_tableClassName = tableClassName;
	
		if (rowClassName != '')
			this.m_rowClassName = rowClassName;
	
		if (cellClassName != '')
			this.m_cellClassName = cellClassName;
			
		if (selectedCellClassName != '')
			this.m_selectedCellClassName = selectedCellClassName;
			
		if (showHome == 'true')
			this.m_showHome = true;
			
		if (spacerImage != '')
			this.m_spacerImageUrl = spacerImage;

		if (doDebug)
			this.m_debug = true;
	}
	catch (e) {
		g_errorHandler('TopUpperMenu', e);
	}
}

function topUpperMenu_Display (node)	
{
	var bSelected = false;
		
	var ds = new Array();
	var di = 0;
	var count =  this.m_showHome ? -1 : 0;

	var href = '';
	var label = '';

	// Start the table that will hold the menu
	ds[di++] = '<table cellspacing="0" cellpadding="0" border="0"';
	
	if (this.m_tableId != '')
		ds[di++] = ' id="' + this.m_tableId + '"';

	if (this.m_tableClassName != '')
		ds[di++] = ' class="' + this.m_tableClassName + '"';

	// append width if specified
	if (this.m_menuWidthValue != '')
		ds[di++] = ' style="width: ' + this.m_menuWidthValue + this.m_menuWidthUnit + '"';
		
	ds[di++] = '><tr'; 

	if (this.m_rowClassName != '')
		ds[di++] = ' class="' + this.m_rowClassName + '"';
		
	ds[di++] = '>';

	//Counted keep buttons correct width even if a section is hidden
	var buttonCount = node.m_subNodes.length;
	for (count = 0;count<node.m_subNodes.length;count++)
	{
		if (node.m_subNodes[count].cp_HideTopLevelSection == 'TRUE' || (!g_isAdmin && node.m_subNodes[count].cp_AdminOnly == 'TRUE'))
		{
			if (this.m_debug)
				alert('Reducing button count; found node ' + node.m_subNodes[count].m_label + ' where hidden is ' + node.m_subNodes[count].cp_HideTopLevelSection);
	
			buttonCount--;
		}	
	}
	// if we got an explicit button width, use that, otherwise based on evenly sized buttons
	if (this.m_menuWidthValue != '') {
		this.m_buttonWidth = parseInt(this.m_menuWidthValue / buttonCount, 10)
				+ this.m_menuWidthUnit;
	}

	// loop and add the sections to the menu table		
	for (count = 0 ; count < node.m_subNodes.length; count++)
	{			
		//hide section from menu if marked to hide or if admin section (and user is not admin)
		if (node.m_subNodes[count].cp_HideTopLevelSection == 'TRUE' || (!g_isAdmin && node.m_subNodes[count].cp_AdminOnly == 'TRUE'))
		{
			continue;
		}
		
		bSelected = false;
		
		if (count == -1)	// Root link
		{
			if ( (this.m_NavPath.length == node.m_level+1) &&
				 (this.m_NavPath[node.m_level] == node.m_id) )
			{
				bSelected = true;
			}
			
			label = node.m_label;
			href  = node.m_href;
		}
		else
		{	
			if (this.m_NavPath.length > node.m_subNodes[count].m_level)
			{
				if (this.m_NavPath[node.m_subNodes[count].m_level] == node.m_subNodes[count].m_id)
				{
					bSelected = true;
				}
			}
			
			label = node.m_subNodes[count].m_label;
			href = node.m_subNodes[count].m_href;
		}
		
		var nodeClass = this.m_cellClassName;
		if (bSelected && this.m_selectedCellClassName != '')
		{
			nodeClass = this.m_selectedCellClassName;
		}
		
		ds[di++] = '<td valign="middle"';
		if (nodeClass != '')
			ds[di++] = ' class="' + nodeClass + '"';
		ds[di++] = ' onMouseOver="this.style.cursor=\'pointer\'" onMouseOut="this.style.pointer=\'arrow\'"';
		ds[di++] = ' onClick="location=\'' + href + '\'"';
		
		if (this.m_buttonWidth != '' && (count + 1) < node.m_subNodes.length)
			ds[di++] = ' style="width: ' + this.m_buttonWidth + '"';
			
		ds[di++] = '>';
		
		// add spacer image except on last button; we add at the beginning of the cell and float right
		if (this.m_spacerImageUrl != '' && (count + 1) < node.m_subNodes.length)
			ds[di++] = '<img src="' + this.m_spacerImageUrl + '" style="float: right" />';

		ds[di++] = '<a href="' + href + '"';
		ds[di++] = ' class="' + nodeClass + '"';
		
		ds[di++] = '>';
		ds[di++] = label;
		ds[di++] = '</a>';
		ds[di++] = "</td>";
	} 

	ds[di++] = '</tr></table>';
	
	if (this.m_debug)
		alert(ds.join(''));
		
	document.write(ds.join(''));
}
