var ToolbarSupported = false;
var NSToolbarSupported = false;

if (navigator.userAgent.indexOf("MSIE")    != -1 && 
	navigator.userAgent.indexOf("Windows") != -1 && 
	navigator.appVersion.substring(0,1) > 3)
{
	ToolbarSupported = true;
}
else if (parseInt(navigator.appVersion) >= 4 )
{
	NSToolbarSupported = true;
}

if (NSToolbarSupported || ToolbarSupported)
{
	var Font;
	var aDefColor  = new Array(3);
	Font = "bold x-small Verdana";
	homeMenu = new Array();
	homeMenu = [
		",Home,Home,Main Page,/",
		"Home,Home - Main Page,/",
		"Home,Mission Statement,/aboutus.asp",
		"Home,News Releases,/press2006.asp",
		"Home,   2006,/press2006.asp",
		"Home,   2005,/press2005.asp",
		"Home,   2004,/press2004.asp",
		"Home",
		"Home,Reports,/reports.asp",
		"Home",
		"Home,Contact Us,/contact_us.asp"
		];
}
if (NSToolbarSupported)
{
	nsMenus = new Array();
	nsNames = new Array();
}
if (ToolbarSupported)
{
	var newLineChar = String.fromCharCode(10);
	var char34 = String.fromCharCode(34);
	var CurICPMenu = "";
	var HTMLStr;
	var x = 0;
	var y = 0;
	var x2 = 0;
	var y2 = 0;
	var MenuWidth;
	var ToolbarMenu;
	var ToolbarBGColor;
	var ToolbarLoaded = false;
	var aCurColor  = new Array(3);
	var MaxMenu = 30;
	var TotalMenu = 0;
	var arrMenuInfo = new Array(30);
        var bannerh = 126;
        var curveh = 20;
        var curvew = 18;

	// Output style sheet and toolbar ID
	document.write("<SPAN ID='StartMenu' STYLE='display:none;'></SPAN>");

	// Build toolbar template
	HTMLStr = 
		"<!--MENU_TITLES-->" +
		"<SCRIPT TYPE='text/javascript'>" + 
		"   var ToolbarMenu = StartMenu;" + 
		"</SCRIPT>";

}

function drawToolbar()
{
	document.write(HTMLStr);
	ToolbarLoaded = true;

	for (i = 0; i < TotalMenu; i++) 
	{
		thisMenu = document.all(arrMenuInfo[i].IDStr);
		if (thisMenu != null)
		{
			if (arrMenuInfo[i].type == "A")
				thisMenu.style.width = arrMenuInfo[i].unit;
			else 
				thisMenu.style.width = Math.round(arrMenuInfo[i].width * arrMenuInfo[i].unit) + 'em';
		}
	}
}

function setToolbarBGColor(color)
{	
}	

function setDefaultMenuColor(bgColor, fontColor, mouseoverColor)
{	
	if (bgColor   != "")	  aDefColor[0] = bgColor;
	if (fontColor != "")	  aDefColor[1] = fontColor;
	if (mouseoverColor != "") aDefColor[2] = mouseoverColor;
}

function setICPBanner(Gif,Url,AltStr)
{
}

function setBanner(Gif,Url,AltStr)
{
}

function setSubMenuWidth(MenuIDStr, WidthType, WidthUnit)
{
	var fFound = false;
	if (TotalMenu == MaxMenu)
	{
		alert("Unable to process menu. Maximum of " + MaxMenu + " reached.");
		return;
	}
	
	for (i = 0; i < TotalMenu; i++)
		if (arrMenuInfo[i].IDStr == MenuIDStr)
		{
			fFound = true;
			break;
		}

	if (!fFound)
	{
		arrMenuInfo[i] = new menuInfo(MenuIDStr);
		TotalMenu += 1;
	}

	if (!fFound && WidthType.toUpperCase().indexOf("DEFAULT") != -1)
	{
		arrMenuInfo[i].type = "A";
		arrMenuInfo[i].unit = 160;
	}
	else
	{
		arrMenuInfo[i].type = (WidthType.toUpperCase().indexOf("ABSOLUTE") != -1)? "A" : "R";
		arrMenuInfo[i].unit = WidthUnit;
	}
}

function menuInfo(MenuIDStr)
{
	this.IDStr = MenuIDStr;
	this.type  = "";
	this.unit  = 0;
	this.width = 0;
	this.count = 0;
}

function updateSubMenuWidth(MenuIDStr)
{
	for (i = 0; i < TotalMenu; i++)
		if (arrMenuInfo[i].IDStr == MenuIDStr)
		{
			if (arrMenuInfo[i].width < MenuIDStr.length) 
				arrMenuInfo[i].width = MenuIDStr.length;
			arrMenuInfo[i].count = arrMenuInfo[i].count + 1;
			break;
		}
}

function menuFactory(menus)
{
	menus = homeMenu.concat (menus);
	for (var i = 0; i < menus.length; i++)
	{
		args = menus[i].split(',');
		if (args.length == 5)
		{
			addMenu(args[1], args[2], args[3], args[4], '_top');
		}
		else if (args.length == 1)
		{
			addSubMenuLine(args[0]);
		}
		else
		{
			addSubMenu(args[0], args[1], args[2], '_top');
		}
	}
	if (NSToolbarSupported)
	{
		for (var i = 0; i < nsMenus.length; i++)
		{
			nsMenus[i] += '</TD></TR></TABLE></BODY>';
		}
	}
}

function addMenu(MenuIDStr, MenuDisplayStr, MenuHelpStr, MenuURLStr, TargetStr)
{
	if (ToolbarSupported) {
		tagStr  = "<!--MENU_TITLES-->";
		MenuStr = newLineChar;
		MenuStr += "<A TARGET='" + TargetStr + "' TITLE='" + MenuHelpStr + "'" +
				   "   ID='AM_" + MenuIDStr + "'" +
				   "   STYLE='text-decoration:none;cursor:hand;font:" + Font + ";background-color:" + aDefColor[0] + ";color:" + aDefColor[1] + ";'";
		if (MenuURLStr != "")
		{
				MenuStr += " HREF='" + MenuURLStr + "'";
		}
		else
			MenuStr += " HREF='' onclick='window.event.returnValue=false;'";
		MenuStr += 	" onmouseout="  + char34 + "mouseMenu('out' ,'" + MenuIDStr + "'); hideMenu();" + char34 + 
					" onmouseover=" + char34 + "mouseMenu('over','" + MenuIDStr + "'); doMenu('"+ MenuIDStr + "');" + char34 + ">" +
					"&nbsp;" + MenuDisplayStr + "&nbsp;</a>";
			MenuStr += "<SPAN STYLE='font:" + Font + ";color:" + aDefColor[1] + "'>&nbsp;|</SPAN>";
		MenuStr += tagStr;
		HTMLStr = HTMLStr.replace(tagStr, MenuStr);	
		setSubMenuWidth(MenuIDStr,"default",0);
	}
	else if (NSToolbarSupported)
	{
		text = '';
		nsNames[nsNames.length] = MenuIDStr;
		nsMenus[nsMenus.length] = '<BODY BGCOLOR=' + aDefColor[0] + '><TABLE BORDER=1><TR><TD><B>';
	}
	
}

function addSubMenu(MenuIDStr, SubMenuStr, SubMenuURLStr, TargetStr, bICPMenu)
{
	if (ToolbarSupported)
	{
		var MenuPos = MenuIDStr.toUpperCase().indexOf("MENU");
		if (MenuPos == -1) { MenuPos = MenuIDStr.length; }

		var LookUpTag  = "<!--" + MenuIDStr + "-->";
		var sPos = HTMLStr.indexOf(LookUpTag);
		if (sPos <= 0)
		{
			HTMLStr += newLineChar + newLineChar +
					"<SPAN ID='" + MenuIDStr + "'" +
					" STYLE='display:none;position:absolute;width:160;background-color:" + aDefColor[0] + ";padding-top:0;padding-left:0;padding-bottom:20;z-index:9;'" +
					" onmouseout='hideMenu();'>" +
					"<HR  STYLE='position:absolute;left:0;top:0;color:" + aDefColor[1] + "' SIZE=1>" +
					"<DIV STYLE='position:relative;left:0;top:8;'>";
		}

		TempStr = newLineChar +
					"<A ID='AS_" + MenuIDStr + "'" +
					"   STYLE='text-decoration:none;cursor:hand;font:" + Font + ";color:" + aDefColor[1] + "'" +
					"   HREF='" + SubMenuURLStr + "' TARGET='" + TargetStr + "'" +
					" onmouseout="  + char34 + "mouseMenu('out' ,'" + MenuIDStr + "');" + char34 + 
					" onmouseover=" + char34 + "mouseMenu('over','" + MenuIDStr + "');" + char34 + ">" +
					"&nbsp;" + SubMenuStr + "</A><BR>" + LookUpTag;
		if (sPos <= 0)
			HTMLStr += TempStr + "</DIV></SPAN>";
		else
			HTMLStr = HTMLStr.replace(LookUpTag, TempStr);	

		updateSubMenuWidth(MenuIDStr);	
	}
	else if (NSToolbarSupported)
	{
		addNSText(MenuIDStr, '<A STYLE="color:' + aDefColor[1] + ';text-decoration:none;" HREF="' + SubMenuURLStr + '"><FONT COLOR="' + aDefColor[1] + '" FACE=' + Font + ' SIZE=2>' + SubMenuStr + '</FONT></A><BR>');
	}
}

function addSubMenuLine(MenuIDStr)
{
	if (ToolbarSupported)
	{
		var LookUpTag = "<!--" + MenuIDStr + "-->";
		var sPos = HTMLStr.indexOf(LookUpTag);
		if (sPos > 0)
		{
			TempStr = newLineChar + "<HR STYLE='color:" + aDefColor[1] + "' SIZE=1>" + LookUpTag;
			HTMLStr = HTMLStr.replace(LookUpTag, TempStr);
		}
	}
	else if (NSToolbarSupported)
	{
		addNSText(MenuIDStr, "<HR SIZE=1 WIDTH=50 ALIGN=LEFT>");
	}
}

function addNSText(MenuIDStr, text)
{
	for (var i = 0; i < nsMenus.length; i++)
	{
		if (nsNames[i] == MenuIDStr) {
			nsMenus[i] += text;
		}
	}
}

function mouseMenu(id, MenuIDStr) 
{
	IsMouseout = (id.toUpperCase().indexOf("OUT") != -1);

	if (IsMouseout)
	{
		color = aDefColor[1];
	}
	else
	{
		color = aDefColor[2];
	}
	window.event.srcElement.style.color = color;
}

function doMenu(MenuIDStr) 
{
	var thisMenu = document.all(MenuIDStr);
	if (ToolbarMenu == null || thisMenu == null || thisMenu == ToolbarMenu) 
	{
		window.event.cancelBubble = true;
		return false;
	}
	// Reset dropdown menu
	window.event.cancelBubble = true;
	ToolbarMenu = thisMenu;
	ToolbarMenu.style.display = "none";
	showElement("SELECT");
	showElement("OBJECT");

	// Set dropdown menu display position
	x  = window.event.srcElement.offsetLeft +
	 	 window.event.srcElement.offsetParent.offsetLeft;
	x2 = x + window.event.srcElement.offsetWidth;
	y  = bannerh+21;
	thisMenu.style.top  = y;
	thisMenu.style.left = x;
	thisMenu.style.clip = "rect(0 0 0 0)";
	thisMenu.style.display = "block";

	// delay 2 millsecond to allow the value of ToolbarMenu.offsetHeight be set
	window.setTimeout("showMenu()", 2);
	return true;
}

function showMenu() 
{
	if (ToolbarMenu != null) 
	{ 
		y2 = y + ToolbarMenu.offsetHeight;

		ToolbarMenu.style.clip = "rect(auto auto auto auto)";
		hideElement("SELECT");
		hideElement("OBJECT");
	}
}

function hideMenu()
{
	if (ToolbarMenu != null && ToolbarMenu != StartMenu) 
	{
		// Don't hide the menu if the mouse move between the menu and submenus
		cY = event.clientY + document.body.scrollTop;
		if ( (event.clientX >= (x+5) && event.clientX <= x2) &&
			 (( cY > (y-10) && cY <= y2)))
		{
			window.event.cancelBubble = true;
			return; 
		}

		ToolbarMenu.style.display = "none";
		ToolbarMenu = StartMenu;
		window.event.cancelBubble = true;

		showElement("SELECT");
		showElement("OBJECT");
	}
}

function hideElement(elmID)
{
	for (i = 0; i < document.all.tags(elmID).length; i++)
	{
		obj = document.all.tags(elmID)[i];
		if (! obj || ! obj.offsetParent)
			continue;

		// Find the element's offsetTop and offsetLeft relative to the BODY tag.
		objLeft   = obj.offsetLeft;
		objTop    = obj.offsetTop;
		objParent = obj.offsetParent;
		while (objParent.tagName.toUpperCase() != "BODY")
		{
			objLeft  += objParent.offsetLeft;
			objTop   += objParent.offsetTop;
			objParent = objParent.offsetParent;
		}
		// Adjust the element's offsetTop relative to the dropdown menu
		objTop = objTop - y;

		if (x > (objLeft + obj.offsetWidth) || objLeft > (x + ToolbarMenu.offsetWidth))
			;
		else if (objTop > ToolbarMenu.offsetHeight)
			;
		else if ((y + ToolbarMenu.offsetHeight) <= 80)
			;
		else
			obj.style.visibility = "hidden";
	}
}

function showElement(elmID)
{
	for (i = 0; i < document.all.tags(elmID).length; i++)
	{
		obj = document.all.tags(elmID)[i];
		if (! obj || ! obj.offsetParent)
			continue;
		obj.style.visibility = "";
	}
}

function menu(MenuIDStr,d)
{
	if (!NSToolbarSupported)
	{
		return true;
	}
	l = document.layers['popup'];
	for (var i = 0; i < nsMenus.length; i++)
	{
		if (nsNames[i] == MenuIDStr)
		{
			l.document.open();
			l.document.write(nsMenus[i]);
			l.document.close();
		}
	}	
	if(d.target.y > window.innerHeight + window.pageYOffset - l.clip.height)
	{ 
		l.top = (window.innerHeight + window.pageYOffset - l.clip.height);
	} else
	{
		l.top = d.target.y+18;
	}
	l.left = d.target.x;
	if( l.left + l.clipWidth > window.width )
	{
		l.left = window.width - l.clipWidth;
	}
	l.visibility="show";
	return false;
}