///////////////////////////////////////////////////////////////////////////////
// Data variables
///////////////////////////////////////////////////////////////////////////////

var menuDelay = 1000;
var timeoutIds = [];

///////////////////////////////////////////////////////////////////////////////
// Menu content and visibility controls
///////////////////////////////////////////////////////////////////////////////

function setMenuOn(el_id)
	{
  // isolate the number at the end of el_id:
	var pattern = /^\D+(\d+)$/;
	var result = el_id.match(pattern);
	if (result == null)
		{
		// couldn't match the item
		return;
		}
		
	var subArray = subMenus[result[1]];
	
	// only one dropdown
	dropper = document.getElementById("dropdown1");
	choice  = document.getElementById(el_id);
	dropper.style.visibility = "hidden";
	document.getElementById("dropdown2").style.visibility = "hidden";
	document.getElementById("dropdown3").style.visibility = "hidden";
	res = "";
	
	// see if there are any dropdown items
	if (subArray.length == 0)
		{
		return true;
		}
	
	// there is at least one pair in the subarray
	for ( var ii = 0; ii < subArray.length; ii++ )
		{
		var subItemArray = subArray[ii];
		var suffix = "";
		var subSubArray = subItemArray[2];
		if (subSubArray.length > 0)
			{
			suffix = "";
			}
		var lk = " href=\"#\"";
		if (subItemArray[1] != "-")
			{
			lk = " href=\"" + subItemArray[1] + "\"' ";
			}
		var item  = "main" + result[1] + "sub" + ii;
		res = res + 	"<p class='menuDropdown'><a class='subMenuItem' " +
						"onmouseover='setSubMenuOn(\"" + item + "\");' " +
						"onmouseout='setMenuOff(\"" + item + "\");'" + lk +
						" id='" + item + "'>" + subItemArray[0] + suffix + "</a></p>";
		}
	
	dropper.innerHTML = res;
	var offsetY = choice.offsetParent.offsetTop;
	dropper.style.top = (offsetY === 0 ? 0 : offsetY + 2) + choice.offsetTop + choice.offsetHeight;
	dropper.style.left = choice.offsetParent.offsetLeft + choice.offsetLeft - 4;
	dropper.style.visibility = "visible";
	flushTimeouts();
	}

function setMenuOff(el_id)
{
  window.status = "";
  setMenuTimeout();
}

function setSubMenuOn(el_id)
	{
	// reset the timeouts, hide any existing dropdown 2 & 3 menus while we work
	// on them
	ssDroper = document.getElementById("dropdown2");
	ssDroper.style.visibility = "hidden";
	document.getElementById("dropdown3").style.visibility = "hidden";
	flushTimeouts();
	
	// check if a sub-sub-menu exists
	var pattern = /^main(\d+)sub(\d+)$/;
	var indices = el_id.match(pattern);
	if (indices == null)
		{
		// couldn't match the item
		return;
		}

	var menuArray = subMenus[indices[1]];
	var subArray  = menuArray[indices[2]];
	var subSubArray = subArray[2];

	if (subSubArray.length == 0)
		{
		// no need to display a sub-sub-menu
		return;
		}

	choice = document.getElementById(el_id);
	dropper1 = document.getElementById("dropdown1");
	
	var res = "";
	// there is at least one pair in the subarray
	for ( var ii = 0; ii < subSubArray.length; ii++ )
		{
		var subItemArray = subSubArray[ii];
		var item  = "main" + indices[1] + "sub" + indices[2] + "sub" + ii;
		var lk = " href=\"#\"";
		if (subItemArray[1] != "-")
			{
			lk = " href=\"" + subItemArray[1] + "\"";
			}
		
		res = res + 	"<p class='menuDropdown'><a class='subMenuItem' " +
						"onmouseover='setSubSubMenuOn(\"" + item + "\");' " +
						"onmouseout='setMenuOff(\"" + item + "\");'" + lk +
						" id='" + item + "'>" + subItemArray[0] + "</a></p>";
		}
		
	ssDroper.innerHTML = res;
	ssDroper.style.top = dropper1.offsetTop + choice.offsetTop - 4;
	ssDroper.style.left = dropper1.offsetLeft + dropper1.offsetWidth - 3;
	ssDroper.style.visibility = "visible";
	}

function setSubSubMenuOn(el_id)
	{
  // hide the dropdown so we can work on it, flush any timeouts
	ssDroper = document.getElementById("dropdown3");
	ssDroper.style.visibility = "hidden";
	flushTimeouts();
  
	// check if a sub-sub-menu exists
	var pattern = /^main(\d+)sub(\d+)sub(\d+)$/;
	var indices = el_id.match(pattern);
	if (indices == null)
		{
		// couldn't match the item
		return;
		}

	var menuArray = subMenus[indices[1]];
	var subArray  = menuArray[indices[2]];
	var subSubArray = subArray[2][indices[3]];
	var subSubSubArray = subSubArray[2];

	if (subSubSubArray.length == 0)
		{
		// no need to display a sub-sub-sub-menu
		return;
		}

	choice = document.getElementById(el_id);
	dropper2 = document.getElementById("dropdown2");
	
	var res = "";
	// there is at least one pair in the subarray
	for ( var ii = 0; ii < subSubSubArray.length; ii++ )
		{
		var subSubItemArray = subSubSubArray[ii];
		var item  = "sss" + ii; // only needs to be unique in the scope of dropdown3
		var lk = " href=\"#\"";
		if (subSubItemArray[1] != "-")
			{
			lk = " href=\"" + subSubItemArray[1] + "\" ";
			}
		
		res = res + 	"<p class='menuDropdown'><a class='subMenuItem' " +
						"onmouseover='flushTimeouts();' " +
						"onmouseout='setMenuOff(\"" + item + "\");'" + lk +
						" id='" + item + "'>" + subSubItemArray[0] + "</a></p>";
		}
		
	
	ssDroper.innerHTML = res;
	ssDroper.style.top = dropper2.offsetTop + choice.offsetTop - 4;
	ssDroper.style.left = dropper2.offsetLeft + dropper2.offsetWidth - 3;
	ssDroper.style.visibility = "visible";
	}


///////////////////////////////////////////////////////////////////////////////
// Timeout and link controls
///////////////////////////////////////////////////////////////////////////////

function setMenuTimeout()
{
  // set up a timeout to kill the dropdown menu
  timeoutIds.push( setTimeout( "killDropdown()" , menuDelay ) );
}

function flushTimeouts()
{
	while ( timeoutIds.length > 0)
	{
		var lastId = timeoutIds.pop();
		clearTimeout( lastId );
	}
}

function killDropdown()
{
  document.getElementById("dropdown3").style.visibility = "hidden";
	document.getElementById("dropdown2").style.visibility = "hidden";
	document.getElementById("dropdown1").style.visibility = "hidden";
	flushTimeouts();
}

