var pm = '';
var am = '';
var th = null;
var ts = null;
var newwindow = 'N';

function initMenus()
{
	if (document.getElementById)
	{
		buildMenus();
		if (document.addEventListener)
			document.addEventListener('mousemove', handleMenus, false);
		else
			document.onmousemove = handleMenus;
	}
}

function buildMenus()
{
	var aboutArray = [ 'tm_about', ['Who We Are', '/about/whoweare.asp'], ['Officers/Chairs', '/about/officerschairs.asp'], ['Administration', '/about/administration.asp'], ['Meetings', '/about/Meetings.asp'], ['By-Laws', '/about/bylaws.asp'], ['Consulting', '/about/consulting.asp'], ['Email Us', 'mailto:eventinfo@usatfadir.org']];
	var eventsArray = [ 'tm_events', ['Events/Races', 'http://www.usatf.org/calendars/searchResults.asp?associationNumber=3'], ['Add Event', 'https://www.usatf.org/calendars/mgmt/index.asp'], ['Adirondack Championships', 'https://www.usatf.org/calendars/searchResults.asp?series=USATF+Association+Championship&startDate=1%2F1%2F2011&endDate=12%2F31%2F2011&associationNumber=3'],  ['Event Coverage/Results', 'http://www.usatf.org/events/'], ['Visa Championship Series', 'http://www.usatf.org/events/2010/VisaChampionshipSeries/'], ['Team USA Events', 'http://www.usatf.org/calendars/teamUSA.aspx'], ['National Championships', 'http://www.usatf.org/calendars/national.aspx'], ['Search the Calendar', 'http://www.usatf.org/calendars/search/'], ['TV Schedule', 'http://www.usatf.org/calendars/TVSchedule.asp']];
	var resourcesArray = [ 'tm_resources', ['Officials', 'http://www.usatf.org/groups/Officials/'], ['Coaches', 'http://www.usatf.org/groups/Coaches/'], ['Youth Athletes', '/resources/Youth.asp'], ['Masters Athletes', '/resources/Masters.asp']];
	var membershipArray = [ 'tm_membership', ['Membership', '/Membership.asp'], ['Clubs', 'https://www.usatf.org/clubs/search/info.asp?associationNumber=3'], ['Sanction Information', 'http://www.usatf.org/events/sanctions/'], ['Sanction Forms', 'http://www.usatf.org/events/sanctions/application/download/info.asp?associationNumber=3'], ['Entry Forms', '/resources/entryforms.asp']];
	addMenu(aboutArray);
	newwindow = 'Y';
	addMenu(eventsArray);
	addMenu(membershipArray);
	newwindow = 'N';
	addMenu(resourcesArray);
}

function addMenu(myArray)
{
	var newA;
	var newDiv = document.createElement('div');
	newDiv.setAttribute('id', myArray[0]);
	for (var i = 1; i < myArray.length; i++)
	{	
		newA = document.createElement('a');
		newA.setAttribute('href', myArray[i][1]);
		newA.appendChild(document.createTextNode(myArray[i][0]));
		var temp = myArray[i][0];
		if (newwindow == 'Y') 
		{
			newA.setAttribute('target', myArray[i][2]);
		}
		newDiv.appendChild(newA);
		newDiv.appendChild(document.createElement('br'));
	}
	var n = document.getElementById(myArray[0] + '_a');
	n.parentNode.insertBefore(newDiv, n.nextSibling);
}



function showMenu(menuID)
{
	pm = menuID;
	clearTimeout(ts);
	if (am == '')
		ts = setTimeout('showMenu_doIt(\'' + menuID + '\')', 80);
	else
		showMenu_doIt(menuID);
}

function showMenu_doIt(menuID)
{
	if (document.getElementById)
	{
		if (am != '')
			hideMenu(am);
		am = menuID;
		pm = '';
		document.getElementById(am + '_a').style.color = '#CE3152';
		if (document.getElementById(menuID))
		{
			document.getElementById(menuID).style.display = 'block';
			posL = document.getElementById(menuID + '_a').offsetLeft;
			document.getElementById(menuID).style.left = posL + 'px';
			posT = document.getElementById(menuID + '_a').offsetTop + document.getElementById(menuID + '_a').offsetHeight;
			document.getElementById(menuID).style.top = posT + 'px';
		}
	}
}

function hideMenu(menuID)
{
	clearTimeout(th);
	th = null;
	if (document.getElementById(menuID))
		document.getElementById(menuID).style.display = 'none';
	document.getElementById(menuID + '_a').style.color = '#183C47';
	am = '';
}

function handleMenus(e)
{
	if (! document.addEventListener)
		handleMenu(event.srcElement);
	else if (e)
		handleMenu(e.target);
}

function handleMenu(myEvent)
{
	holdMenu = false;
	mv = myEvent;
	if (am != '')
	{
		while (mv)
		{
			if (mv.id == am || mv.id == am + '_a')
			{
				holdMenu = true;
				break;
			}
			mv = mv.parentNode;
		}
		if (! holdMenu)
		{
			if (th == null)
				th = setTimeout('hideMenu(\'' + am + '\')', 325);
		}
		else
		{
			clearTimeout(th);
			th = null;
		}
	}
	else if (pm != '')
	{
		while (mv)
		{
			if (mv.id == pm + '_a')
			{
				holdMenu = true;
				break;
			}
			mv = mv.parentNode;
		}
		if (! holdMenu)
		{
			clearTimeout(ts);
			ts = null;
		}
	}
}
