(function($){
  $.fn.jMenu = function(options){
    var defaults = {
    }
    
    var $settings = $.extend({}, defaults, options);
    
    this.each(function(){
      var $menu = $(this);

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

        //if($item.children('ul').size() > 0){
          $item.hoverIntent(function(){
            $('ul ul', $item).hide(); // (!)
            $('h2', $item).addClass('actived');
            $('h2 a', $item).addClass('actived');
            
            $ul = $('ul:eq(0)', $item);
            
            pos = $item.position();
            width = $ul.outerWidth(true, true);
            x = (pos.left+width) - 960; // (!)
            left = x > 0 ? pos.left - x : pos.left;
            
            $ul
              .stop(true, true)
              .css({left: left+'px'})
              .slideDown('fast');
          }, function(){
            if($item.find('ul:eq(0)').length > 0){
            $item.find('ul:eq(0)')
              .stop(true, true)
              .fadeOut('fast', function(){
                $('h2', $item).removeClass('actived');
                $('h2 a', $item).removeClass('actived');
              });
            } else{
              $('h2', $item).removeClass('actived');
              $('h2 a', $item).removeClass('actived');
            }
          });
            
          // level 2
          /***
          $('ul:eq(0)', $item).children('li').each(function(){
            var $item_2 = $(this);
            
            if($item_2.children('ul').size() > 0){
              
              $item.hoverIntent(function(){
                var $left = $item_2.parent().outerWidth(true);
                
                $('ul ul', $item).hide(); // (!)
                
                $ul_2 = $('ul:eq(0)', $item_2);
                
                $ul_2
                  .stop(true, true)
                  .show()
                  .css({left: $left + 'px', top: '0px'});
              }, function(){
                if($ul_2.is(':hidden')){
                  $ul_2
                    .stop(true, true)
                    .hide();
                }
              });
            }
          });
          ***/
        //}
      });
    });
  }
})(jQuery);

