//Menus - by Jotham Ritorze
var menuActive = true;
var seperator = '_';  		//used as a seperator in the menu path (ensure that its unique)
var onMenu=true;			//flag used to ignore body onClick when on menu (toggled on by menuOn(); and toggled back off by <body onClick="clearMenu()">)
var dropdownmenuActive =true;		//dropdown Menu active flag
var currentMenu ='';  		//currently active Menu

//*****************************
//  Helper Functions
//*****************************
function positionToMarker(target, marker, offsetLeft, offsetTop) { //positions layer (div) in relation to a marker element plus offsets
  marker=document.getElementById(marker);
  target=document.getElementById(target);
  while (marker.offsetParent) {  //add all parent offsets together to give total offset
    offsetLeft+=marker.offsetLeft;
    offsetTop+=marker.offsetTop;
    marker=marker.offsetParent;
  }
  if (offsetLeft+target.offsetWidth > document.body.offsetWidth) offsetLeft= document.body.offsetWidth - target.offsetWidth;
  target.style.top=offsetTop+'px';
  target.style.left=offsetLeft+'px';
  target.style.visibility='visible';
  target.style.zIndex=20;
}

//********************
//  Creation Code 
//********************
function generateMenus() { for (var i=0; i<menus.length; i++) { generateMenu(eval('menu'+menus[i]),menus[i]); } }

function generateMenu(menuItem, path) {
  var menuCode = '<div id="menu' + path + '" class="submenu"><table border="0" cellpadding="0" cellspacing="0" width="160">\n';
  if (path!='') path+=seperator;
  for (var i=0; i<menuItem.length; i++) {
    var itemName=menuItem[i][0];
	var itemAction=menuItem[i][1];
    var itemTarget=itemName;
    if (itemAction=='') {
	  itemAction="selectMenu('"+path+i+"')";
    } else { //if (substr($itemAction,0,4)=='http') {
//	  itemTarget='<a href="'+itemAction+'">'+itemName+'</a>';	   
	  itemAction="document.location='"+itemAction+"';";
    }

    if (typeof(menuItem[i][2])=='object') {
      menuCode += '  <tr id="'+path+i+'" class="menuInactive" onmouseover="menuOn(\''+path+i+'\',this.offsetWidth,1);" onmouseout="menuOff();" onclick="'+itemAction+'"><td style="padding-left:10px">'+itemTarget+'</td><td align="right" class="menuArrow"><a href="#menu'+path+i+'"><img src="http://transactions.brisbane.qld.gov.au/global/ob/images/menu/arrow.gif" height=17 width=15 alt="click to expand"></a></td></tr>\n';
     generateMenu(menuItem[i][2],path+i,itemName);
    }
    else if (itemName=='<<seperator>>') menuCode += '<tr><td colspan="2" onmouseover="menuOn(\''+path+i+'\');" onmouseout="menuOff();" onclick="'+itemAction+'"><img src="http://transactions.brisbane.qld.gov.au/global/images/spacer.gif" width="100%" height="2"></td></tr>\n';
    else menuCode += '  <tr height="17" id="'+path+i+'" class="menuInactive" onmouseover="menuOn(\''+path+i+'\');" onmouseout="menuOff();" onclick="'+itemAction+'"><td colspan="2" style="padding-left:10px">'+itemTarget+'</td></tr>\n';

}
  document.write(menuCode + '</table></div>\n\n');
}
//********************
//  DropDown Menu
//********************
function changeMenu(menu, visibility) {
  var nav = ["whatson","activeandhealthy","living","gettingaround","visitors","inbrief","paybills","news","business","community"];
  clearTimeout(timerId);
  (visibility == 'visible') ? className = 'menuActive' : className = 'menuInactive';
//split id into path and cycle through
  path = menu.split(seperator);
  var menu='';
  for (i=0; i<path.length; i++) {
    menu += path[i];
    if (document.getElementById(menu)) document.getElementById(menu).className = className;
	if (menu!='' && document.getElementById('menu'+menu)) document.getElementById('menu'+menu).style.visibility = visibility;
    menu += seperator;
  }
  if (document.all) { // bug fix for IE drop-down menu rendering -- hide SELECT elements while menu is visible 
    var formvisibility = (visibility == 'visible') ? 'hidden':'inherit'; 
    for (var j=0; j<document.forms.length; j++) {
      for (var k=0; k<document.forms[j].elements.length; k++){ 
	    if(document.forms[j].elements[k].options) { 
		  document.forms[j].elements[k].style.visibility = formvisibility;
		  if(document.forms[j].elements[k].parentNode){ // if possible shaddow the SELECT elements with grey background
			if(document.forms[j].elements[k].parentNode.tagName!='SPAN' && document.forms[j].elements[k].parentNode.id!='ob_selShaddow_'+j+'_'+k) document.forms[j].elements[k].outerHTML='<span id="ob_selShaddow_'+j+'_'+k+'">'+document.forms[j].elements[k].outerHTML+'</span>';
			document.forms[j].elements[k].parentNode.style.cssText=(formvisibility=='hidden')? 'background-color:#eee;' : '';
		  }
		}
	  }
    }
  }
}

var timerId;
function menuOff() { timerId = setTimeout("changeMenu('"+currentMenu+"','hidden')",200); } //turn off current menu path

function menuOn(menu, x, y) {
  if (menuActive==false) return;
  if (document.getElementById('menu'+menu)) positionToMarker('menu'+menu, menu, x, y);
  if (currentMenu!='') changeMenu(currentMenu, 'hidden');	//turn off current menu path
  changeMenu(menu, 'visible');			//turn on new menu path
  currentMenu = menu;					//set new menu as current
}

function selectMenu(menu) {
  onMenu=true;  //check by clearMenu()
  if (menu) {
    document.getElementById('menu'+menu).style.backgroundColor='#cccccc';
    setTimeout("document.getElementById('menu"+menu+"').style.backgroundColor='#eeeeee'",100);
    setTimeout("document.getElementById('menu"+menu+"').style.backgroundColor='#cccccc'",200);
    setTimeout("document.getElementById('menu"+menu+"').style.backgroundColor='#eeeeee'",300);
  }
}
