/* =============================================================================
* iMenu.js
*
* Dynamic menuing script for use with CSS-based menu positioning
*
* ---------------------+--------------------------------------------------------
* Add this to the HEAD |
* ---------------------+
*
* <script type="text/javascript" src="/common/js/iMenu/trunk/iMenu3.js"></script>
* <script type="text/javascript"><!--
*   var iMenuCalls = function() {
*     // create iMenus here as:
*     // new iMenu(_iMenu_ID_);
*   };
* </script>
*
* ---------------------------------+--------------------------------------------
* Required CSS for vertical menus: |
* ---------------------------------+
*
* #iMenu {
*   list-style: none none outside;
*   padding: 0;
*   width: 200px;
* }
*
* #iMenu li {
*   list-style: none none outside;
*   position: relative;
*   width: 100%;
* }
*
* #iMenu li ul {
*   display: none;
*   padding: 0;
*   position: absolute; top: -1px; left: 100%;
*   white-space: nowrap;
*   z-index: 5;
* }
*
* #iMenu iframe {
*   background: #fff none;
*   border: 0;
*   color: #000;
*   position: absolute;
*   z-index: 4;
* }
*
* #iMenu ul li {
*   width: auto;
* }
*
* -----------------------------------+------------------------------------------
* Required CSS for horizontal menus: |
* -----------------------------------+
*
* NOT TESTED
*
* -------------------------------+----------------------------------------------
* Recommended CSS for all menus: |
* -------------------------------+
*
* #iMenu li a:link,
* #iMenu li a:active,
* #iMenu li a:visited {
* }
*
* #iMenu li a.iMenuHasSub:link,
* #iMenu li a.iMenuHasSub:active,
* #iMenu li a.iMenuHasSub:visited {
* }
*
* #iMenu li a:link:hover,
* #iMenu li a:active:hover,
* #iMenu li a:visited:hover {
* }
*
* #iMenu ul li a:link,
* #iMenu ul li a:active,
* #iMenu ul li a:visited {
* }
*
* #iMenu ul li a.iMenuHasSub:link,
* #iMenu ul li a.iMenuHasSub:active,
* #iMenu ul li a.iMenuHasSub:visited {
* }
*
* #iMenu ul li a:link:hover,
* #iMenu ul li a:active:hover,
* #iMenu ul li a:visited:hover {
* }
*
* #iMenu ul li a.iMenuHasSub:link:hover,
* #iMenu ul li a.iMenuHasSub:active:hover,
* #iMenu ul li a.iMenuHasSub:visited:hover {
* }
*
* ------------------------------------------------------------------------------
*
* Developed by Jake Kronika <jkronika@imagescape.com>
* For Imaginary Landscape, LLC <www.imagescape.com>
* Copyright (c) 2006-2007
*
* Last updated 2007.01.04
*
* Reuse or modification without permission is prohibited.
* --------------------------------------------------------------------------- */

var iMenu = function(id, pause, path) {
  this.id = id;
  this.openMenu = null;
  this.pause = (typeof (pause) == 'number' && isFinite (pause) ? pause : 500);
  this.path = path || '/etc/js/';

  if (document.getElementById) {
    this.element = document.getElementById(this.id);
  }

  if (typeof (this.element) == 'object'
      && this.element.getElementsByTagName) {
    this.LIs = this.element.getElementsByTagName('li');
  }

  if (typeof (this.LIs) != 'undefined') {
    for (var i = 0; i < this.LIs.length; i++) {
      // traverse all LIs in the iMenu

      // give each LI an unique ID attribute
      this.LIs[i].id = this.id + '_' + i;
      this.LIs[i].iMenu = this;

      if (this.LIs[i].getElementsByTagName) {
        // this is a DOM-enabled browser capable of using iMenus

        // set up storage of submenus if any exist
        var uls = this.LIs[i].getElementsByTagName('ul');
        this.LIs[i].uls = [];

        for (var j = 0; j < uls.length; j++) {
          if (uls[j].parentNode == this.LIs[i]) {
            this.LIs[i].uls[this.LIs[i].uls.length] = uls[j];
          }
        }

        // set up storage of submenu LIs if any exist
        this.LIs[i].lis = [];

        for (var j = 0; j < this.LIs[i].uls.length; j++) {
          var lis = this.LIs[i].uls[j].getElementsByTagName('li');

          for (var k = 0; k < lis.length; k++) {
            if (lis[k].parentNode == this.LIs[i].uls[j]) {
              this.LIs[i].lis[this.LIs[i].lis.length] = lis[k];
            }
          }
        }

        // set up storage of links if any exist
        var as = this.LIs[i].getElementsByTagName('a');
        this.LIs[i].as = [];

        for (var j = 0; j < as.length; j++) {
          if (as[j].parentNode == this.LIs[i]) {
            this.LIs[i].as[this.LIs[i].as.length] = as[j];

            if (typeof (this.LIs[i].uls) == 'object'
                && this.LIs[i].uls.length > 0) {
              as[j].className += 'iMenuHasSub';
            }
          }
        }

        this.LIs[i].containsNode = function(el) {
          var cn = this.childNodes;

          for (var i = 0; i < cn.length; i++) {
            if (cn[i] == el) {
              return true;
            }
          }

          return false;
        };

        this.LIs[i].iMenuHide = function() {
          if (this.displayed) {
            return;
          }

          if (typeof (this.uls) != 'object') {
            this.uls = this.getElementsByTagName('ul');
          }

          for (var i = 0; i < this.uls.length; i++) {
            if (this.uls[i].parentNode == this) {
              this.uls[i].style.display = 'none';

              if (this.uls[i].iframe) {
                this.uls[i].iframe.style.display = 'none';
              }
            }
          }

          if (typeof (window.iMenuOpen) != 'undefined'
              && window.iMenuOpen == this) {
            window.iMenuOpen = null;
          }
        };

        this.LIs[i].onmouseover = function() {
          clearTimeout(this.timeout);

          if (this.iMenu.openMenu
              && this.iMenu.openMenu != this
              && !this.iMenu.openMenu.containsNode(this)
              && this.iMenu.openMenu.displayed !== true) {
            this.iMenu.openMenu.iMenuHide();
          }

          this.displayed = true;

          for (var i = 0; i < this.uls.length; i++) {
            this.uls[i].style.display = 'block';

            // lazy instantiation of iframes
            if (!this.uls[i].iframe) {
              this.uls[i].iframe = document.createElement('iframe');
              this.uls[i].iframe.src = this.iMenu.path + 'blank.html';
              this.uls[i].iframe.style.height = this.uls[i].offsetHeight + 'px';
              this.uls[i].iframe.style.width = this.uls[i].offsetWidth + 'px';
              this.uls[i].style.zIndex = 5;
              this.uls[i].iframe.style.zIndex = 4;
              this.insertBefore(this.uls[i].iframe, this.uls[i]);
            }

            this.uls[i].iframe.style.display = 'block';
          }

          if (!this.containsNode(this.iMenu.openMenu)) {
            this.iMenu.openMenu = this;
          }

          var parent = this.parentNode;
          while (parent
                 && parent != this.iMenu) {
            if (parent.nodeName.toLowerCase() == 'li') {
              break;
            }

            if (typeof (parent.parentNode) != 'undefined') {
              parent = parent.parentNode;
            }
          }

          for (var i = 0;
               typeof (parent) == 'object'
               && parent
               && i < parent.lis.length; i++) {
            if (parent.lis[i] != this) {
              parent.lis[i].iMenuHide();
            }
          }
        };

        this.LIs[i].onmouseout = function() {
          this.displayed = false;

          this.iMenu;

          this.timeout = setTimeout(
            'document.getElementById("' + this.id + '").iMenuHide()',
            this.iMenu.pause);
        };
      }
    }
  }
};

var iMenuCalls = (typeof (iMenuCalls) == 'function' ? iMenuCalls : function() {
  return;
});

var preMenuLoad = window.onload;
window.onload = function() {
  if (typeof (preMenuLoad) == 'function') {
    preMenuLoad();
  }

  iMenuCalls();
};
