// Function to parse the menu definition variable and 
// pre-select an item by setting the ON image to the OVER image.
function preSelectMenuItem( strCurrentPage, objMenuDef, strSiteMap )
{
	var ii; 
	var strUrl;
	var blnFound;
	var jj = 0;

	// First parse the site map string
	var arySiteMap = strSiteMap.split(";");

	blnFound = false;
	for( ii = 0; ii < objMenuDef.length; ii++ )
	{
		if( objMenuDef[ii] )
		{
			strUrl = objMenuDef[ii].url;
			if( strUrl != undefined )
			{
				if( strCurrentPage.toLowerCase() == strUrl.toLowerCase() )
				{
					blnFound = true;
				}
				else
				{
					objSub = objMenuDef[ii].sub;
					if( objSub != undefined )
					{
						if( isPageInSubNav( strCurrentPage, objSub ) )
						{
							blnFound = true;
						}
					}
				}
				
				// Try the sitemap (for mixed cool js menus with hyperlinks)
				if( !blnFound )
				{
					for( jj = 0; jj < arySiteMap.length; jj++ )
					{
						if( arySiteMap[jj].indexOf( strUrl.toLowerCase() ) == 0 )
						{
							if( arySiteMap[jj].indexOf( strCurrentPage.toLowerCase() ) >= 0 )
							{
								blnFound = true;
							}
						}
					}
				}
				
				if( blnFound )
				{
					// See if there is a style to pre-select (non-image top menu)
					var objSelectedStyle;
					if( objMenuDef[0].style )
					{
						if( objMenuDef[0].style[0] )
						{
							if( objMenuDef[0].style[0].CSS_SELECTED )
							{
								objSelectedStyle = objMenuDef[0].style[0].CSS_SELECTED;
							}
						}
					}

					// If there is a format item...
					if( objMenuDef[ii].format )
					{
						// The top level menu is image based
						if( objMenuDef[ii].format.image )
						{
							objMenuDef[ii].format.image[0] = objMenuDef[ii].format.image[1];
						}
						// The top level menu is text/css based
						// Format item exists, just overwrite the css item
						else
						{
							if( objSelectedStyle )
							{
								objMenuDef[ii].format.css = objSelectedStyle;
							}
						}
					}
					else
					{
						// The top level menu is text/css based, and no format tag exists in the item
						if( objSelectedStyle )
						{
							objMenuDef[ii].format = { };
							objMenuDef[ii].format.css = objSelectedStyle;
						}
					}
					break;
				}
			}
		}
	}
}

function isPageInSubNav( strCurrentPage, objSub )
{
	var jj;
	var strUrl;
	var blnFound = false;
	var objSubSub;

	if( objSub != undefined )
	{
		for( jj = 0; jj < objSub.length; jj++ )
		{
			try
			{
				strUrl = objSub[jj].url;
				if( strUrl != undefined )
				{
					if( strCurrentPage.toLowerCase() == strUrl.toLowerCase() )
					{
						blnFound = true;
					}
					else
					{
						objSubSub = objSub[jj].sub;
						if( objSubSub != undefined )
						{
							if( isPageInSubNav( strCurrentPage, objSubSub ) )
							{
								blnFound = true;
							}
						}
					}
				}
			}
			catch( ex )
			{
				// Weird error that doesn't really break the page, but happens in MSIE 6.0
				//alert( 'failed at jj = ' + jj + ', ex:' + ex );
			}
		}
	}
	return blnFound;
}
