$(document).ready(function () {
    //многоуровневое меню "Действия"
    $('.menu_vert').liMenuVert();
    
});

(function ($) {
    $.fn.liMenuVert = function (params) {
        return this.each(function () {
            var
            menuWrap = $(this),
            menuWrapWidth = menuWrap.outerWidth(),
            menuWrapLeft = menuWrap.offset().left,
            menuSubSub = $('ul', menuWrap);
            

            menuSubSub.each(function() {
                var mArrowRight = $('<div>').addClass('arrow-right'),
                    mSubSub = $(this),
                    mSubList = mSubSub.closest('li'),
                    mSubLink = mSubList.children('a').append(mArrowRight),
                    mSubArrow = $('<div>').addClass('arrow-left').prependTo(mSubSub),
                    mSubArrow2 = $('<div>').addClass('arrow-left2').prependTo(mSubSub);
                var isFirstClick = true;

                mSubLink.on('mouseenter', function () {
                    if (isFirstClick) {
                        mSubSub.show();
                        
                        $(this).next('ul').children('li').width($(this).next('ul').width());
                        mSubArrow.css({ top: mSubLink.outerHeight() / 2 - 5 });
                        mSubArrow2.css({ top: mSubLink.outerHeight() / 2 - 5 });
                        var mSubSubLeft = mSubLink.position().left + mSubLink.outerWidth();
                        mSubSub.css({ top: (mSubLink.position().top - (mSubLink.closest('ul').outerWidth() - mSubLink.closest('ul').width()) / 2) });
                        mSubSub.css({ left: mSubSubLeft });
                        

                        var w3 = $(window).width(),
                            w6 = (mSubSub.offset().left + mSubSub.outerWidth());

                        if (w6 >= w3) {
                            mSubSub.closest('ul').addClass('toLeft');
                            mSubSubLeft = -mSubSub.outerWidth();
                        }
                        if (mSubSub.parents('ul').hasClass('toLeft')) {
                            mSubSubLeft = -mSubSub.outerWidth();
                        }
                        mSubSub.css({ left: mSubSubLeft });
                        mSubLink.addClass('active');
                        mSubLink.css('z-index:10;');
                        isFirstClick = false;
                    } else {
                        mSubLink.addClass('active');
                       /* mSubSub.closest('ul').addClass('toLeft');
                        var offset = menuWrap.offset();
                        //var left = parseInt(mSubSub.css('left'), 10);
                        mSubSub.css('top', offset.top -300).css('left', offset.left - 50);*/
                        mSubSub.show();
                    }

                });
                mSubList.on('mouseleave', function() {
                    mSubSub.hide();
                    mSubLink.removeClass('active');
                });
            });
            menuWrapWidth = menuWrap.outerWidth();
            menuWrapLeft = menuWrap.offset().left;
            $(window).resize(function() {
                menuWrapWidth = menuWrap.outerWidth();
                menuWrapLeft = menuWrap.offset().left;
            });
        });
    };
})(jQuery);