IE = (navigator.appName=="Microsoft Internet Explorer");

// Menu Parameters
bgCol="#182a3d";
bgOverColor="#163352";
borCol=bgCol; 
fontCol="#ffffff";
fontOverCol="#ffffff";
fontFam="trebuchet MS";
fontSiz="8pt";
menuWidth=140;
if (!window.yCoord) { yCoord = 183; }
windowWidth = IE ? document.body.clientWidth : innerWidth;

function getX(obj) {
  var x=0;
  while (obj.offsetParent.tagName != "BODY") {
    x += obj.offsetLeft;
    obj = obj.offsetParent;
  }
  return x + obj.offsetLeft;
}


function getY(obj) {
  var y=0;
  while (obj.offsetParent.tagName != "BODY") {
    y += obj.offsetTop;
    obj = obj.offsetParent;
  }
  return y + obj.offsetTop;
}


var currentMenu=-1;
var noMenu=true;
var currentLayer=null;
function setBack() {
  if (window.currentLayer) currentLayer.restoreBack();
  this.style.color=fontOverCol;
  this.style.backgroundColor=bgOverColor;
  this.style.cursor="pointer";
  currentLayer=this;
  if (timerID) {
    clearTimeout(timerID);
    timerID=null;
  }
}


function restoreBack() {
  this.style.color=fontCol;
  this.style.backgroundColor=bgCol;
  noMenu=true;
  if (!timerID) initHide();
}


function linkIt() {
    if (this.mLink) {
        document.cookie = "ma_section=" + this.parentNode.id.substr(5);
        document.cookie = "ma_option=" + this.option;
        noMenu=false;  
        if (this.mLink.substr(this.mLink.length-2, 2) == '|w') {
            var newlink = this.mLink.substr(0,this.mLink.length-2);
            var newwin = open(newlink, "newwin");
        }
        else {
            location.href=this.mLink;
        }
    }
}


function makeMenu(area, n) {
  var thisMenu = document.createElement("div");
  thisMenu.id = "Menu_" + n;
  with (thisMenu.style) {
    position = "absolute";
    visibility = "hidden";
    width = menuWidth + "px";
    top = "100px";
    left = "100px";
  }
  area.appendChild(thisMenu);
  thisMenu.makeOptions = makeOptions;
  return thisMenu;
}


function makeOptions(menuName) {
  var mArray=menuArray[menuName];
  var menuItems=mArray.length/2;
  for (var i=0; i<menuItems; ++i) {
    var mText = mArray[i*2];
    var mLink = mArray[i*2+1] ? (mArray[i*2+1].substr(0,4) == "http" ? mArray[i*2+1] : path + mArray[i*2+1]) : "";
    var suffix=menuName+"_"+i;
    var temp = document.createElement("div");
    this.appendChild(temp);
    var text = document.createTextNode(mText);
    temp.id = "menuOpt" + suffix;
    with (temp.style) {
      position = "absolute";
      backgroundColor = bgCol;
      borderStyle = "solid";
      borderColor = borCol;
      borderWidth = "1px";
      fontFamily = fontFam;
      fontSize = fontSiz;
      color = fontCol;
      width = (menuWidth - 8) + "px";
      padding = 3 + "px";
    }
    temp.appendChild(text);
    temp.mLink = mLink;
  }
  var h=0;
  for (var i=0; i<this.childNodes.length; ++i) {
    temp=this.childNodes[i];
    temp.option = i;
    temp.style.cursor="pointer";
    temp.style.top = h + "px";
    temp.setBack=setBack;
    temp.restoreBack=restoreBack;
    temp.onmouseover=setBack;
    temp.onmouseup=linkIt;
    h += (temp.offsetHeight - 1);
  }
  this.menuHeight = h + 1;
}


function IEhide() {
  menus[currentMenu].style.visibility="hidden";
}


INTERVAL = 25;
function slideDown(m) {
  var top = parseInt(menus[m].style.top);
  if (top < 0) {
    top = Math.ceil(top / 2);
    menus[m].style.top = top + "px";
    menus[m].timerID = setTimeout("slideDown('" + m + "')", INTERVAL);
  }
}


function initShow(m) {
  menus[m].style.visibility = "visible";
  menus[m].style.top = (menus[m].menuHeight * -1) + "px";
  menus[m].style.left = (menus[m].menuX) + "px";
  menus[m].timerID = setTimeout("slideDown('" + m + "')", INTERVAL);
}


function hierinit() {
  loaded=true;
  var menuarea = document.createElement("div");
  document.body.appendChild(menuarea);
  menuarea.style.position = "absolute";
  menuarea.style.clip = "rect(0px,2000px,200px,0px)";
//  menuarea.style.top = getY(document.getElementById("nav_"+navImgArray[0])) + yCoord;
  menuarea.style.top = yCoord + "px";
  menus=new Array();
  for (var i = 0; i < navImgArray.length; i++) {
    if (menuArray[navImgArray[i]]) {
      menus[navImgArray[i]] = makeMenu(menuarea, navImgArray[i]);
      menus[navImgArray[i]].makeOptions(navImgArray[i]);
//      menus[navImgArray[i]].menuY = getY(document.getElementById("nav_"+navImgArray[i])) + yCoord;
      menus[navImgArray[i]].menuY = yCoord;
      menus[navImgArray[i]].menuX = getX(document.getElementById("nav_"+navImgArray[i])) + navImgWidth[i] / 2 - menuWidth / 2 + 5;
      menus[navImgArray[i]].timerID = null;
    }
  }

  document.onmousemove=menuCheck;
  document.onclick=checkHide;
  origWidth = document.body.clientWidth;
  origHeight = document.body.clientHeight;
//  onresize=reDo;
}

function reDo() {
  if (document.body.clientWidth != origWidth || document.body.clientHeight != origHeight) {
    location.reload();
  }
}


function checkHide() {
  if (noMenu) {
    hide()
  }
}


timerID=null;
function initHide() {
  timerID=setTimeout("hide()", 500);
}


function hide(e) {
  if (currentMenu==-1) return;
  IEhide();
  currentMenu=-1;
}

function show(n,e) {
  if (timerID) clearTimeout(timerID);
  hide();
  var m = menuArray[n];
  if (m.length > 0) {
    currentMenu=n;
    initShow(n);
  }
}

function menuCheck(e) {
  var hit = false;
  var x = IE ? event.clientX : e.clientX;
  var y = IE ? event.clientY : e.clientY;
  var o = IE ? event.srcElement : e.target;
  while (o.id && o.nodeName != "BODY" && !hit) {
    if (o.id.substring(0,7) == "menuOpt") {
      o.setBack();
      hit = true;
    }
    o = o.parentNode;
  }
  if (!hit && currentLayer) {
    currentLayer.restoreBack();
  }
}

