/*

  TOMM (Timed Out Multi Menu) 0.1

  by Viala
  http://viala.hu
  info@viala.hu

  Features:
    - Allow to use on several different menu in one webpage
    - Allow to use timeout to hide the menu items (in milliseconds)
    - Allow to specify hover class name
    - Allow to use a special js function to indicate that the menu item have submenu
    - Fix some IE 6 bug

  Changelog:
     
  28/01/2007
    - First release
    - Some content is based on my TOM (Timed Out Menu) 0.2
    - Fully rewrited into Object Oriented concept


  Licence: Creative Commons Attribution-ShareAlike 2.5 (http://creativecommons.org/licenses/by-sa/2.5/)

  You are free:
    * to Share -- to copy, distribute, display, and perform the work
    * to Remix -- to make derivative works

  Under the following conditions:
    * Attribution. You must attribute the work in the manner specified by the author or licensor.
    * Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one.

  * For any reuse or distribution, you must make clear to others the license terms of this work.
  * Any of these conditions can be waived if you get permission from the copyright holder.

 */

var Menu = {};
Menu.add = function() {};
Menu.add.prototype = {
  init: function(menuId, pars) {
    // this.menu = this;
    this.debug = (typeof pars.debug !== 'undefined') ? pars.debug : true;
    this.menuContainer = document.getElementById(menuId);
    if(!this.menuContainer && this.debug === true) { alert("Can't find the menu container elment. ID: "+ menuId); return false; }
    this.menuLis = this.menuContainer.getElementsByTagName('li');
    this.overName = pars.overName ? pars.overName : 'over';
    this.timeOut = (typeof pars.timeOut !== 'undefined' && isInt(pars.timeOut)) ? pars.timeOut : 100;
    this.ie = getIeType();
    this.ieFrame = (typeof pars.ieFrame !== 'undefined') ? pars.ieFrame : true;
    this.subMenuFunc = (typeof pars.subMenuFunc === 'function') ? pars.subMenuFunc : false;
    
    for(var i = 0; i < this.menuLis.length; i++) {
      // this.menuLis[i].menu = this.menu;
      this.menuLis[i].menu = this;
      this.addOver(this.menuLis[i]);
      this.menuLis[i].id = menuId+'lis'+i;
      if(getFirstUl(this.menuLis[i]) && (typeof this.subMenuFunc == 'function')) {
        this.subMenuFunc(this.menuLis[i]);
      }
    }
  },
  
  addOver: function(obj) {
    obj.onmouseover = function() {
      if(this.timer) {
        clearTimeout(this.timer);
      }
      if(!hasClassName(this.id)) {
        addClassName(this, this.menu.overName);
      }
      if(this.menu.ieFrame) {
      if(window.ie !== false && window.ie === 'ie6' && getFirstUl(this) && !getFirstIframe(this)) {
        /*
         * add an iframe if ie6 or earlier - select|z-index bug
         */
        var ieUL = getFirstUl(this);
        var ieMat = document.createElement('iframe');
		    ieMat.style.height = ieUL.offsetHeight;
		    ieMat.style.width = ieUL.offsetWidth;
        ieUL.insertBefore(ieMat, ieUL.firstChild);
      }
      }
    };
    obj.onmouseout = function(obj) {
       this.timer = setTimeout("removeClassName('"+this.id+"')", this.menu.timeOut);
    };
  }  
};

/*
 * checks that the browser is Microsoft Internet Explorer 6
 */
getIeType = function() {
  ie = (navigator.appName == 'Microsoft Internet Explorer' && !window.opera && document.all) ? Object() : false;
  if(ie !== false) {
    window.ie = (/MSIE 7/i.test(navigator.userAgent)) ? 'ie7' : 'ie6';
  }
};

/*
 * Special function to indicate that the object have child list items
 */
subMenuFunc = function(obj) {
  obj.innerHTML += ' >';
  // addClassName(obj, 'sub');
};
subMenuFuncRight = function(obj) {
  addClassName(obj, 'sub');
};
/*
 * Get the objects first ul element
 */
getFirstUl = function(obj) {
  return obj.getElementsByTagName('ul')[0];
};

/*
 * Get the object first child iframe element
 */
getFirstIframe = function(obj) {
  iframes = obj.getElementsByTagName('iframe');
  for(var i=0; i<iframes.length; i++)
  {
    if(iframes[i].parentNode.parentNode == obj) {
      return iframes[i];
    }
  }
};

/*
 * Add the specified class name to the given object
 */
addClassName = function(obj, cln) {
  obj.className += ' '+cln;
};

/*
 * Remove the class name of the given object
 */
removeClassName = function(objId) {
  obj = document.getElementById(objId);
  cRex = new RegExp(' ?'+obj.menu.overName, 'g');
  obj.className = obj.className.replace(cRex, '');
};
/*
 * Check that the obj have the classname already
 */
hasClassName = function(objId) {
  obj = document.getElementById(objId);
  cRex = new RegExp(' ?'+obj.menu.overName, 'g');
  return obj.className.match(cRex);
}

/*
 * Check, that the given string is an interval number
 */
isInt = function(str) {
  n = new RegExp('^[0-9]+$');
  if(n.test(str)) {
    return true;
  }
  return false;
};

addEvent = function(eventType, func, obj) {
  if (window.attachEvent) { obj.attachEvent('on'+eventType, func); }
  else if (window.addEventListener) { obj.addEventListener(eventType, func, false); }
}





















