var HEIGHT = 32;
var WIDTH = 146;
var LEFT = 16;

var oldId = -1;
var oldMenu = null;
var oldPopup = null;

/**
* hides all drop downs in the current page (IE-6 bug)
*/
function switchDropDownVisibility(isOn) {
  if (!document.all) return;
  var ua = navigator.userAgent;
  if (ua.indexOf("MSIE ")<0) return;
  if (ua.substr(ua.indexOf("MSIE ") + 5, 1) < 6) return;
  
  var elArray = document.getElementsByTagName("select");
  for (var i = 0; i<elArray.length; ++i) {
    elArray[i].style.visibility = isOn ? "visible" : "hidden";
  }
}


/**
* shows the given menu on screen and hides all other,
* possibly currently visible menus
*/
function showMenu(menuid, popupid, menu, num)	{
	if (!document.getElementById) return;

	var mid = document.getElementById(menuid);
  if (!mid) return;
  var pid = document.getElementById(popupid);
  if (!pid) return;
  
  if (oldMenu!=mid || pid.style.visibilty != "visible") {
  	hideMenu(num);  	
	  //id.style.lseft = (LEFT + (num-1) * WIDTH) + 'px';
	  pid.style.visibility = "visible";	  
	  
	  setActive(mid, num);
	  
	  oldId = num;
	  oldMenu = mid;
	  oldPopup = pid;
	  
	  switchDropDownVisibility(false);
  }
}

function countMenuItems(id) {
	var count = 0;
	for(var i=0; i<id.childNodes.length; ++i) {
		//alert(id.childNodes[i].tagName);
		if (id.childNodes[i].tagName) {
			if (id.childNodes[i].tagName.toLowerCase() == 'a') {
				count++;
			}
		}
	}
	return count;	
}

function getMenuHeight(id) {
	var h = 0;
	for(var i=0; i<id.childNodes.length; ++i) {
		if (id.childNodes[i].tagName) {
			if (id.childNodes[i].tagName.toLowerCase() == 'div') {
				//alert(id.childNodes[i].style.height);
				h += id.childNodes[i].style.height;
			}
		}
	}
	return h;	
}

function hideMenu(num)	{
	if (!document.getElementById) return;

	if (oldMenu) {
	  resetMenu(oldMenu, num);
	  oldMenu = null;
	}
	if (oldPopup) {
	  oldPopup.style.visibility = 'hidden';
	  oldPopup = null;
	}
	
  switchDropDownVisibility(true);
}

function setActive(el, num) {
  setCssClass(el, 'menulink_active border'+num);
}
function resetMenu(el, num) {
  var attr = "";
  for (var i = 0; i< el.attributes.length; ++i) {
    attr += el.attributes[i].nodeName + "\n";
  }
  //alert(attr);

  setCssClass(el, 'menulink border'+num);
}
function setCssClass(el, style) {
  el.setAttribute("class", style);
  el.setAttribute("className", style);
  //el.style.cssClass = style;
}