// support functions for dropdowns

var theActiveDropdown = null;
var theActiveDropdownFocused = false;
var theActiveDropdownTimeout = 500;

var theTabOriginalClass = new Array();

// get x offset relative to body
function getAbsOffsetX(pElement) {
  var theRet = 0;
  var theElement = pElement;
  while (theElement != null) {
    theRet += theElement.offsetLeft;
    theElement = theElement.offsetParent;
  }
  return theRet;
}

// get y offset relative to body
function getAbsOffsetY(pElement) {
  var theRet = 0;
  var theElement = pElement;
  while (theElement != null) {
    theRet += theElement.offsetTop;
    theElement = theElement.offsetParent;
  }
  return theRet;
}

// show dropdown menu
// - anchor is the anchor point: if anchor has a 0 height, then we use the parent's height
function showDropdown(pId, pParent) {
  var theOffsetLeft = getAbsOffsetX(pParent);
  var theOffsetTop = getAbsOffsetY(pParent);

  var theDropdown = document.getElementById(pId);

  // close any active dropdown if it's not us
  if ((theActiveDropdown != null) && (theActiveDropdown != theDropdown)) {
    theActiveDropdown.style.visibility = 'hidden';
  }

  theDropdown.style.visibility = 'visible';
  theDropdown.style.left = (theOffsetLeft) + 'px';
  theDropdown.style.top = (theOffsetTop + pParent.offsetHeight) + 'px';

  // begin fixes for IE/mac

  // margin is needed for IE/mac
  var theMarginLeft = (document.body.leftMargin) ? parseInt(document.body.leftMargin) : 0;
  var theMarginTop = (document.body.topMargin) ? parseInt(document.body.topMargin) : 0;

  // IE/mac doesn't add top margins into offset, so we'll do it manually here
  if (theMarginTop > theOffsetTop) {
    theOffsetLeft += theMarginLeft;
    theOffsetTop += theMarginTop;

    theDropdown.style.left = (theOffsetLeft) + 'px';
    theDropdown.style.top = (theOffsetTop + pParent.offsetHeight) + 'px';
    
    // also doesn't auto-width properly
    theDropdown.style.width = "1px";
  }

  // remember the current dropdown
  theActiveDropdown = theDropdown;
  theActiveDropdownFocused = true;
}

// maintain the dropdown in the dropped position
function maintainDropdown() {
  theActiveDropdownFocused = true;
}

// hide dropdown menu
function hideDropdown() {
  // instead of immediately hiding the dropdown, we remember that we should hide it and hide it on timeout if it'is still not focused
  theActiveDropdownFocused = false;
  setTimeout("dropdownSupportTimerCallback()", theActiveDropdownTimeout);
}

// callback to hide the menu after it isn't active for a time
function dropdownSupportTimerCallback() {
  if (theActiveDropdownFocused) {
    // if we're still focused, wait another timeout period
    setTimeout("dropdownSupportTimerCallback()", theActiveDropdownTimeout);
  } else {
    // hide the active menu if it's active
    if (theActiveDropdown != null) {
      theActiveDropdown.style.visibility = 'hidden';
      theActiveDropdown = null;
      theActiveDropdownFocused = false;
    }
  }
}

// mouseover handler for tabs to change it to the selected style
function selectTab(pObj) {
  theTabOriginalClass[pObj] = pObj.className;
  pObj.className = "nav_tab_selected";
}

// mouseout handler for tabs to reset it to the original style
function resetTab(pObj) {
  pObj.className = theTabOriginalClass[pObj];
}

