// Globals
var topNavTimer;
var subNavTimer;
var prevRepTimer;
var activeNavIndex;

/*
 * Called on page load. Determines which top nav button is active, if any. 
 * 'active' means we're in that section of the site.
 */
function determineActive() {
  for (var i = 0; i < 4; i++) {
    topNavID = 'topnav_' + i.toString();
    if (document.getElementById(topNavID).src.indexOf('_on.gif') != -1) {
      activeNavIndex = i;
    }
  }
}


/*
 * Previous Reports functions.
 */
function startPrevRepHideTimer() {
  prevRepTimer = setTimeout("hidePreviousReports()", 250);
} 
function hidePreviousReports() {
  document.getElementById('previous_reports').style.visibility = 'hidden';
}
function cancelPrevRepHideTimer() {
  clearTimeout(prevRepTimer);
}
function prevRepMouseOver() {
  cancelPrevRepHideTimer();
  document.getElementById('previous_reports').style.visibility = 'visible';
}

/*
 * Top nav functions.
 */
function startTopnavHideTimer(imgID, index) {
  var code = "hideTopnav('" + imgID + "', " + index.toString() + ")";
  topNavTimer = setTimeout(code, 250);
}
function hideTopnav(imgID, index) {
  if (index != activeNavIndex) {
    document.getElementById(imgID).src = mainNavOff[index].src;  
  }  
}
function cancelTopnavHideTimer() {
  clearTimeout(topNavTimer);
}
function clearTopNav(currIndex) {
  for (var i = 0; i < 4; i++) {
    if (i == currIndex || (i == activeNavIndex)) {
      cancelTopnavHideTimer();
    } else {
      var topNavID = 'topnav_' + i.toString();
      document.getElementById(topNavID).src = mainNavOff[i].src;  
    }
  }
}


/*
 * Subnav functions.
 */
function startSubnavHideTimer(subnavID) {
  var code = "hideSubnav('" + subnavID + "')";
  subNavTimer = setTimeout(code, 250);
}
function hideSubnav(subnavID) {
  document.getElementById(subnavID).style.visibility = 'hidden';
}
function cancelSubnavHideTimer() {
  clearTimeout(subNavTimer);
}
function clearAllSubnavs() {
  document.getElementById('subnav_1').style.visibility = 'hidden';
  document.getElementById('subnav_2').style.visibility = 'hidden';
}


/*
 * Called onmouseover of a main nav image.
 *
 * Parameters:
 *  imgObj - the images that has been moused over.
 *  index - the array index of the imgObj in the preloaded image array.
 *  isOff - a boolean for whether or not the image is in the off state.
 *  NOTE: an optional fourth parameter may be provided. it should be a string
 *  for the id of the subnav.
 */
function mainNavOver(imgObj, index, isOff) {
  clearAllSubnavs();
  clearTopNav(index);
  
  // If the image is in the off state, turn it on.
  if (isOff) {
    imgObj.src = mainNavOn[index].src;
  }
  
  // Optional arg was passed.
  if (arguments.length != 3) {
    // Turn the subnav on.
    cancelSubnavHideTimer();
    document.getElementById(arguments[3]).style.visibility = 'visible';
  }
}


/*
 * Called onmouseout of a main nav image.
 *
 * Parameters:
 *  imgObj - the images that has been moused out of.
 *  index - the array index of the imgObj in the preloaded image array.
 *  isOff - a boolean for whether or not the image is in the off state.
 *  NOTE: an optional fourth parameter may be provided. it should be a string
 *  for the id of the subnav.
 */
function mainNavOut(imgObj, index, isOff) {
  // If the image is in the off state, turn it back off.
  if (isOff) {
    var imgID = 'topnav_' + index.toString();
    startTopnavHideTimer(imgID, index);
  }
  
  // Optional arg was passed.
  if (arguments.length != 3) {
    // Turn the subnav off.
    startSubnavHideTimer(arguments[3]);
  }
}


/*
 * Called onmouseover of the subnav div container.
 */
function subNavOver() {
  cancelSubnavHideTimer();
  cancelTopnavHideTimer();
}


/*
 * Called onmouseout of the subnav div container.
 *
 * Parameters:
 *   subnavID - this.id of the moused out subnav. The index of the subnavID is
 *   the same as the index of the top nav button above it. For example, 
 *   'subnav_1' is below 'topnav_1', both in the page layout and in the site 
 *   folder hierarchy. 
 */
function subNavOut(subnavID) {
  topNavImgID = 'topnav_' + subnavID.slice(subnavID.length-1, subnavID.length);
  topNavImgIndex = Number(subnavID.slice(subnavID.length-1, subnavID.length));
  startTopnavHideTimer(topNavImgID, topNavImgIndex)
  startSubnavHideTimer(subnavID);
}

/*
 * A javascript function that constructs a mailto so that address-gathering spambots
 * won't scrape the email address.
 *
 * Parameters:
 * fullname - The name of the person to mail. This is the text that will appear on the page.
 * mailid - The mail id for the supplied domain.
 * domain - The domain to mail to. For example "duke.edu".
 */
function mailTo(fullname, mailid, domain) {
  document.write('<a href=\"mailto:' +
                    mailid + 
                    "@" + 
                    domain + 
                    "\">" + 
                    fullname + 
                    "</a>");
}