// valzmenu javaScript pull-down menus
// (c) 2004, Tom Auger | d3
// www.tomauger.com

// ============ PARAMETERS ================

var browserAdjust = document.all ? 0 : 1;

var baseURL = "http://www.b9productions.com.vs7.korax.net"; // this is prefixed to all links

var menuHeight = 19;
var menuWidth = 100;
var cellWidth = menuWidth + browserAdjust;
var subMenuWidth = cellWidth + 1;
var menuColour = "#ffffff";
var submenuColour = "eeeeee";
var hilightColour = "cccccc";
var resetSensitivity = 400; // this is the delay that the computer waits after mouseOut before resetting the menu. Should be > 50




// ============ MENU STRUCTURE =============

var menuAry = 	[	["EVENTS", [		["Calendar", "linkTo('http://www.tomauger.com/cgi-bin/valzCal.pl?o=B9PROD')" ]	] ],
					["ABOUT US", [		["Who We Are", "linkTo('who.html')"], ["What We Do", "linkTo('what.html')"], ["Contact", "linkTo('contact.html')"] ] ],
					["MAILING LIST", [	["Subscribe", "linkTo('mailing.html', 1)"], ["Unsubscribe", "linkTo('unsubscribe.html', 1)"] ] ],
					["LINKS", [			["Friends & Supporters", "linkTo('friends.html')"] ] ]
				];










// ============= PULL-DOWN MENU FUNCTIONS ===================

var menuSelectObj = new Object(); // contains all the menu items that can be selected or "checked off" (eg: preferences menu items)



function initializePulldowns()
{
	var menuDiv = document.getElementById("menuDiv");
	var menuHTML = '<div ID="menu_0"><table id="t_menu_header" class="valzmenu_table"><tr>';
	
	for (i in menuAry)
	{
		var menuTitle = menuAry[i][0];
		menuHTML += '<td ID="c_menu_0_' + i + '" class="valzmenu_cell" width="' + menuWidth + '" bgcolor=#' + menuColour + ' onMouseOver="javascript:showMenu(\'menu_0_' + i + '\');" onMouseOut="javascript:hideMenu(\'menu_0_' + i + '\');"><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td class="valzmenu_text">' + menuTitle + '</td></tr></table></td>';
	}

	menuHTML += '</tr></table></div>';
	menuDiv.innerHTML = menuHTML;
	
	for (i in menuAry)
	{
		var thisTable = document.getElementById("t_menu_header");

		createMenu(menuAry[i], "menu_0", i, i * subMenuWidth, menuHeight);
	}
}

function createMenu(whichMenu, menuParent, menuIndex, offsetX, offsetY)
{
	var menuDiv = document.getElementById("menuDiv");
	var menuName = menuParent + "_" + menuIndex;
	
	var menuHTML = '<div ID="' + menuName + '" style="position: absolute; visibility: hidden; display: none; top: ' + offsetY + 'px; left: ' + offsetX + 'px;"><table ID="t_' + menuName + '" class="valzmenu_submenu_table" width="' + subMenuWidth + '">';
	
	var subMenu = whichMenu[1];
	
	for (i in subMenu)
	{
		var menuItem = subMenu[i][0];
		var itemName = menuName + "_" + i;
		menuHTML += '<tr><td id="c_' + itemName + '" bgcolor=#' + submenuColour + ' height="' + menuHeight + 'px" class="valzmenu_submenu_cell" ';
		if (typeof subMenu[i][1] == "object")
		{
			menuHTML += ' onMouseOver="showMenu(\'' + itemName + '\');" onMouseOut="hideMenu(\'' + itemName + '\');"';
		}
		else
		{
			menuHTML += ' onMouseOver="showMenu(\'' + itemName + '\');" onMouseOut="hideMenu(\'' + itemName + '\');" onMouseUp="clearMenu(\'' + itemName + '\'); ' + subMenu[i][1] + ';"';
		}
		
		var cellHeight = menuHeight;
		menuHTML += '><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td><p class="valzmenu_submenu_text">';
		
		if (subMenu[i][2]) // this item is selectable / activatable
		{
			if (menuSelectObj[subMenu[i][2]]) // the item is selected / activated
			{
				menuHTML += '<img src="images/menu_item_active.gif">';
			}
			else
			{
				menuHTML += '<p></p>';
			}
		}
		
		menuHTML += menuItem + '</p></td><td style="text-align: right">';
		
		if (typeof subMenu[i][1] == "object") // it has a submenu
		{
			menuHTML += '<img src="images/valzmenu/submenu_arrow.gif" border="0">'
			createMenu(subMenu[i], menuName, i, offsetX + cellWidth, offsetY + i * (menuHeight + 3));
		}
		else // it's an actionable menu item
		{
			menuHTML += '<p></p>';
		}
		menuHTML += '</td></tr></table></td></tr>';
	}
	menuHTML += '</table></div>';
	menuDiv.innerHTML += menuHTML;
}

var prevMenu;
var prevCell;
var menuTimer;

function menuOn(menuName)
{
	if (prevMenu && prevMenu != null)
	{
		clearTimeout(menuTimer);
	}
	
	thisCell = document.getElementById("c_" + menuName); 
	thisCell.style.backgroundColor = hilightColour;
	
	if (prevCell && prevCell != null && prevCell != thisCell)
	{
		resetCellHighlight(prevCell);
	}

	prevCell = thisCell;
}

function menuOff(menuName)
{
	//hideMenu(menuName);
}



function resetCellHighlight(prevCell)
{
	if (prevCell.id.split("_").length > 4)
	{
		prevCell.style.backgroundColor = submenuColour;
	}
	else
	{
		prevCell.style.backgroundColor = menuColour;
	}
	prevCell.getElementsByTagName("TABLE")[0].getElementsByTagName("TR")[0].getElementsByTagName("TD")[0].style.color = "#000000";
}

function showMenu(menuName)
{
	if (prevMenu && prevMenu != null)
	{
		var prevNameAry = prevMenu.split("_");
		var nameAry = menuName.split("_");
		
		for (i = nameAry.length; i <= prevNameAry.length; i++)
		{
			var prevName = prevNameAry.slice(0,i).join("_");
			document.getElementById(prevName).style.visibility = "hidden";
		}
	}
	var subMenuDiv = document.getElementById(menuName);
	if (subMenuDiv && subMenuDiv != null)
	{
		subMenuDiv.style.visibility = "visible";
		subMenuDiv.style.display = "block"; // corrects an IE bug that displays the table borders even if the container DIV is hidden
		prevMenu = menuName;
	}
	//alert (menuName);
	menuOn(menuName);
}

function hideMenu(menuName)
{
	menuTimer = setTimeout('clearMenu("' + menuName + '")', resetSensitivity);
}



function clearMenu(menuName)
{
	if (prevCell.id == "c_" + menuName)
	{
		var prevNameAry = prevMenu.split("_");
		for (i = 3; i <= prevNameAry.length; i++)
		{
			var prevName = prevNameAry.slice(0,i).join("_");
			document.getElementById(prevName).style.visibility = "hidden";
		}
	}
	resetCellHighlight(prevCell);
}


// MENU ACTION FUNCTIONS ===========================================================================

function linkTo(whichURL, popupFlag) {
	if (whichURL && whichURL != '') {
		if (! /^https?:\/\//.test(whichURL)) whichURL = baseURL + "/" + whichURL;
		
		if (popupFlag){
			window.open(whichURL, "valzPopupWin");
		} else {
		document.location = whichURL;
		}
	}
}