// Variables for browser detection
var nua = navigator.userAgent.toLowerCase(); // Returns the user agent string as lower case
var isWin = navigator.userAgent.indexOf("Win") != -1; // Detects windows platform
var isMac = navigator.userAgent.indexOf("Mac") != -1; // Detects macintosh platform
var isOther = (!isWin && !isMac); // Detects other platforms
var isNav4 = (document.layers) ? true : false; // Detects netscape navigator 4
var isOpera = (nua.indexOf("opera") != -1);
var isOperaMac = (isOpera && isMac);
var isIE = (document.all) ? true : false; // Detects ie 4 compatible browsers, all platforms
var isDom = (document.getElementById) ? true : false; // Detects w3c dom compatible browsers

// Detects Netscape ver 4.7 and below; Necessary to fix layout issues in Netscape below version 4.72
var isNavOld = false;
var isNN40 = false;
if (isNav4) {
  var nnVer = navigator.appVersion.substring(0,4);
  nnVer = nnVer.replace(/\s/, "");
  if (nnVer < 4.72 || isMac) isNavOld = true;
  var isNN40 = (nnVer < 4.1 && nnVer >= 4.0) ? true : false;
}
// Detects the presence of php files, due to layout problems in Netscape Navigator 4.0* browsers
var isPHP = (location.pathname.match(/.php/) && !location.pathname.match(/.php3/) && isNN40) ? true : false;

var isMenus = (((isIE && !isMac) || (isNav4) || isDom) && !isOperaMac && !isPHP); // Set for the browsers which will display the menus


// Initializes several variables
var menuArray = new Array(); // An array of visible menus
var menuIsShown = null; // Indicates when a mneu is visible
var menuIsUnder = false; // Indicates that the mouse is over a menu
var menuDelta;
var isLoaded = false;
var menuTimer = null; // Variable for the hide menu timer
var isIndex = false;


// Functions which control the menu & image rollovers
function rollOver(element, aLink) { // Rollover for the menu items
  if (isNav4) {
    menuIsUnder = true;
  } else if (isDom || isIE) {
    menuIsUnder = true;
    element = getObject(element);
    aLink = getObject(aLink);
    element.style.background = "#999999";
    element.style.color = "#ffffff";
    aLink.style.background = "#999999";
    aLink.style.color = "#ffffff";
  }
}

function rollOff(element, aLink) { // Roll off for the menu items
  if (isNav4) {
    menuIsUnder = false;
  } else if (isDom || isIE) {
    menuIsUnder = false;
    element = getObject(element);
    aLink = getObject(aLink);
    element.style.background = "#ffffff";
    element.style.color = "#666666";
    aLink.style.background = "#ffffff";
    aLink.style.color = "#666666";
  }
}


// Create the menus
function writeMenus(menuStr) {
  if (isMenus) document.write(menuStr);
}


// Hides Form elements
function hideForm(menuName) {
  if (isNav4) {
    seekTop = parseInt(menuName.top) + menuName.fullHeight;
    seekLeft = parseInt(menuName.left) + 110;

    if ((seekTop > 137) && (seekLeft > 603)) {
      document.layers.seek_layer.visibility = "hidden";
    }
  } else if (isIE || isDom) {
    seekTop = parseInt(menuName.style.top) + menuName.fullHeight;
    seekLeft = parseInt(menuName.style.left) + 92;

    if ((document.hsc_seek) && (seekTop > 100) && (seekLeft > 605)) {
      document.hsc_seek.style.visibility = "hidden";
    }
  }
}

function showForm() {
  if (isNav4) {
    document.layers.seek_layer.visibility = "visible";
  } else if (isIE || isDom) {
    if (document.hsc_seek) {
      document.hsc_seek.style.visibility = "visible";
    }
  }
}


//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, aLink)

  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

    if (parentObj) { // Retrieves the coordinates for the submenus
      obj_menu=parentObj.substring(0,parentObj.lastIndexOf("_"));
      obj_menu=getObject(obj_menu);
      if (isNav4) {
        obj_num=parentObj.substring(parentObj.lastIndexOf("_")+1);
        x = obj_menu.left + (obj_menu.clip.width*0.75); // Sets the x-coordinate for the submenu
        deltaY = (isIndex) ? 68 : 49; // Evaluates the deltaY variable based upon whether the page is a 2d level page
        y = (obj_num*20)+deltaY; // Sets the y-coordinate for the submenu
      } else {
        parentObj = getObject(parentObj);
        x = obj_menu.offsetLeft + 75; // Sets the x-coordinate for the submenu
        y = obj_menu.offsetTop+parentObj.offsetTop+8; // Sets the y-coordinate for the submenu
      }
    } else {
      var posArray = eval("ar_" + menuName.id); // Gets the position array for the menu object
      x = (isNav4) ? posArray[0][0] : (isIE) ? posArray[0][1] : posArray[0][2]; // Retrieves the x-coordinate
      deltaY = (isIndex) ? 0 : 19; // Evaluates the deltaY variable based upon whether the page is a 2d level page
      y = posArray[1] - deltaY; // 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 = "visible"; // Sets the menu's visibility property
      }
    } else if (isIE || isDom) {
      menuName.style.left = x+"px"; // Moves the menu along the x-axis
      menuName.style.top = y+"px"; // 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
  }
  if (isIndex && menuIsShown) hideForm(menuName);
}

//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
      }
    }
  }
  if (isIndex) showForm();
}

// Hides all menus; called when the user clicks on the document
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;

  if (isIndex) showForm();
}

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

// 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();
    }
}