(function($){
  $.fn.jMenu = function(options){
    var defaults = {
    }
    
    var $settings = $.extend({}, defaults, options);
    
    this.each(function(){
      var $menu = $(this);
      
      $menu.find('ul').hide();
      $menu.find('ul').css({"z-index": 9999, position: "absolute"})

      // level 1
      $menu.children('li').each(function(){
        var $item = $(this);

        if($item.children('ul').size() > 0){
          //console.log('>> ancho : '+$item.width()+' >> ancho(2)'+$item.outerWidth()+' >> ancho(3)'+$item.outerWidth(true));
          $item.children('ul').css({'min-width': + $item.width()+'px', '_width': + $item.width()+'px'});
          
          $item
            .mouseenter(function(){
							$item.find('ul ul').hide(); // (!)
							
              $item.find('ul:eq(0)')
								.stop(true, true)
								.slideDown('fast');
            })
            .mouseleave(function(){
							$item.find('ul:eq(0)')
								.stop(true, true)
								.hide();
            });
            
          // level 2
          $item.find('ul:eq(0)').children('li').each(function(){
            var $item2 = $(this);
            
            if($item2.children('ul').size() > 0){
              $item2
                .mouseenter(function(){
                  var $left = $item2.parent().outerWidth(true);
                  
                  $item.find('ul ul').hide(); // (!)
                  
                  $item2.find('ul:eq(0)')
										.stop(true, true)
                    .show()
                    .css({left: $left + 'px', top: '0px'});
                })
                .mouseleave(function(){
									if($item2.find('ul:eq(0)').is(':hidden')){
										$item2.find('ul:eq(0)')
											.stop(true, true)
											.hide();
									}
                });
            }
          });
        }
      });
    });
  }
})(jQuery);

