/*
 * Droppy 0.1.2
 * (c) 2008 Jason Frame (jason@onehackoranother.com)
 */
fmenu.fn.droppy = function(options) {
    
  options = fmenu.extend({speed: 1}, options || {});
  
  this.each(function() {
    
    var root = this, zIndex = 100001;
    
    function getSubnav(ele) {
      if (ele.nodeName.toLowerCase() == 'li') {
        var subnav = fmenu('> ul', ele);
        return subnav.length ? subnav[0] : null;
      } else {
        return ele;
      }
    }
    
    function getActuator(ele) {
      if (ele.nodeName.toLowerCase() == 'ul') {
        return fmenu(ele).parents('li')[0];
      } else {
        return ele;
      }
    }
    
    function hide() {
      var subnav = getSubnav(this);
      if (!subnav) return;
      fmenu.data(subnav, 'cancelHide', false);
      setTimeout(function() {
        if (!fmenu.data(subnav, 'cancelHide')) {
          fmenu(subnav).slideUp(options.speed);
        }
      }, 1);
    }
  
    function show() {
      var subnav = getSubnav(this);
      if (!subnav) return;
      fmenu.data(subnav, 'cancelHide', true);
      fmenu(subnav).css({zIndex: zIndex++}).slideDown(options.speed);
      if (this.nodeName.toLowerCase() == 'ul') {
        var li = getActuator(this);
        fmenu(li).addClass('hover');
        fmenu('> a', li).addClass('hover');
      }
    }
    
    fmenu('ul, li', this).hover(show, hide);
    fmenu('li', this).hover(
      function() { fmenu(this).addClass('hover'); fmenu('> a', this).addClass('hover'); },
      function() { fmenu(this).removeClass('hover'); fmenu('> a', this).removeClass('hover'); }
    );
    
  });
  
};
