// Subheader menu script, created by Robby Edwards, 12May2003

// 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 menuTimer = null; // Variable for the hide menu timer
var flowerCookie = document.cookie.indexOf("sitespecial=flower") != -1; // Check to see if the flower stylesheet is chosen

window.onload = init; // When the page loads execute the function init

function init() {
  document.onmousedown = function(){hideAll()} // sets the event capture for the mouse click (hides the menu when it is showing)
  if (document.getElementById) { // sets the event capture for the display timers for the three menus
    document.getElementById("stuff").onmouseout = function(){setMenuTimer('1200')}
    document.getElementById("stuff").onmouseover = function(){clrMenuTimer()}
    document.getElementById("photos").onmouseout = function(){setMenuTimer('1200')}
    document.getElementById("photos").onmouseover = function(){clrMenuTimer()}
    document.getElementById("links").onmouseout = function(){setMenuTimer('1200')}
    document.getElementById("links").onmouseover = function(){clrMenuTimer()}
  }
  setTabindex();
}


//Main function: displays the menus, controls rollover of images, and menus; Moves the menus when appropriate
function showMenu(menuName, parentObj) {
  if (!document.getElementById) return; // If you don't understand the W3C DOM go away!

  //Hides any currently displayed menus
  hideAll();

  if (menuName) { // Checks to see if display of a menu is necessary
    var isIE = navigator.userAgent.indexOf("MSIE") != -1;
    menuName = document.getElementById(menuName); // Create the menu object

    menuName.fullHeight = menuName.offsetHeight // Gets the height of the menu object

    // Gets the height of the browser window
    if (flowerCookie == true) {
      pageBottom = (isIE) ? (document.documentElement.clientHeight - 21) : (window.pageYOffset + window.innerHeight - 21);
    } else {
      pageBottom = (isIE) ? (document.documentElement.clientHeight - 102) : (window.pageYOffset + window.innerHeight - 112);
    }

    if (menuName.fullHeight < pageBottom) {
      if (isIE) { // Detects windows platform . . .
        menuName.style.top = "28px"; // sets top position for IE
        if (menuName.id == "links") { // If the links menu is shown
          document.getElementById("footer").style.zIndex = -1; // creates a footer object and sets its z-index
        }
        if (flowerCookie == true) { // If the links menu is shown
          document.getElementById("masthead").style.zIndex = -1; // creates a mashead object and sets its z-index
          document.getElementById("content").style.zIndex = -1; // creates a content object and sets its z-index
          document.getElementById("footer").style.zIndex = -1; // creates a footer object and sets its z-index
        }
      }
      menuName.style.visibility = "visible"; // Sets the menu's visibility to 'visible'
    }
    menuArray[menuArray.length] = menuName; // Appends the menu object to menuArray 
    menuIsShown = true; // Indicates that a menu is currently visible
  }
}

// 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 = menuArray[i].style
    menuVis.visibility = "hidden"
  }
  menuArray.length = 0;
  menuIsShown = null;

  if (document.getElementById("masthead").style.zIndex == -1) document.getElementById("masthead").style.zIndex = 1; // if the masthead's z-index is -1 set it to 1
  if (document.getElementById("content").style.zIndex == -1) document.getElementById("content").style.zIndex = 1; // if the content's z-index is -1 set it to 1
  if (document.getElementById("footer").style.zIndex == -1) document.getElementById("footer").style.zIndex = 1; // if the footer's z-index is -1 set it to 1
}

function setMenuTimer(msec) {
  menuIsUnder = false;
  menuTimer = setTimeout("hideAll()",msec);
}

function clrMenuTimer() {
  menuIsUnder = true;
  clearTimeout(menuTimer);
}

function windowCloser() {
  if (window.opener) {
    if (document.createElementNS) {
      var XHTMLNS = 'http://www.w3.org/1999/xhtml';

      var textNode = document.createTextNode("Close this window ");
      var closeLink = document.createElementNS(XHTMLNS,'a');
      closeLink.href = 'javascript:window.close()';
      closeLink.onmouseover = function() {window.status='Close this window'; return true;}
      closeLink.onmouseout = function() {window.status='Done';}
      closeLink.appendChild(textNode);

      var textNode = document.createTextNode(" ");
      var closeWin = document.createElementNS(XHTMLNS,'span');
      closeWin.className="left";
      closeWin.appendChild(closeLink);
      closeWin.appendChild(textNode);

      var topNav = document.getElementById('topNav');
      topNav.appendChild(closeWin);

    } else {
      document.write('<span class="left"><a href="javascript:window.close();" onmouseover="window.status=\'Close this window\'; return true;" onmouseout="window.status=\'Done\';">Close this window<\/a>&nbsp;&nbsp;<\/span>');
    }
  }
}

function setTabindex() {
  if (!document.getElementsByTagName) return;

  var anchors = document.getElementsByTagName('a');
  for (var i=0; i<anchors.length; i++) {
    var anchor = anchors[i];
    if (anchor.getAttribute('href') && !anchor.getAttribute('tabIndex')) {
      anchor.tabIndex = 999;
    }
    if (anchor.getAttribute('href') && anchor.getAttribute('tabIndex')=='100' && !anchor.getAttribute('onfocus')) {
      anchor.onfocus = function(){menuIsUnder = true};
    }
    if (anchor.getAttribute('href') && anchor.getAttribute('tabIndex')=='100' && !anchor.getAttribute('onblur')) {
      anchor.onblur = function(){menuIsUnder = false};
    }
  }
}