/* =============================================================================
* iMenu.js
*
* Dynamic menuing script for use with CSS-based menu positioning
*
* Documentation online at:
*   https://portal.imagescape.com/trac/wiki/InteractiveMenus
*
* ------------------------------------------------------------------------------
*
* Developed by Jake Kronika <jkronika@imagescape.com>
* For Imaginary Landscape, LLC <www.imagescape.com>
* Copyright (c) 2006-2008
*
* Last updated 2008.02.12
*
* Reuse or modification without permission is prohibited.
* --------------------------------------------------------------------------- */

var debug = false;

var iMenu = function(id, pause, path) {
  this.id = id;
  this.openMenu = null;
  this.pause = (typeof (pause) == 'number' && isFinite (pause) ? pause : 500);
  this.path = path || '/common/js/iMenu/trunk/';

  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]
              && !uls[j].iframe) {
            var lis = uls[j].getElementsByTagName('li');
            uls[j].lis = [];

            for (var k = 0; k < lis.length; k++) {
              if (lis[k].parentNode == uls[j]) {
                uls[j].lis[uls[j].lis.length] = lis[k];
              }
            }

            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.getElementsByTagName('li');

          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;

          this.iMenu.addHover(this);

          for (var i = 0; i < this.as.length; i++) {
            this.iMenu.addHover(this.as[i]);
          }

          for (var i = 0; i < this.uls.length; i++) {
            if (this.uls[i].lis.length) {
              this.uls[i].style.display = 'block';

              var h = parseInt(this.uls[i].offsetHeight) || false,
                  w = parseInt(this.uls[i].offsetWidth) || false;

              if (h && isFinite(h)
                  && w && isFinite(w)) {
                var added = false;

                // 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';
                } else {
                  added = true;
                }

                this.uls[i].iframe.style.position = 'absolute';
                this.uls[i].iframe.style.height = (h-2) + 'px';
                this.uls[i].iframe.style.width = w + 'px';
                this.uls[i].iframe.style.display = 'block';
              
                if (!added) {
                  this.insertBefore(this.uls[i].iframe, this.uls[i]);
                }
              } else if (this.uls[i].iframe) {
                this.uls[i].iframe.style.display = 'none';
              }
            }
          }

          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.remHover(this);

          for (var i = 0; i < this.as.length; i++) {
            this.iMenu.remHover(this.as[i]);
          }

          this.timeout = setTimeout(
            'document.getElementById("' + this.id + '").iMenuHide()',
            this.iMenu.pause);
        };
      }
    }
  }

  this.addHover = function(el) {
    if (this.isClassElement(el)
        && !el.className.match(/\biMenuHover\b/)) {
      el.className += ' iMenuHover';
    }
  };

  this.remHover = function(el) {
    if (this.isClassElement(el)) {
      el.className = el.className.replace(/\t*\biMenuHover\b/g, '');

      if (el.className.match(/^\t*$/)) {
        el.className = null;

        if (el.removeAttribute) {
          el.removeAttribute('class');
        }
      }
    }
  };

  this.isClassElement = function(el) {
    if (el && el.className !== null && el.className !== undefined
        && typeof el.className == 'string') {
      return true;
    }

    return false;
  };
};

var iMenuCalls = (typeof (iMenuCalls) == 'function' ? iMenuCalls : function() {
  return;
});

var preMenuLoad = window.onload;
window.onload = function() {
  if (typeof (preMenuLoad) == 'function') {
    preMenuLoad();
  }

  iMenuCalls();
};
