(function($) {

jQuery.fn.hovermenu = function(options) {

  o = $.extend({
    animSpeed: 400,
    closeDelay: 400,
    linkSelector: "li>a"
  }, options);
  
  return $(this).each(function() {
    var $menu = $(this);
    var timeout = null;
    function mouseOutItem() {
      var $item = $(this);
      $item.closest("li").removeClass("hover");
      $item.unbind("mouseout", mouseOutItem);
      $($item.attr("rel")).stop().animate({ opacity: 0.0 }, o.animSpeed, function() {
        $(this).hide();
      });
    }
    
    function mouseOutTimeout() {
      var e = this;
      clearTimeout(timeout);
      timeout = setTimeout(function() { mouseOutItem.call(e); }, o.closeDelay);
    }
    
    function mouseOverItem() {
      clearTimeout(timeout);
      var $item = $(this);
      $item.closest("ul").find(o.linkSelector).each(mouseOutItem);
      $item.closest("li").addClass("hover");
      $($item.attr("rel")).show().hover(function() {
        clearTimeout(timeout);
      }, function() {
        mouseOutTimeout.call($item[0]);
      }).stop().animate({ opacity: 1.0 }, o.animSpeed, function() {
        $item.mouseout(mouseOutTimeout);
      });
    }
    
    $menu.find(o.linkSelector).mouseover(mouseOverItem);
    $menu.find("a[href=#]").click(function(){ return false; });
    $menu.find(o.linkSelector).each(function() {
      $($(this).attr("rel")).css("opacity", 0.0).hide();
    });
  });
}

})(jQuery);
