/* (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 (var i=0; i<items.length; i++)
    {
      // the mouse over out is quite simple
      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 is quite simple
      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  isInParent
 * @description checks if 'el' is childNode of 'parent'
 * @author  Michiel van der Blonk
 * @date  Nov 22, 2005
*/
function isInParent(el, parent)
{
  // no element?
  if (el == null)
    return true;
  // find my parents children
  var aEls = parent.getElementsByTagName(el.tagName);
  // my parent doesn't have any children? Am I an orphan? Oh no!
  if(aEls && aEls.length==0)
    return false;
  // look for all the children
  for(var i=0;i<aEls.length;i++)
  {
    // is this me?
    if(el==aEls[i])
      return true;
  }
  // I wasn't found
  return false;
}

/*
 * @name  hideDropDowns
 * @description hides <select> elements in two ways
 * for IE < 5.5 deletes them from display
 * for IE >=5.5 places an IFRAME behind the objectmenu
 * @author  Michiel van der Blonk
 * @date  Nov 22, 2005
*/
function hideDropdowns(obj, bool)
{
  // old IE: hide the <select>s
  if (navigator.appVersion.substr(22,3)=="5.0")
  {
    if(bool)
      hideSelects();
    else
      showSelects();
    return;
  }
return;
}

/*
 * @name  maxBorderWidth
 * @description   find the widest border of all child elements
 *      note: only works for UL with LI
 * @author  Michiel van der Blonk
 * @date  Nov 22, 2005
*/
function maxBorderWidth(obj)
{
  // list all LI
//debugger;
  var menuitems = obj.children;
  maxWidth = 0;
  for (var i=0; i<menuitems.length; i++)
  {
    // get this item
    child = menuitems[i];
    if (child.tagName=='LI')
    {
      // get it's width
      border = borderWidth(child);
      // if it's the biggest set it to the new max
      if (border > maxWidth)
        maxWidth = border;
    }
  }
  return maxWidth;
}

/*
 * @name  borderWidth
 * @description returns the borderWidth as a number
 * even if the object has no border
 * @author  Michiel van der Blonk
 * @date  Nov 22, 2005
*/
function borderWidth(obj)
{
  // default to zero
  borderSize = 0;
  if (obj.currentStyle)
  {
    // get left
    if (obj.currentStyle.borderLeftWidth!='')
      borderSize += parseInt(obj.currentStyle.borderLeftWidth);
    // get right
    if (obj.currentStyle.borderRightWidth!='')
      borderSize += parseInt(obj.currentStyle.borderRightWidth);
  }
  return borderSize;
}

/*
 * @name  borderWidth
 * @description returns the vertical borderWidth as a number
 * even if the object has no border
 * @author  Michiel van der Blonk
 * @date  Nov 22, 2005
*/
function verticalBorder()
{
}

/*
 * @name  findPosX
 * @description returns the exact horizontal position from the left
 * takes IE bugs into account
 * returns relative position if necessary
 * @author  Michiel van der Blonk
 * @date  Nov 22, 2005
*/
function findPosX(obj)
{
  var curleft = 0;
  if (obj.offsetParent)
  {
    while (obj.tagName != "BODY" && obj.offsetParent)
    {
      // when the parent is normally positioned (i.e. not a rel or abs)
      // add the offset value
      isPositioned = obj.currentStyle.position=="absolute" || obj.currentStyle.position=="relative";
      if (!(obj.tagName=="DIV" && isPositioned))
        curleft += obj.offsetLeft;
      obj = obj.offsetParent;
    }
  }
  else if (obj.x)
    curleft += obj.x;
  return curleft;
}

/*
 * @name  findPosY
 * @description returns the exact vertical position from the top
 * takes IE bugs into account
 * returns relative position if necessary
 * @author  Michiel van der Blonk
 * @date  Nov 22, 2005
*/
function findPosY(obj)
{
  var curtop = 0;
  borderSize = 0;
  // if there is a parent
  if (obj.offsetParent)
  {
    while (obj.tagName != "BODY" && obj.offsetParent)
    {
      // when the parent is normally positioned (i.e. not a rel or abs)
      // add the offset value
      isPositioned = obj.currentStyle.position=="absolute" || obj.currentStyle.position=="relative";
      if (!(obj.tagName=="DIV" && isPositioned))
        curtop += obj.offsetTop + borderSize;
      obj = obj.offsetParent;
    }
  }
  else if (obj.y)
    curtop += obj.y + borderSize;
  return curtop;
}

/*
 * @name  hideSelect
 * @description hides a <select> element by adding the classname 'hide'
 * this classname can be adjusted in a style sheet
 * @author  Michiel van der Blonk
 * @date  Nov 22, 2005
*/
function hideSelect(id){
  var el=document.getElementById(id);
  if (el!=null)
    el.className+=" hide";
}

/*
 * @name  hideSelect
 * @description hides all <select> elements in the document by adding the classname 'hide'
 * this classname can be adjusted in a style sheet
 * @author  Michiel van der Blonk
 * @date  Nov 22, 2005
*/
function hideSelects()
{
  var oSelects=document.getElementsByTagName("select");
  for(var i=0;i<oSelects.length;i++)
    oSelects[i].className+=" hide";
}

/*
 * @name  showSelects
 * @description shows all <select> elements in the document by removing the classname 'hide'
 * this classname can be adjusted in a style sheet
 * @author  Michiel van der Blonk
 * @date  Nov 22, 2005
*/
function showSelects()
{
  var oSelects=document.getElementsByTagName("select");
  for(var i=0;i<oSelects.length;i++)
    oSelects[i].className=oSelects[i].className.replace(" hide","");
}

/*
 * @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)
{
  // MB:check if fn exists
  if (!fn)
    return;
  // adds an eventListener for browsers which support it
  // Written by Scott Andrew: nice one, Scott
  if (obj.addEventListener)
  {
    obj.addEventListener(evType,fn,true);
    return true;
  }
  else
    if (obj.attachEvent)
    {
      var r = obj.attachEvent("on"+evType,fn);
      return r;
    }
    else
      return false;
}


addEvent(window,"load", startList);