/*************  Javascript for the ATP menu system. **************

  Menu items and sub-menus are defined in the menuTree[] array below.

  The 1st level of the array contains the main menu objects.
      The 1st element is the "off" button image.
      The 2nd element is the "on" button image.
      The 3rd element is the URL to link to.
      The 4th element is the array of sub-menu objects.  If there 
          is no sub-menu, The element should be a null;

  The 4th element contains one array for each item in the submenu.
      The 1st element is the text which appears in the sub-menu
      The 2nd element is the URL to link to.

  This script (js/menu4.js) and the associated CSS file 
  (css/menu4.css) should be included in the HTML file.

  Finally, the following snippet of HTML should be placed in the HTML 
  file where the menu should appear:

---------------------------------------------------------------------------
	<script>displayMainMenu();</script>
---------------------------------------------------------------------------
***************************************************************************/

var menuTree = [
				new MenuItem('IDVAAC News', 'http://www.dvinstitute.org/assemblingthepieces/news.html', 'http://www.dvinstitute.org/assemblingthepieces/images/site_images/nav/idvaac_off.png', 'http://www.dvinstitute.org/assemblingthepieces/images/site_images/nav/idvaac_on.png',[
					]),
				new MenuItem('IDVAAC Review', 'http://www.dvinstitute.org/assemblingthepieces/idvaacreviews.html', 'http://www.dvinstitute.org/assemblingthepieces/images/site_images/nav/makingchange_off.png', 'http://www.dvinstitute.org/assemblingthepieces/images/site_images/nav/makingchange_on.png',[
					]),
				new MenuItem('News Briefs', 'http://www.dvinstitute.org/assemblingthepieces/newsbriefs.html', 'http://www.dvinstitute.org/assemblingthepieces/images/site_images/nav/News_off.png', 'http://www.dvinstitute.org/assemblingthepieces/images/site_images/nav/News_on.png',[
					]),
				new MenuItem('Whos Doing What', 'http://www.dvinstitute.org/assemblingthepieces/whosdoingwhat.html', 'http://www.dvinstitute.org/assemblingthepieces/images/site_images/nav/initiativenews_off.png', 'http://www.dvinstitute.org/assemblingthepieces/images/site_images/nav/initiativenews_on.png',[
					]),
				];


var lastSelMenuItem = -1;
var lastSubMenuItem = -1;

function MenuItem(text, link, img, over, subitems) {
	this.image = img;
	this.rollover = over;
	this.title = text;
	this.url = link;
	this.submenu = subitems;
	
	if(img)
		this.imageRep = new Image().src = img;
	if(over)
		this.rolloverRep = new Image().src = over;
}

function mainMenuSel(mIdx) {
	if(lastSelMenuItem > -1) {
		mid = 'mmImg' + lastSelMenuItem;
		document.getElementById(mid).src = menuTree[lastSelMenuItem].image;
		document.getElementById('submenu').innerHTML = '<li>&nbsp;</li>';
	}

	mid = 'mmImg' + mIdx;
	if(menuTree[mIdx].rollover)
		document.getElementById(mid).src = menuTree[mIdx].rollover;

	if(menuTree[mIdx].submenu != null)
		showSubMenu(mIdx);

	lastSelMenuItem = mIdx;
}


function clearMainMenuItem(mIdx) {
	//  The first line below leaves the selected menu item displayed when you move the cursor to the submenu.
	//  The second line clears the selected menu item when the cursor is moved to the submenu.
	//  Uncomment the behaviour you'd like and comment out the other!
	if(mIdx > -1 && menuTree[mIdx].submenu == null) {
	//if(mIdx > -1) {
		mid = 'mmImg' + mIdx;
		document.getElementById(mid).src = menuTree[mIdx].image;
		//lastSelMenuItem = -1;
	}
}



function subMenuSel(sIdx) {
/*
	if(lastSubMenuItem > -1) {      
		sid = 'smitem' + lastSubMenuItem;
		if(document.getElementById(sid)) document.getElementById(sid).style.color = 'black';
	}

	sid = 'smitem' + sIdx; document.getElementById(sid).style.color = 'red';
*/

	lastSubMenuItem = sIdx;
}


function showSubMenu(mIdx) {
	var oStr = '';
	var currentSubmenu = menuTree[mIdx].submenu;

	for(var i in currentSubmenu) {
		oStr += '<li id="subMenuItem' + i + '">';
		oStr += '<a id="smitem' + i + '" href="' + 
			currentSubmenu[i].url + '" onmouseover="subMenuSel(' + i + ');">' +
			currentSubmenu[i].title + '</a>\n';
		oStr += "</li>";
	}

	document.getElementById('submenu').innerHTML = oStr;
}


function displayMainMenu() {
	oText = buildMainMenu();
	document.write(oText);
}

function buildMainMenu() {
	mStr = '<div id="menu">\n';
	mStr += '<ul id="mainmenu">\n';
	
	for (var i in menuTree){
		mStr += '<li id="mtd' + i + '" class="mitem">' +
			'<a id="mmItem' + i + '" href="' + menuTree[i].url + '" onmouseover="mainMenuSel(' + i + ');" ' + 'onmouseout="clearMainMenuItem(' + i +  ');">' +
			'<img id="mmImg' + i +  '" src="' + menuTree[i].image + '" alt="' + menuTree[i].title + '" /></a></li>\n';
	}
	
	mStr += '</ul>\n';
	mStr += '<ul id="submenu">\n';
	mStr += '<li>&nbsp;</li>';
	mStr += '</ul>\n'
	mStr += '</div>\n';
	return mStr;
}

// hide iPhone URL bar
if (navigator.userAgent.indexOf('iPhone') != -1) {
	addEventListener("load", function() {
												setTimeout(hideURLbar, 0);
        						}, false);
}

function hideURLbar() {
	window.scrollTo(0, 1);
}