// 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;
}
isNav4 = (isNav4 && !isNavOld);

// 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 layerID; var is2d;
var isLoaded = false;
var menuTimer = null; // Variable for the hide menu timer


// Functions for preloading the menu images
function preloadImages(menu_images_array) {
  for(loop = 0; loop < menu_images_array.length; loop++) {
    var an_image = new Image();
    an_image.src = menu_images_array[loop];
  }
}

function doPreload() {
  var menu_images = new Array(
    '/navbar/future_on.gif',
    '/navbar/apply_on.gif',
    '/navbar/current_on.gif',
    '/navbar/alumni_on.gif',
    '/navbar/divider1_left_on.gif',
    '/navbar/divider1_right_on.gif',
    '/navbar/divider2_left_on.gif',
    '/navbar/divider2_right_on.gif',
    '/navbar/divider3_left_on.gif',
    '/navbar/divider3_right_on.gif'
  );
  preloadImages(menu_images);
}


// Function checks the search terms to see if the user selected a search query
function checkTerms() {
  var formObj = document.utk_seek.qtype;
  var n = formObj.options[formObj.selectedIndex].value;

  switch (n) {
    case "none": break;
    default:
      document.utk_seek.submit();
  }
}



// Function for showing today's date
function echoDate() {
  var thisDate = new Date();

  monthName = ["January","February","March","April","May","June","July","August","September","October","Novenber","December"];

  thisMonth = monthName[thisDate.getMonth()];
  thisDay = thisDate.getDate();
  thisYear = thisDate.getFullYear();
  document.write(thisMonth+" "+thisDay+", "+thisYear);
}


// 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";
  }
}

function imgOver(menuName) { // Rollover for the images
  if (menuName == "future") ar_image_names = ['divider1_left',null,null];
  else if (menuName == "apply") ar_image_names = ['divider1_right','divider2_left',null];
  else if (menuName == "current") ar_image_names = [null,'divider2_right','divider3_left'];
  else if (menuName == "alumni") ar_image_names = [null,null,'divider3_right'];

  menuIsUnder = true
  if (document.images) { // Active Images
    document.images[menuName].src = "/navbar/" + menuName + "_on.gif";
    if (ar_image_names[0]) document.images['divider1'].src = "/navbar/" + ar_image_names[0] + "_on.gif";
    if (ar_image_names[1]) document.images['divider2'].src = "/navbar/" + ar_image_names[1] + "_on.gif";
    if (ar_image_names[2]) document.images['divider3'].src = "/navbar/" + ar_image_names[2] + "_on.gif";
  }
}

function imgOff(menuName) { // Roll off for the images
  if (menuName == "future") ar_image_names = ['divider1',null,null];
  else if (menuName == "apply") ar_image_names = ['divider1','divider2',null];
  else if (menuName == "current") ar_image_names = [null,'divider2','divider3'];
  else if (menuName == "alumni") ar_image_names = [null,null,'divider3'];

  menuIsUnder = false
  if (document.images) { // Inactive Images
    document.images[menuName].src = "/navbar/" + menuName + ".gif";
    if (ar_image_names[0]) document.images['divider1'].src = "/navbar/" + ar_image_names[0] + ".gif";
    if (ar_image_names[1]) document.images['divider2'].src = "/navbar/" + ar_image_names[1] + ".gif";
    if (ar_image_names[2]) document.images['divider3'].src = "/navbar/" + ar_image_names[2] + ".gif";
  }
}


// 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) + 132;

    if ((seekTop > 100) && (seekLeft > 464)) {
      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.utk_seek) && (seekTop > 100) && (seekLeft > 464)) {
      document.utk_seek.style.visibility = "hidden";
    }
  }
}

function showForm() {
  if (isNav4) {
    document.layers.seek_layer.visibility = "visible";
  } else if (isIE || isDom) {
    if (document.utk_seek) {
      document.utk_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);
  else imgOver(menuName);

  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 + 105; // 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 y-coordinate
      y = posArray[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 = "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 (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
      }
    }
  }
  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;

  showForm();
}

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

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

// Handles browser resizing problem for navigator 4
if (document.layers) {
  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();
  }
}

