jQuery.fn.adaptiveMenu = function(options) {

    var options = jQuery.extend({
        object: null,
        dropBox: '.dropAdativeMenu', 
        moreBtn: '.moreBtn',
        delBoxes: '',
    }, options);

    var props = {
        menuWidth: 0,
        moreBtnWidth: 0,
        delBoxes: 0,
        fullMenu: 0,
    }

    var methods = {
        init: function() {
            setTimeout(function() {
                methods.menuSize();
                methods.completeMenu(0);
            },150);

            $(window).resize(function() {
                props.fullMenu = 0;
                methods.menuSize();
                methods.completeMenu(1);
            });
        },
        completeMenu: function(flag) {
            allWidth = 0;
            freeSpace = parseInt(props.menuWidth - props.moreBtnWidth - props.delBoxes);
            $(options.object).find(' > ul > li:not(' + options.moreBtn + ')').each(function() {
                elWidth = Math.ceil(parseFloat($.trim($(this).outerWidth(true)))) + 1;
                allWidth = allWidth + elWidth;
                if (allWidth > freeSpace) {
                    el = $(this).clone(true);
                    el.find('ul.nesting').removeClass('to-left').addClass('to-right');
                    (flag) ? $(options.dropBox).prepend(el): $(options.dropBox).append(el);
                    $(this).remove();
                    props.fullMenu = 1;
                }
            });
            if (!props.fullMenu) methods.returnInMenu(allWidth, freeSpace);
            methods.statusMoreBtn();
        },
        returnInMenu: function(aw, fs) {
            $(options.dropBox).find(' > li').each(function() {
                elWidth = Math.ceil(parseFloat($.trim($(this).outerWidth(true)))) + 1;
                aw = aw + elWidth;
                if (aw <= fs) {
                    el = $(this).clone(true);
                    $(options.object).find(options.moreBtn).before(el);
                    $(this).remove();
                }
            });
        },
        statusMoreBtn: function() {
            ($(options.dropBox).find(' > li').length) ? $(options.moreBtn).show(): $(options.moreBtn).hide();
        },
        menuSize: function() {
            props.menuWidth = parseInt($(options.object).width());
            props.moreBtnWidth = parseInt($(options.moreBtn).width());
            if ($(options.delBoxes).length) props.delBoxes = parseInt($(options.delBoxes).width()) + 1;
        }
    };

    return this.each(function() {
        options.object = this;
        methods.init()
    });

};