var timerRunning = false;
var clearMenu;
var dropMenus = new Array();

function goToChildLink(referringObject) {
  if (referringObject.childNodes[1]) { document.location = referringObject.childNodes[1].href; }
  else if(referringObject.childNodes[0]) { document.location = referringObject.childNodes[0].attributes.href.value; }
}

function showMenu(id,obj,tier) {
  obj.style.backgroundColor = '#8F9BBF';
  if (obj.className == 'menu-submenu') obj.style.backgroundImage = 'url(http://www.cs.virginia.edu/~csadmin/images/arrow-light.gif)';
  if (id == 'none') {
    hideDivClass(tier,id);
    if(timerRunning == true) clearTimeout(clearMenu);
  } else if (id != '') {
    // Brute force coding...woohoo!
    hideDivClass(tier,id);
    if (tier == 'menu1') {
      hideDivClass('menu2',id);
      hideDivClass('menu3',id);
    } else if (tier == 'menu2') {
      hideDivClass('menu3',id);
    }
    if(timerRunning == true) clearTimeout(clearMenu);
    var newY = findPosY(obj);
    document.getElementById(id).style.top = newY +'px';
    if (type=="IE") { var divDisplay = "document.all." + id + ".style.display"; }
    if (type=="NN") { var divDisplay = "document." + id + ".display"; }
    if (type=="MO" || type=="OP") { var divDisplay = "document.getElementById('" + id + "').style.display"; }
    eval(divDisplay + "='block'");
  }
}

function hideMenu(obj) {
  obj.style.backgroundColor = '';
  if (obj.className == 'menu-submenu') obj.style.backgroundImage = 'url(http://www.cs.virginia.edu/~csadmin/images/arrow-dark.gif)';
  clearMenu = setTimeout("hideAll()",500);
  timerRunning = true;
}

function hideAll() {
  var tmp = document.getElementsByTagName('div');
  var body = document.getElementsByTagName("body").item(0);
  for (var i = 0; i < tmp.length; i++) {
    if ((tmp[i].className == 'menu1') || (tmp[i].className == 'menu2') || (tmp[i].className == 'menu3')) tmp[i].style.display = 'none';
    if (tmp[i].className == 'menu-datechooser') tmp[i].parentNode.removeChild(tmp[i]);
  }
  for (d in dropMenus) {
    try {
      var throwaway = body.removeChild(dropMenus[d]);
    } catch (e) { } // Node is already gone
    dropMenus[d] = null;
  }
  dropMenus = new Array();
}

function checkMenu(e) {
  if (!e) e = window.event;
  if (!e.target) e.target = e.srcElement;

  // Find the containing div, if it isn't part of a menu, clear all menus
  var element = e.target;
  while ((element.nodeName != 'DIV') && (element.parentNode)) {
    var element = element.parentNode;
  }
  if ((element.id) && (element.id.indexOf('datechooser') != -1)) return;
  if ((element.className) && (element.className.indexOf('menu') != -1)) return;

  hideAll();
}

document.onmousedown = checkMenu;

// ----------------------------------------------------------------------
// Below is the code for the dynamic popup menu, above is the static menu
// ----------------------------------------------------------------------

function dropMenu(obj,mode) {
  if (mode == 'bottom') {
    var x = findPosX(obj);
    var y = findPosY(obj) + obj.offsetHeight;
  } else if (mode == 'right') {
    var x = findPosX(obj) + obj.offsetWidth;
    var y = findPosY(obj);
  } else {
    alert('The menu mode "' + mode + '" is not supported.');
  }
  this.x = x;
  this.y = y;
  this.items = new Array();

  this.cssClass = 'menu-container';
  this.cssClassItem = 'menu-item';
  this.cssClassComment = 'menu-comment';
  this.cssClassLink = 'menu-link';
  this.draw = drawMenu;
  this.add = addMenuItem;
  this.addComment = addComment;
}

function dropMenuFromCoords(x,y) {
  this.x = x;
  this.y = y;
  this.items = new Array();

  this.cssClass = 'menu-container';
  this.cssClassItem = 'menu-item';
  this.cssClassComment = 'menu-comment';
  this.cssClassLink = 'menu-link';
  this.draw = drawMenu;
  this.add = addMenuItem;
  this.addComment = addComment;
}

function menuItem(name, link) {
  this.type = 'item';
  this.name = name;
  this.link = link;
  this.html = null;
}

function menuComment(text) {
  this.type = 'comment';
  this.text = text;
  this.html = null;
}

function drawMenu() {
  var menuDiv = document.createElement('div');
  menuDiv.className = this.cssClass;
  menuDiv.style.left = this.x + 'px';
  menuDiv.style.top = this.y + 'px';

  for (var x = 0; x < this.items.length; x++) {
    var i = this.items[x];
    var l;

    i.html = document.createElement('div');
    i.html.menu = menuDiv;
    if (i.type == 'comment') {
      i.html.className = this.cssClassComment;
      l = document.createElement('span');
      l.innerHTML = i.text;
    } else if (i.type == 'item') {
      i.html.className = this.cssClassItem;
      i.html.onmouseover = function() { this.className += '-highlight'; }
      i.html.onmouseout = function() { this.className = this.className.replace('-highlight',''); }
      i.html.onclick = function() {
        goToChildLink(this);
        if (this.menu.parentNode) this.menu.parentNode.removeChild(this.menu);
      }
      i.html.style.cursor = 'pointer';
      l = document.createElement('a');
      l.menu = menuDiv;
      l.href = i.link;
      l.onclick = function() {
        hideAll();
        window.location = this.href;
      }
      l.className = this.cssClassLink;
      l.innerHTML = i.name;
    }

    i.html.appendChild(l);
    menuDiv.appendChild(i.html);
  }
  var body = document.getElementsByTagName("body").item(0);
  body.appendChild(menuDiv);
  dropMenus.push(menuDiv);
}

function addMenuItem(name,link) {
  this.items.push(new menuItem(name,link));
}
function addComment(text) {
  this.items.push(new menuComment(text));
}

