// *****************************************************************************
// 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];
  }

 return elm;
}

// *****************************************************************************
function M_startTimer() {
  if (M_timerOn == false) {
    M_timerID = setTimeout("M_stopMenus()" , M_timerCount);
    M_timerOn = true;
    if (M_timerShowID != null) {
      clearTimeout(M_timerShowID);
    }
  }
}

// *****************************************************************************
function M_stopTimer() {
  if (M_timerOn) {
    clearTimeout(M_timerID);
    M_timerID = null;
    M_timerOn = false;
  }
}

// *****************************************************************************
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);
    }
  }
}

// *****************************************************************************
function M_dropMenu(menuId) {
  var objHLayer = null;
  var objMLayer = null;
  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;

  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;
	  }
  }
  if (is_chrome || is_safari)
  {
    iLeft = objHLayer.offsetParent.offsetLeft + objHLayer.offsetLeft;
  }
  else
  {
    iLeft = objHLayer.offsetLeft;
  }
  if (objHLayer.offsetTop == 0)
  {
    iBottom = objHLayer.offsetParent.offsetTop + objHLayer.offsetHeight;
  }
  else
  {
    iBottom = objHLayer.offsetTop + objHLayer.offsetHeight;
  }
  iRight = objHLayer.offsetLeft + objHLayer.offsetWidth;

  for (id=menuId; id <= M_menuCount; id++ ) {
    iWidth += getMenuObject('cssmh_'+id).offsetWidth;
  }

  // Determine left anchor point by ofsetting width of drop div from end of header
  if (objMLayer.offsetWidth > iWidth) {
    if (is_chrome || is_safari)
    {
      iLeft = (objHLayer.offsetParent.offsetLeft + objHLayer.offsetLeft) - (objMLayer.offsetWidth - iWidth);
    }
    else
    {
      iLeft = objHLayer.offsetLeft - (objMLayer.offsetWidth - iWidth);
    }
  }

  M_hideCombos();

  objMLayer.style.top = iBottom +"px";
  objMLayer.style.left = iLeft +"px";
  objMLayer.style.visibility = 'visible';

  clearTimeout(M_timerShowID);
}

// *****************************************************************************
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);
      if (objLayer.style.visibility == "visible") {
        visCount += 1;
      }
    }
    if (visCount > 0) {
      M_hideMenus();
      M_dropMenu(menu.id.substring(6));
    }
  }
  M_stopTimer();
}

// *****************************************************************************
function M_menuTabOff(menu) {
  menu.className = 'M_menuTab';
  M_startTimer();
}

// *****************************************************************************
function M_stopMenus(){
  M_hideMenus();
  M_timerShowOn = false;
}

// *****************************************************************************
function M_hideMenus(){
  for (id = 1; id <= M_menuCount; id++ ) {
    M_hideLayer('cssm_menu'+ id);
  }
  M_showCombos();
}

// *****************************************************************************
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() {
//  alert("Browser: "+ navigator.appName +" - "+ navigator.appVersion);
  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';
        }
      }
    }
  }
}

// *****************************************************************************

