/*
The following code was written by Kevin Clements.  Use it if you like.  If
you use it, all I ask is that you (1) leave all comments, including this
one intact, (2) let me know that you used it by posting a message to the
Milonic Menu mailing list or on-line forums (my username is kevin3442 in
the forums - I'd give my email address, except I don't want it appearing
in any code on anyone's site for any potential spammers to harvest), and
(3) inform me of any improvements you make (I like to learn too).

The following matrix shows the browser/os combinations that I've tested
using Milonic's mmenu.js version 3.5.11.  The combinations marked with Y
are the only combinations I've tested and all have worked. If you modify
the script for any additional browser/os combinations, please add to the
matrix.  --Kevin

"Scott Huber" from the Milonic mailing list verified additional combos,
marked with 'S' in the matrix:

            | OS:
------------|----------------------------------------------------
Browser     | WinXPpro  Win2Kpro  WinNT4srv  WinMe  Win98  Win95
------------|----------------------------------------------------
IE6         |     Y         Y        Y         Y
IE5.5       |               Y        Y
IE5         |                        Y                Y
IE4         |                                                 Y
NS7         |     S         Y                  
NS4.75      |               Y                                 Y
Opera 6.05  |               Y
Opera 7b2   |     S
Mozilla 1.1 |               Y
------------|----------------------------------------------------

Disclaimer: This code is provided as is, with no guarantee that it will work for you.
If you want to use it, you are advised to thoroughly test it first, with the
browsers and OS combinations that you intend to be used to view your site.
*/

/* -----------------------------------------------------------------------
Function: kc_divVisibility()
Description: Used to hide or show a DIV
Parameters: divID - the ID of the DIV whose visibility you want to change.
            state - true = visible, false = hidden
-------------------------------------------------------------------------*/

function kc_divVisibility(divID, state)
{
  if (ns4) {
    document.layers[divID].visibility = state ? "show" : "hide";
  }
  else if (ie4) {
    document.all[divID].style.visibility = state ? "visible" : "hidden";
  }
  else {
    document.getElementById(divID).style.visibility = state ? "visible" : "hidden";
  }
}



/* -----------------------------------------------------------------------
Function: kc_isOpen()
Description: Checks to see if the named Milonic Menu is open.
Parameters: menuName = A string containing the name of the menu to check.
Returns: true if menuName is open, false if menuName is not open
Notes: getMenuByName() and gmstyle() are functions built into the menuing
  system as of mmenu.js version 3.5.11.
-------------------------------------------------------------------------*/

function kc_isOpen(menuName)
{
  var menuStyle = gmstyle("menu" + getMenuByName(menuName));

  if (ns4) {
    return (menuStyle.visibility == "show"); // lousy Netscape 4.x!!!
  }
  else {
    return (menuStyle.visibility != "hidden"); // test != hidden to handle "" default for visible objects
  }
}


/* -----------------------------------------------------------------------
Function: kc_reshowDiv()
Description: Recursive.  Checks to see if all specified menus are closed.
  If one is still open, the function recurses.  If all menus are closed,
  the specified DIV is shown again.
Parameters: divID = The ID of the DIV that is hidden and needs to be
  reshown.  menuNames = A comma-delimited string of Milonic Menu names to
  check.  The order of names is not important.  NO SPACES AFTER COMMAS!!!
Notes: checkInterval is the number of milliseconds between checks to see
  if any of the named menus are open.  I have set mine equal to the
  TimeGap setting that I typically use in my menu array files.  Increase
  this number to slow down checking.  500 milliseconds = half a second.
-------------------------------------------------------------------------*/

function kc_reshowDiv(divID, menuNames)
{
  var checkInterval = 500;  // number of milliseconds between checks to see if menus are open
  var menusAreOpen  = false;
  var functionStr   = "kc_reshowDiv('"  + divID + "','" + menuNames + "')";

  var menuNameArr = menuNames.split(',');

  for (var i=0; i < menuNameArr.length; i++) {
    if (kc_isOpen(menuNameArr[i])) {
      menusAreOpen = true;
      setTimeout(functionStr, checkInterval);
      return;
    }
  }

  if (!menusAreOpen) kc_divVisibility(divID, true);
}


/* -----------------------------------------------------------------------
Function: onFunc()
Description: Called from the onfunction alternate property of a Milonic
  Menu item that opens a submenu that needs to hide a DIV.
Parameters: divID = The ID of the DIV that needs to be hidden when the
  menu item is triggered.  menuNames = A comma-delimited string of Milonic
  Menu names that, when open, should cause the DIV to stay hidden. This
  should include not only the name of the submenu that the selected menu
  item opens, but any adjacent submenus that overlap the DIV as well.
  The order of menu names passed is not important. NO SPACES AFTER COMMAS!!!
Notes: Except as noted (ie5), the browser testing in the if statement is
  done using the browser indicators from mmenu.js, version 3.5.11.
-------------------------------------------------------------------------*/

function kc_onFunc(divID, menuNames)
{
  var ie5 = (navigator.appVersion.indexOf("MSIE 5.0") != -1);  // not set in mmenu.js

  if (ns6 || opra || ie5 || ns4 || ie4) {
    kc_divVisibility(divID, false);
    menuNames = menuNames.replace(/, /gi, ","); // remove a space after a comma
    kc_reshowDiv(divID, menuNames);
  }
}


