﻿// global.js
// global javascript functions for nfb website

var activePopup = 0;
var timerPopup;

function showNavPopup(menuId) {
  // hide any other menu if open
  if (activePopup > 0 && activePopup != menuId) { hideNavPopup(activePopup); }
  // cancel close timer
  clearTimeout(timerPopup);
  
  if (activePopup != menuId) {
    // show the menu
    var anchor = document.getElementById("popupmenu_" + menuId + "_anchor");
    var menu = document.getElementById("popupmenu_" + menuId);
    menu.style.display = "block";
    menu.style.left = recursePosition(anchor, "left") + "px";
    menu.style.top = (recursePosition(anchor, "top") + anchor.offsetHeight) + "px";
    activePopup = menuId;
    }
  }

function outNavPopup() {
  // set timer to close active popup
  if (activePopup > 0) { timerPopup = setTimeout("hideNavPopup(" + activePopup + ")", 500); }
}

function hideNavPopup(menuId) {
  // hide the menu
  var menu = document.getElementById("popupmenu_" + menuId);
  menu.style.display = "none";
  
  activePopup = 0;
  }

function recursePosition(object, dimension) {
  if (object.tagName == "BODY" || object.tagName == "HTML") {
    if (dimension == "left") { return object.offsetLeft; } else { return object.offsetTop; }
    }
  else {
    if (dimension == "left") {
      return(object.offsetLeft + recursePosition(object.offsetParent, "left"));
      }
    else {
      return(object.offsetTop + recursePosition(object.offsetParent, "top"));
      }
    }
  }