// Functions which control the menu & image rollovers
function rollOver(element) { // Rollover for the menu items
  if (isNav4) {
    menuIsUnder = true;

  } else if (isDom || isIE) {
    menuIsUnder = true;
    element = getObject(element);
    element.style.background = "#eeeeee";
    element.style.color = "#ffffff";
  }
}

function rollOff(element) { // Roll off for the menu items
  if (isNav4) {
    menuIsUnder = false;

  } else if (isDom || isIE) {
    menuIsUnder = false;
    element = getObject(element);
    element.style.background = "#ffffff";
    element.style.color = "#666666";
  }
  setMenuTimer();
}


//Retrieves the menu object
function getObject(menuName) {
  if (document.getElementById) {
    return document.getElementById(menuName);
  } else if (isIE) {
    return eval("document.all." + menuName);
  } else if (isNav4) {
    return eval("document.layers." + menuName);
  }
}


//Main function: displays the menus, controls rollover of images, and menus; Moves the menus when appropriate
function showMenu(menuName, isTop, aLink, parentObj) {
  if (!isLoaded) return;

  //Changes the rollover of the parent element
  if (!isTop) rollOver(parentObj)

  if (!isMenus) return; // Prevents non-menu browsers from executing unnecessary code

  // Checks for the existance of any menus and sends them to the hide menu function
  if (menuArray.length > 0) hideMenu(menuName, isTop);

  if (menuName) { // Checks to see if display of a menu is necessary

    menuName = getObject(menuName); // Calls the function to create the menu object
    menuName.fullHeight = (isNav4) ? menuName.clip.height : menuName.offsetHeight // Gets the height of the menu object

    var posArray = eval("ar_" + menuName.id); // Gets the position array for the menu object
    x = posArray[0]; // Retrieves the x-coordinate
    y = (isNav4) ? posArray[1][0] : (isMac && isIE) ? posArray[1][2] : posArray[1][1]; // Retrieves the y-coordinate

    // This block of code positions and displays the menu
    if (isNav4) {
      menuName.moveTo(x, y); // Moves the menu into place

      menuName.origTop = menuName.top // Saves the original menu position

      pageBottom = (window.pageYOffset + window.innerHeight) - 20; // Gets the height of the browser window
      if (menuName.fullHeight > pageBottom) {
        return; // If the menu is too big to fit in the browser window, the menu is not displayed
      } else { // Otherwise . . .
        menuBottom = menuName.top + menuName.fullHeight; // Finds the position of the bottom of the menu . . .
        if (menuBottom > pageBottom) { // Compares it to the height of the browser window
          menuDelta = menuBottom - pageBottom; // The amount by which the menu exceeds the browser window 
          menuName.top -= menuDelta; // Moves the menu up accordingly
        } else if (menuDelta) {
          menuName.top -= menuDelta; // If menuDelta already exists, then don't recalculate it, just move the menu accordingly
        }
        menuName.visibility = "show"; // Sets the menu's visibility property
      }
    } else if (isIE || isDom) {
      menuName.style.left = x; // Moves the menu along the x-axis
      menuName.style.top = y; // Moves the menu along the y-axis

      menuName.origTop = menuName.style.top // Saves the original menu position

      // Gets the height of the browser window 
      pageBottom = (isIE) ? (document.body.scrollTop + document.body.clientHeight) - 20 : (window.pageYOffset + window.innerHeight) - 20;
      if (menuName.fullHeight > pageBottom) {
        return; // If the menu is too big to fit in the browser window, the menu is not displayed
      } else {
        menuBottom = (isIE) ? menuName.style.pixelTop + menuName.fullHeight : parseInt(menuName.style.top) + menuName.fullHeight; // Finds the position of the bottom of the menu . . .
        if (menuBottom > pageBottom) { // Compares it to the height of the browser window
          menuDelta = menuBottom - pageBottom; // The amount by which the menu exceeds the browser window
          // Moves the menu up accordingly
          if (isIE) menuName.style.pixelTop -= menuDelta;
          else menuName.style.top = parseInt(menuName.style.top) - menuDelta
        } else if (menuDelta) { // If menuDelta already exists, then don't recalculate it, just move the menu accordingly
          if (isIE) menuName.style.pixelTop -= menuDelta;
          else menuName.style.top = parseInt(menuName.style.top) - menuDelta
        }
        menuName.style.visibility = "visible"; // Sets the menu's visibility property
      }
    }
    menuArray[menuArray.length] = menuName; // Appends the menu object to menuArray 
    menuIsShown = true; // Indicates that a menu is currently visible
  }
}

//Hides the currently displayed menu
function hideMenu(menuName, isTop) {
  if (isTop) {
    for (i = 0; i < menuArray.length; i++) {
      menuVis = (isNav4) ? menuArray[i] : menuArray[i].style;
      menuVis.visibility = "hidden";
      menuVis.top = menuArray[i].origTop;
      menuArray[i].isDelta = null;
    }
    menuArray.length = 0;
    menuIsShown = null;
    menuDelta = null;
  } else {
    if (menuName && menuArray.length > 1) {
      if (menuArray[menuArray.length-1].id.length > 7) menuStr = menuArray[menuArray.length-1].id.substr(0, menuArray[menuArray.length-1].id.length-1)
      if (menuName.substr(0, menuName.length-1) == menuStr) {
        menuVis = (isNav4) ? menuArray[menuArray.length-1] : menuArray[menuArray.length-1].style
        menuVis.visibility = "hidden"
        menuVis.top = menuArray[menuArray.length-1].origTop
        menuArray.length = menuArray.length-1
      }
    }
  }
}

// Hides all menus; called when the user clicks on the document or when the timer expires
function hideAll() {
  if (!menuIsShown || menuIsUnder) return;
  for (i = 0; i < menuArray.length; i++) {
    menuVis = (isNav4) ? menuArray[i] : menuArray[i].style
    menuVis.visibility = "hidden"
    if (menuArray[i].origTop) menuVis.top = menuArray[i].origTop
    menuArray[i].isDelta = null
  }
  menuArray.length = 0;
  menuIsShown = null;
  menuDelta = null;
}

// Sets a timer for clearing the menus
function setMenuTimer() {
  menuTimer = setTimeout("hideAll()",3500);
}

// Sets a timer for clearing the menus
function clearMenuTimer() {
  clearTimeout(menuTimer);
}

// Handles browser resizing problem for navigator 4
if(!window.orig_width) {
  window.onresize = reset_layers;
  window.orig_width = window.innerWidth;
  window.orig_height = window.innerHeight;
}

function reset_layers() {
    if (window.innerWidth != orig_width || window.innerHeight != orig_height) {
      location.reload();
    }
}
