/* (c) 2005 CaribMedia
* this module places IFRAME elements behind UL elements
* in order to prevent content to be placed
* on top of the dynamic menus
*/

/*
 * @name  addMenuEffects
 * @description places the IFRAME
 * preconditions:
 *  - there can be only one IFRAME
 *  - the menu has an id of "dynamicMenu"
 *  - the menu is built as a nested list (UL/LI)
* - the script is run in IE
 * @author  Michiel van der Blonk
 * @date  Nov 22, 2005
*/
function addMenuEffects(obj, doShim, doHover)
{
  var iframeCode = '<iframe class="menushim" src="about:blank" scrolling="no" frameborder="0"></iframe>';

  if (!obj)
    return;
  if (doShim)
  {
    // add an IFRAME to all UL's in the menu,
    // this will make IE display the menu on top of all other elements
    var menus = obj.getElementsByTagName("ul");
    for (var i=0; i<menus.length; i++)
    {
      submenu = menus[i];
      submenu.innerHTML=(iframeCode + menus[i].innerHTML);
    }
  }

  if (doHover)
  {
    // one function to show and hide the submenu
    // it also resizes the iframe in the course of it
    // this can not be done in the doShim part
    // (see above) since natural height is calculated
    // during rendering, not during loading of the page
    var items = obj.getElementsByTagName("li");
    for (i=0; i<items.length; i++)
    {
      // the mouse over is quite complex
      items[i].onmouseover = function(){
        var menus = this.getElementsByTagName("ul");
        // apply the hover class
        this.className+=" over";
        // loop through all visible submenus that need to display
        // the iframe, and show it
        for (var i=0; i<menus.length; i++)
        {
          element = menus[i];
          // check if there actually is something there
          if (!element.firstChild)
            continue;
          // and of course it should be an iframe
          if (!element.firstChild.tagName.toUpperCase()=='IFRAME')
            continue;
          iframe = element.firstChild;
          // if the thing actually has a height use this for the iframe
          if (parseInt(element.clientHeight)>0)
            // the 1px is a quick fix, it should actually be the border size
            // since IE 'forgets' to take the border height into account
            iframe.style.height = (parseInt(element.clientHeight)-1) + "px";
        }
      };
      // the mouse out removes the .over class from the list items
      items[i].onmouseout=function(){
        this.className=this.className.replace(" over","");
      };
    }
  }
}

/*
 * @name  startList
 * @description adds the menu effects for several menus
 * @author  Michiel van der Blonk
 * @date  Nov 22, 2005
*/
startList = function()
{
  // check current environment
  isIE = navigator.userAgent.toLowerCase().match(/msie 5.5/i);
  isIE = isIE || navigator.userAgent.toLowerCase().match(/msie 6/i);

  var supportsID = (document.getElementById)?true:false;

  // document.all checks for IE. Al other browsers are OK
  if (isIE && supportsID)
  {
    addMenuEffects(document.getElementById("dynamicMenu"), true, true);
    addMenuEffects(document.getElementById("subMenu"), true, true);
  }
};

/*
 * @name  addEvent
 * @description adds an event handler to an element
 * this way multiple functions can be triggered by an event
 * @author  Michiel van der Blonk
 * @date  Nov 22, 2005
*/
function addEvent(obj, evType, fn)
{
  var ret = false;
  // MB:check if fn exists
  if (!fn)
    return false;
  // adds an eventListener for browsers which support it
  // Written by Scott Andrew: nice one, Scott
  if (obj.addEventListener)
  {
    obj.addEventListener(evType,fn,true);
    ret = true;
  }
  else
    if (obj.attachEvent)
    {
      var r = obj.attachEvent("on"+evType,fn);
      ret = r;
    }
  return ret;
}

//addEvent(window,"load", startList);