// *****************************************************************************
// PMZ Menu System - 4/28/09 GCS

// Define global variables
var M_timerID         = null;
var M_timerOn         = false;
var M_timerCount      = 500;
var M_timerShowID     = null;
var M_timerShowOn     = false;
var M_timerShowCount  = 500;
var M_menuCount       = 0;
var M_menuWin         = "";

// *****************************************************************************
function getMenuObject(theId) {
  var elm = null;
  if(document.getElementById) {
    // browser implements part of W3C DOM HTML Gecko, Internet Explorer 5+, Opera 5+
    elm = document.getElementById(theId);
  } else if(document.all) {
    // Internet Explorer 4 or Opera with IE user agent
    elm = document.all[theId];
  } else if(document.layers) {
    // Navigator 4
    elm = document.layers[theId];
  } // end else if.
  return elm;
} // end function.

// *****************************************************************************
function M_startTimer() {
  if(M_timerOn == false) {
    M_timerID = setTimeout("M_stopMenus()", M_timerCount);
    M_timerOn = true;
    if(M_timerShowID != null) {
      clearTimeout(M_timerShowID);
    } // end if.
  } // end if.
} // end function.

// *****************************************************************************
function M_stopTimer() {
  if(M_timerOn) {
    clearTimeout(M_timerID);
    M_timerID = null;
    M_timerOn = false;
  } // end if.
} // end function.

// *****************************************************************************
function M_showMenu(menuId) {
  if(M_timerShowOn == false) {
    M_timerShowID = setTimeout("M_dropMenu("+ menuId +")" , M_timerShowCount);
    M_timerShowOn = true;
  } else {
    if(M_timerShowID != null) {
      clearTimeout(M_timerShowID);
      M_timerShowID = setTimeout("M_dropMenu("+ menuId +")" , M_timerShowCount);
    } // end if.
  } // end else.
} // end function.

// *****************************************************************************
function M_dropMenu(menuId) {
  var objHLayer = null;
  var objMLayer = null;

  var set_offset = false;

  var iTop, iLeft, iBottom, iRight, iWidth = 0;
  var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
  var is_safari = navigator.userAgent.toLowerCase().indexOf('safari') > -1;
  var is_ie9    = navigator.userAgent.toLowerCase().indexOf('msie 9.0') > -1;

  if(is_chrome || is_safari || is_ie9) {
    set_offset = true;
  } // end if.

  objHLayer = getMenuObject('cssmh_' + menuId);
  objMLayer = getMenuObject('cssm_menu' + menuId);

  iTop = objHLayer.offsetTop;

  if(objHLayer.offsetTop == 0) {
    if(objHLayer.offsetParent.offsetTop > 0) {
      iTop = objHLayer.offsetParent.offsetTop;
    } // end if.
  } // end if.
  
  if(set_offset) {
    iLeft = objHLayer.offsetParent.offsetLeft + objHLayer.offsetLeft;
  } else {
    iLeft = objHLayer.offsetLeft;
  } // end else.

  if(objHLayer.offsetTop == 0) {
    iBottom = objHLayer.offsetParent.offsetTop + objHLayer.offsetHeight;
  } else {
    iBottom = objHLayer.offsetTop + objHLayer.offsetHeight;
  } // end else.

  iRight = objHLayer.offsetLeft + objHLayer.offsetWidth;

  for(id = menuId; id <= M_menuCount; id++) {
    iWidth += getMenuObject('cssmh_' + id).offsetWidth;
  } // end for-loop.

  // Determine left anchor point by ofsetting width of drop div from end of header
  if(objMLayer.offsetWidth > iWidth) {
    if(set_offset) {
      iLeft = (objHLayer.offsetParent.offsetLeft + objHLayer.offsetLeft) - (objMLayer.offsetWidth - iWidth);
    } else {
      iLeft = objHLayer.offsetLeft - (objMLayer.offsetWidth - iWidth);
    } // end else.
  } // end if.

  M_hideCombos();

  objMLayer.style.top = iBottom  + "px";
  objMLayer.style.left = iLeft  + "px";
  objMLayer.style.visibility = "visible";

  if(objMLayer.style.visibility == "visible") {
    objHLayer.style.background = "url(/images/nav_bg_new_rev.png)";
  } // end if.
  
  clearTimeout(M_timerShowID);
} // end function.

// *****************************************************************************
function M_menuTabOn(menu) {
  menu.className = 'M_menuTabOn';

  // If it looks like we've just moved the cursor off of an open menu and onto this one,
  // then open the current menu as if a click was processed
  if(M_timerOn) {
    var objLayer = null;
    var visCount = 0;
    for (id = 1; id <= M_menuCount; id++ ) {
      objLayer = getMenuObject('cssm_menu'+id);
      getMenuObject('cssmh_' + id).style.background = "url(/images/nav_bg_new.png)"; // resets menu hover.

      if(objLayer.style.visibility == "visible") {
        visCount += 1;
      } // end if.
    } // end for-loop.

    if(visCount > 0) {
      M_hideMenus();
      M_dropMenu(menu.id.substring(6));
    } // end if.
  } // end if.
  M_stopTimer();
} // end function.

// *****************************************************************************
function M_menuTabOff(menu) {
  menu.className = 'M_menuTab';
  M_startTimer();
} // end function.

// *****************************************************************************
function M_stopMenus(){
  M_hideMenus();
  M_timerShowOn = false;
} // end function.

// *****************************************************************************
function M_hideMenus(){
  for(id = 1; id <= M_menuCount; id++ ) {
    M_hideLayer('cssm_menu'+ id);
    getMenuObject('cssmh_' + id).style.background = "url(/images/nav_bg_new.png)"; // resets menu hover.
  } // end for-loop.

  M_showCombos();
} // end function.

// *****************************************************************************
function M_hideLayer(layerName){
  var objLayer = null;
  objLayer = getMenuObject(layerName);
  objLayer.style.visibility = "hidden";
}

// *****************************************************************************
function M_menuDo(url, newWin) {
  if(newWin) {
    var other = "left=0,top=0,screenx=0,screeny=0,toolbar=yes,location=yes,directories=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes";
    M_menuWin = window.open(url,'PMZLink',other);
    M_menuWin.focus();
  } else {
    window.location = url;
  }
}

// *****************************************************************************
function M_hideCombos() {
  if(navigator.appVersion.indexOf("MSIE 6") > 0) {
    var f, e, a = 0, b = 0;

    for(a = 0; a < document.forms.length; a++) {
      f = document.forms[a];
      for(b = 0; b < f.elements.length; b++) {
        e = f.elements[b];
        if( ((e.type == "select-one") || (e.type == "select-multiple")) && (e.style.visibility == 'visible' || e.style.visibility != 'hidden') ) {
          e.style.visibility = 'hidden';
          e.showme = true;
        }
      }
    }
  }
}

// *****************************************************************************
function M_showCombos() {
  if(navigator.appVersion.indexOf("MSIE 6") > 0) {
    var f, e, a = 0, b = 0;

    for(a = 0; a < document.forms.length; a++) {
      f = document.forms[a];
      for(b = 0; b < f.elements.length; b++) {
        e = f.elements[b];
        if( ((e.type == "select-one") || (e.type == "select-multiple")) && e.showme ) {
          e.style.visibility = 'visible';
        }
      }
    }
  }
}

// *****************************************************************************


