/////////////////////////////////////////////////////////////////////////////
// Function : AquaSureNav
// Comments : Builds out the main AquaSure menu displayed in the header of
//            the website.
/////////////////////////////////////////////////////////////////////////////

function AquaSureNav(strTextColor, strHoverColor, strFocusColor, strSeparator, strSeparatorWidth, strClassName, 
							strShowHome, strStartLevel, strNumLevels, strStyle)
{
	this.m_TextColor  = '';
	this.m_HoverColor = '';
	this.m_FocusColor = '';
	this.m_Separator  = '|';
	this.m_SeparatorWidth = '';
	this.m_ClassName  = 'AquaSureNav';
	
	this.m_ShowHome   = false;
	
	this.m_StartLevel = 1;
	this.m_NumLevels  = 1;
	this.m_EndLevel   = 1;
	
	this.m_NavPath    = g_navNode_Path;
	
	this.m_Style = '';
			
	AquaSureNav.prototype.Display = AquaSureNav_Display;
	AquaSureNav.prototype.DisplaySubLevel = AquaSureNav_DisplaySubLevel;
		
	if (strTextColor != '')
		this.m_TextColor = strTextColor;
		
	if (strHoverColor != '')
		this.m_HoverColor = strHoverColor;

	if (strFocusColor != '')
		this.m_FocusColor = strFocusColor;

	if (strSeparator != '')
		this.m_Separator = strSeparator;
	
	if (strSeparatorWidth != '')
		this.m_SeparatorWidth = strSeparatorWidth;

	if (strClassName != '')
		this.m_ClassName = strClassName;

	if (strShowHome == 'true')
		this.m_ShowHome = true;
		
	if (strStartLevel != '')
	{
		var value = parseInt(strStartLevel);
		if (value != NaN)
			this.m_StartLevel = value;
	}
	
	if (strNumLevels != '')
	{
		var value = parseInt(strNumLevels);
		if (value != NaN)
			this.m_NumLevels = value;
	}
	
	if (strStyle != '')
	{
		this.m_Style = strStyle;
	}

	this.m_EndLevel = this.m_StartLevel + this.m_NumLevels - 1 ;
}

function AquaSureNav_Display (node)
{
	document.write('<table width="100%" cellspacing="0" cellpadding="0" border="0">');
	this.DisplaySubLevel(node);	
	document.write('</table>');
}

function AquaSureNav_DisplaySubLevel (node)	
{
	var bSelected = false;
	var bDisplay  = false;

	var nodeColor = this.m_TextColor;
	var nodeClass = this.m_ClassName
	var nodeLevel = node.m_level+1;
	
	if (nodeLevel > 6)
		nodeLevel = 6;
		
	var ds = new Array();
	var di = 0;
	var count = 0;

	var selectedNode = null;

	if (this.m_ShowHome && (node.m_level == 0))
		count = -1;

	var href = '';
	var label = '';
	var id = '';
	
	if (node.m_level >= this.m_StartLevel-1 && node.m_level < this.m_EndLevel)
		bDisplay = true;

	if (bDisplay) 
	{
		ds[di++] = '<tr';
		
		if (nodeLevel > 0)
			ds[di++] = ' class="' + nodeClass + '-' + nodeLevel + '"';
		else
			ds[di++] = ' class="' + nodeClass + '"';
		
		//ds[di++] = '>';
		ds[di++] = '><td nowrap>';
//		ds[di++] = '&nbsp;';
	}	
		
	for ( ; count < node.m_subNodes.length; count++)
	{
		bSelected = false;
		nodeColor = this.m_TextColor;

		if (count == -1)	// Root/home link 
		{	
			// this is only really applicable if we're working the top level
			// but the same technique could be used to display a generalised "back-up-one-level")
			
			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;
			id = node.m_id;
		}
		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;
					selectedNode = node.m_subNodes[count];
				}
			}
			label = node.m_subNodes[count].m_label;
			href = node.m_subNodes[count].m_href;
			id = node.m_subNodes[count].m_id;
		}

		if (bSelected)
		{
			nodeColor = this.m_FocusColor;
			nodeClass = this.m_ClassName + '-focus';
		}
		else
		{
			nodeColor = this.m_TextColor;
			nodeClass = this.m_ClassName;
		}
		
		// Special case for Site Manager, make it red to stand out.
		if(id == 'AS_SiteManager')
		{
			nodeColor = '#990000';
		}

		if (nodeLevel > 0)
			nodeClass += '-' + nodeLevel;

		if (bDisplay)
		{
			ds[di++] = '<a href="' + href + '"';

			ds[di++] = ' class="' + nodeClass + '"';
			
			if (nodeColor != '')
			{
				ds[di++] = ' style="color:' + nodeColor + '; text-decoration: none; ' + this.m_Style + '"';

				if (!bSelected && this.m_HoverColor != '')
				{
					ds[di++] = ' onmouseover="this.style.color=\'' + this.m_HoverColor + '\'"';
					ds[di++] = ' onmouseout="this.style.color=\'' + nodeColor + '\'"';
				}
			}
			
			ds[di++] = '>';
			ds[di++] = label;
			ds[di++] = '</a>';

			if (count < node.m_subNodes.length - 1)
			{
				ds[di++] = '&nbsp;';
				ds[di++] = '<span style="color: ' + this.m_TextColor + '; text-align: center; padding: ' + this.m_SeparatorWidth + '; ' + this.m_Style + '">' + this.m_Separator + '</span>';
				ds[di++] = '&nbsp;';
			}
			else
			{
				ds[di++] = '&nbsp;';
			}
		}
	} 
	
	if (bDisplay)
	{
		ds[di++] = '</td></tr>';
		//ds[di++] = '</tr>';
		document.write(ds.join(''));
	}
	
	if (selectedNode != null)
		this.DisplaySubLevel (selectedNode);
}

// Gets the primary page title
function getMainPageTitle(node, isLink) {
	var navPath = g_navNode_Path;
	var siteId = navPath[0];
	if (navPath.length >= 2) {
		siteId = navPath[1];
	}
	
	// Find the site in the heirarchy
	var node = nodeSearchRecursive(node, siteId);
	if (node != null)
	{
		if (isLink != undefined && isLink == true)
		{
			var link = '<a href="' + node.m_href + '">' + node.m_label + '</a>';
			return link;
		}
		else
		{
			return node.m_label;
		}
	}
	return '';
}

// Gets the title of the current page, which may be a subpage or the main page.
function getCurrentPageTitle(node, isLink) {
	var navPath = g_navNode_Path;
	var siteId = navPath[navPath.length - 1];
	
	// Find the site in the heirarchy
	var node = nodeSearchRecursive(node, siteId);
	if (node != null)
	{
		if (isLink != undefined && isLink == true)
		{
			var link = '<a href="' + node.m_href + '">' + node.m_label + '</a>';
			return link;
		}
		else
		{
			return node.m_label;
		}
	}
	return '';
}

// Searches the site heirarchy to find the node that matches the target site id.
function nodeSearchRecursive(node, target) {
	if (node.m_id == target) {
		return node;
	}
	else {
		var i;
		for (i = 0; i < node.m_subNodes.length; ++i) {
			var result = nodeSearchRecursive(node.m_subNodes[i], target);
			if (result != null) {
				return result;
			}
		}
	}
	return null;
}