EASY.scheduledTasks.scheduledOnDocumentReady.push(function () {
    var $richMenuParent = $("#top-menu-rich-more");
    var $richMenuAnchor = $richMenuParent.find(">a");
    var $richMenuMenu = $richMenuParent.find(">ul.menu-children");
    var $topMenuContainer = $("#top-menu-container");
    var $topMenuParents = $topMenuContainer.find(".with-easy-submenu");

    function closeRichMenu() {
        $richMenuMenu.hide();
        ERUI.body.removeClass('left-menu-opened');
        ERUI.document.off('click.richMenuCloser');
        ERUI.document.off('keydown.topRichMenu');
    }

    function openRichMenu() {
        $richMenuMenu.show();
        ERUI.body.addClass('left-menu-opened');
        window.setTimeout(function () {
            ERUI.document.on('click.richMenuCloser', function (e) {
                if ($(e.target).closest("ul.menu-children").length === 0) {
                    closeRichMenu();
                }
            });
        }, 10);
        ERUI.document.on("keydown.topRichMenu", function(e) {
          if(e.which === 27) {
            $("#easy_top_menu_more_rich_menu.open").toggle().parent().children().toggleClass('open');
          }
        });
    }

    $richMenuAnchor.click(function (e) {
        ($richMenuMenu.is(":visible") ? closeRichMenu : openRichMenu)();
    });

    $topMenuParents.each(function (index, parent) {
        var $parent = $(parent);
        var $clickables = $parent.find(">span");
        var $menu = $parent.find(">ul.menu-children");

        function close() {
            $menu.hide();
            ERUI.body.removeClass('top-menu-opened');
            ERUI.document.off('click.topMenuCloserNo' + index);
        }

        function open() {
            $menu.show();
            ERUI.body.addClass('top-menu-opened');
            window.setTimeout(function () {
                ERUI.document.on('click.topMenuCloserNo' + index, function (e) {
                    if ($(e.target).closest("ul.menu-children").length === 0) {
                        close();
                    }
                });
            }, 10);
        }

        $clickables.click(function () {
            ($menu.is(":visible") ? close : open)();
        });
    });

    // probably unused
  var topRichMenuItem = $('.top-menu-rich-more-group > ul > li');
  topRichMenuItem.append('<b class="expander"></b>');
  topRichMenuItem.children('.expander').click(function() {
    $(this).closest('li').toggleClass('open').children('ul').toggleClass('open');
  });
  $('#top-menu-fake-container ').find('.easy-top-menu-more-toggler').removeAttr('data-menu-toggle');
  $('.top-menu-rich-more-group').perfectScrollbar({
    suppressScrollX: true,
    includePadding: true,
    wheelPropagation: false,
    swipePropagation: false
  }).css({overflowY: 'hidden'});

  $("select.select-color").each(function () {
    var obj = $(this);
    obj.addClass(obj.val());
    obj.change(function () {
      obj.addClass(obj.val());
    });
  });
});
