
/*
  - Dropmenu Module for Joomla! ----------------------------------------------------------

  Filename:       mod_dropmenu/mod_dropmenu.js
  Version:        1.4
  Release Date:   10/17/06
  Developer:      David Hunt
  Copyright:      2006 Byrgius Technologies
  License:        GNU/GPL (http://www.gnu.org/copyleft/gpl.html)
  Source:         http://software.byrgius.com/

  - Dependants ---------------------------------------------------------------------------

  none

  ----------------------------------------------------------------------------------------
*/

/* -------------------------------- */
function wbDropMenu(){

  this.rowHeight  = 21;
  this.rowMargin  = 34;
  this.toggleInit = 0;
  this.dropTimer  = [];
  this.dropCache  = [];
  this.tumbleDry  = [];
  this.options = {
    'dropSpeed'       :250,
    'tumbleDry'       :true,
    'tumbleDrySpeed'  :10
    };

  // ===================================================================
  this.toggleDrop = function(t,o){
    if(t && o){
      if( !o.className.match(/_hov/) ){
        if( o.down('ul') ){
          Effect.Fade(o.down('ul'),{
            'beforeStart':function(e){
              var eL=e.element.up('li');
              e.element.setStyle({'display':'none'});
              eL.className=eL.className.replace(/(main|sub)/,'$1_hov');
            },'from':0,'to':1,'duration':.3});
        } else
          o.className=o.className.replace(/(main|sub)/,'$1_hov');
      }
      for(var i=0;i<this.dropCache.length;i++)
        if(o==this.dropCache[i]){
          this.dropCache.splice(i,1);
          clearTimeout(this.dropTimer.splice(i,1));
        }
    } else if(!t && o) {
      this.dropCache.push( o );
      this.dropTimer.push( setTimeout("document.wbdmenu.toggleDrop(0,null);",this.options.dropSpeed) );
    } else if(!t && this.dropCache.length) {
      var dropObj = this.dropCache.pop();
      var dropTim = this.dropTimer.pop();
      this.closeSet(dropObj);
    }
  }

  // ===================================================================
  this.closeSet = function(o,t){
    if(!o||!o.tagName){
      if(t&&this.tumbleDry.length){
        var o=$(this.tumbleDry.pop());
        if( o.down('ul') ){
          Effect.Fade(o.down('ul'),{
            'afterFinish':function(e){
              var eL=e.element.up('li');
              eL.className=eL.className.replace(/_hov/,'');
            },'from':1,'to':0,'duration':.3});
        } else
          o.className = o.className.replace(/_hov/,'');
        if(this.tumbleDry.length)
          setTimeout('document.wbdmenu.closeSet(null,true);',this.options.tumbleDrySpeed);
      }
      return;
    }
    if(this.options.tumbleDry){
      if( o.className.match(/_hov/) ){
        if(!this.tumbleDry.length)
          setTimeout('document.wbdmenu.closeSet(null,true);',(this.options.tumbleDrySpeed*this.getLevel(o)));
        this.tumbleDry.push(o);
      }
    } else
      o.className = o.className.replace(/_hov/,'');
    for(var i=0;i<o.childNodes.length;i++)
      if(o.childNodes[i].childNodes.length)
        this.closeSet(o.childNodes[i]);
  }

  // ===================================================================
  this.getLevel = function(o){
    if(o.parentNode&&o.parentNode.tagName)
      if(o.parentNode.tagName.toLowerCase()=='div')
        return 1;
      else if(o.parentNode.tagName.toLowerCase()=='ul')
        if(o.parentNode.className.match(/level/))
          return (o.parentNode.className.replace(/level/,'')*1)+1;
        else
          return 1;
      else
        return this.getLevel(o.parentNodes[i]);
    return 1;
  }

  // ===================================================================
  this.isChildOpen = function(o){
    for(var i=0;i<o.childNodes.length;i++)
      if(o.childNodes[i].tagName)
        if(o.childNodes[i].tagName.toLowerCase()=='li' && o.childNodes[i].className.match(/_hov/))
          return true;
        else if(o.childNodes[i].childNodes.length)
          if(this.isChildOpen(o.childNodes[i]))
            return true;
    return false;
  }

  // ===================================================================
  this.init = function(nodes){
    if(nodes){
      var childCount = 0;
      for(var i=0;i<nodes.length;i++){
        if( nodes[i].nodeName == 'A' )
          nodes[i].title = '';
        if( nodes[i].nodeName == 'LI' ){
          childCount++;
          if( nodes[i].childNodes[0].id != 'active_menu' ){
            nodes[i].wbdmenu = this;
            Event.observe(nodes[i],'mouseover',function(){this.wbdmenu.toggleDrop(1,this);});
            Event.observe(nodes[i],'mouseout',function(){this.wbdmenu.toggleDrop(0,this);});
          }
        }
        if( nodes[i].childNodes.length ){
          var myChildCount = this.init(nodes[i].childNodes);
          if( nodes[i].nodeName == 'UL' ){
            nodes[i].childCount = myChildCount;
            if( myChildCount && nodes[i].id.match(/_up/) ){
              var rowh = nodes[i].getAttribute('rowh'); if(!rowh)rowh = this.rowHeight; rowh = Math.floor(rowh);
              var rowm = nodes[i].getAttribute('rowh'); if(!rowm)rowm = this.rowMargin; rowm = Math.floor(rowm);
              nodes[i].style.marginTop = '-'+((myChildCount*rowh)+rowm)+'px';
            }
          }
        }
      }
      return childCount;
    } else {
      if( this.toggleInit )return null;
      var domElm = 0;
      do {
        var name = 'wbDropMenu'+domElm.toString();
        var menu = document.getElementById(name);
        if( menu )
          menu.childCount = this.init(menu.childNodes);
        domElm = domElm + 1;
      } while((menu !== null) || (typeof menu !== 'object'));
    }
  }

  // ===================================================================
  if(typeof document!='object')return null;
  if(typeof document.wbdmenu!=='undefined')return null;
  document.wbdmenu=this;

}
var wbDropMenuObj = new wbDropMenu();
Event.observe(window,'load',function(){document.wbdmenu.init();});

