document.menus = [];

function hideBullys() {
	if (!document.bullys)
		document.bullys = document.body.getElementsByTagName("select");
	
	for(var i = 0; i < document.bullys.length; i++)
		document.bullys[i].style.visibility = "hidden";
}

function restoreBullys() {
	if (document.bullys) {
		var allHidden = true;
		for(var i = 0; i < document.menus.length; i++) {
			if ( document.menus[i].m.style.visibility != "hidden")
			{
				allHidden = false;
				break;
			}
		}
		if (allHidden)
			for(var i = 0; i < document.bullys.length; i++)
				document.bullys[i].style.visibility = "";

	}
}

function Menu (root_id) {
  this.root = document.getElementById(root_id);
  this.setup(this.root); }

Menu.prototype.setup = function (r) { 
  var c = r.childNodes;
  for (var i = 0; i < c.length; i++) {
    if (c[i].tagName == "A" && c[i].parentNode.tagName == "LI") {
      c[i].onmouseover = mMouOvrItm;
      c[i].onmouseout = mMouOutItm;
      c[i].getParents = mGetParents;
      var pli = c[i].parentNode;
      for (var j = 0; j < pli.childNodes.length; j++) {
        if (pli.childNodes[j].tagName == "UL")
        {
          c[i].m = pli.childNodes[j];
          c[i].m.p = c[i];
          c[i].m.style.visibility = "hidden";
          c[i].m.onmouseover = mMouOvrMnu;
          c[i].m.onmouseout = mMouOutMnu;
          c[i].ptr = document.menus.length;

          c[i].hide = mHide;
          document.menus.push(c[i]);
          break;
        }
      }
    }
    this.setup( c[i] );
  }
}

function mMouOvrItm() {
  if (this.parentNode.className) {  
    this.parentNode.classNameOrig = this.parentNode.className;
    this.parentNode.className = this.parentNode.className + "-hover";
  }
  
    if (this.tOut) clearTimeout(this.tOut);

    var p = this.getParents();

    for (var i = 0; i < document.menus.length; i++) {
      var h = true;
      for (var j = 0; j < p.length; j++)
      	if (p[j] == document.menus[i].m) h = false;
      if (h) document.menus[i].hide();
    }

    if (this.m) {
		hideBullys();
		this.m.style.visibility = "visible";
		
	}
}

function mMouOutItm() {
  
  if (this.parentNode.classNameOrig) {
    this.parentNode.className = this.parentNode.classNameOrig;
    this.parentNode.classNameOrig = null;
  }
  if (this.m) {
    this.tOut = setTimeout("document.menus["+this.ptr+"].hide()",500);
  }
}

function mMouOvrMnu() {
  if (this.p.tOut) clearTimeout(this.p.tOut);
}

function mMouOutMnu() {
  this.p.onmouseout();
}

function mHide(p) {
  this.m.style.visibility = "hidden";
  restoreBullys();
}

function mGetParents() {
  var a = [];
  var p = this.parentNode;
  while (p) {
     if (p.tagName=='UL')
     	a.push(p);
     p = p.parentNode;
  }
  return a;
}

