!function(nestedMenuPane, nestedMenuItem, nestedSubMenu) {
  $(function() {
    var openedMenu = [];

    $("table" + nestedMenuPane).each(function(i, table) {
      $(table).find(nestedMenuItem).bind("mouseover", function(event) {
        var cell = $(event.currentTarget).closest("td").get(0);
        var row  = cell.parentNode;

        if (row.rowIndex === row.parentNode.rows.length - 1) {
          // это последняя строка - нет подменю
          return;
        }

        var subCell = row.parentNode.rows[row.rowIndex + 1].cells[cell.cellIndex];

        var subMenu = $(subCell).find(nestedSubMenu);

        var opened = false;
        var count = openedMenu.length;
        while (0 < count--) {
          var sub = openedMenu.shift();

          if (sub.get(0) !== subMenu.get(0)) {
            sub.hide();
          } else {
            opened = true;
            openedMenu.push(sub);
          }
        }

        if (opened) {
          return;
        }

        if (subMenu.length) {
          subMenu.show();

          openedMenu.push(subMenu);
        }
      });

      $(document.body).bind("mousemove", function(event) {
        if (openedMenu.length && ! $.contains(table, event.target)) {
          var count = openedMenu.length;
          while (0 < count--) {
            openedMenu.shift().hide();
          }
        }
      });
    });
  });
}(".nestedMenu", "a.nestedMenuItem", ".nestedSubMenu");
