$(document).ready(function () {

    // Input FILE title change
    let fileblocks = document.querySelectorAll('.file-select');

    fileblocks.forEach(function (fileblock) {
        let label = fileblock.getElementsByClassName('file-label'),
            input = fileblock.getElementsByClassName('fily'),
            tit = label[0].getElementsByClassName('tit'),
            labelVal = label[0].getElementsByClassName('tit').innerHTML;

        input[0].addEventListener('change', function (e) {

            if (this.files[0].size > 10485760) {
                $('.limit').addClass('active');
                this.value = "";
                tit[0].innerHTML = 'Size of the file is too big';
            } else {
                $('.limit').removeClass('active');
                let fileName = '';
                if (this.files && this.files.length > 1)
                    fileName = (this.getAttribute('data-multiple-caption') || '').replace('{count}', this.files.length);
                else
                    fileName = e.target.value.split('\\').pop();

                if (fileName)
                    tit[0].innerHTML = fileName;
                else
                    tit[0].innerHTML = labelVal;
            }

        });
    });

    //Menu-slider on mobile
    $('.sliding-menu').each(function () {
        var it = $(this),
            back = it.find('.back-btn'),
            slideInner = it.find('.sliding-inner'),
            dropLink = it.find('.drop-link > a'),
            counter = 0,
            times = 0.3;

        slideInner.css({
            transition: times + 's'
        });

        $('.sliding-menu').on('click', '.drop-link > a', function (e) {
            e.preventDefault();

            counter = ++counter;

            let dropMenu = $(this).siblings('.drop-menu');

            if (counter > 0) {
                back.addClass('active');
                slideInner.css({
                    transform: 'translateX(-' + counter + '00%)'
                });
            }

            dropMenu.show().addClass('open');

        });

        $(document).on('click', '.back-btn', function (e) {
            e.preventDefault();

            counter = --counter;

            if (counter < 1) {
                back.removeClass('active');
                slideInner.css({
                    transform: 'translateX(0)'
                });
            } else {
                slideInner.css({
                    transform: 'translateX(-' + counter + '00%)'
                });
            }
            setTimeout(function () {
                it.find('.drop-menu.open').eq(counter).hide().removeClass('open');
            }, times + 100);

        });

    });


    // Mobile menu open / close
    $('.humb').click(function () {
        $('.mobile-menu, .humb, .menu-back').toggleClass('active');
    });
    $('.menu-back').click(function () {
        $(this).removeClass('active')
        $('.mobile-menu, .humb').removeClass('active');
    });
});


$(window).on('load resize', function () {
    appendBlocks('.main-menu-move', 0, 1199, '.mobile-menu .sliding-inner');
    appendBlocks('.main-menu-move', 1199, 0, '.main-menu-desktop');

    appendBlocks('.languages-move', 0, 1199, '.manage');
    appendBlocks('.languages-move', 1199, 0, '.languages-desktop');

    appendBlocks('.contacts-move', 0, 1199, '.for-other');
    appendBlocks('.contacts-move', 1199, 0, '.contacts-desktop');
});

// Block place changing
function appendBlocks(block, windowMin, windowMax, appendTo) {
    var exists = $(appendTo).find(block)

    if (!exists.length) {
        if (windowMax == 0) {
            if ($(window).width() > windowMin) {
                $(block).appendTo($(appendTo));
            }
        } else {
            if ($(window).width() > windowMin && $(window).width() < windowMax) {
                $(block).appendTo($(appendTo));
            }
        }
    }
}

$(window).on('load', function () {
    if ($(window).width() > 1199 && $('.h-bottom').length) {
        menuFixed('.h-bottom', 0);
    }
});

// Fixed menu on scroll
function menuFixed(menu, offseting) {

    var height = $(menu).outerHeight(),
        offsetParametr = offseting,
        offsetTop = $(menu).offset().top + offsetParametr,

        wrapper = menu.replace(/[\s.#]/g, '') + '-wrapper'; // removing dot and hashtag

    $(menu).wrap('<div class="' + wrapper + '"></div>');
    $('.' + wrapper).css({
        minHeight: height
    });

    $(window).scroll(function () {
        if ($(window).scrollTop() >= offsetTop) {
            $(menu).addClass('active');
        } else {
            $(menu).removeClass('active');
        }
    });
}