$(document).ready(function () {
    $('.js-main-slider').slick({arrows: false, dots: true});

    $('.js-reviews').slick({
        slidesToShow: 3,
        arrows: false,
        dots: true,
        responsive: [
          {
            breakpoint: 991,
            settings: {
              slidesToShow: 2,
            }
          },
          {
            breakpoint: 600,
            settings: {
              slidesToShow: 1,
            }
          },
        ]
    });

    $('.js-burger').click(function () {
        $(this).toggleClass('open');
        $('.js-menu').toggleClass('active');
        $('body').toggleClass('fixed');
    });

	$('ul.navmenu').flexMenu({
        cutoff: 1,
		linkText: "...",
		linkTitle: "Показать еще",
		linkTextAll: "Меню",
		linkTitleAll: "Развернуть меню",
        popupClass: 'header-nav-dropdown'
	});

    $(document).on('click', '.js-bar-submenu-toggle', function(e){
        e.preventDefault();
        let $submenu = $(this).parent().parent().find('ul.js-bar-submenu');
        if($submenu.hasClass('hide')){
            $submenu.slideUp();
            $(this).removeClass('hide').addClass('show');
        }
        else{
            $submenu.slideDown();
            $(this).addClass('hide').removeClass('show');
        }
    });

    $(document).on('mouseenter mouseleave click touch', '.navmenu li', function (e) {
      let $dropdown = $(this).find('.header-nav-dropdown');
      if (!$dropdown.length) return;
      e.type == 'mouseleave' ? $dropdown.addClass('d-none') : $dropdown.removeClass('d-none');
    });

    $(document).on('click', '.js-accordion', function(){
        $(this).toggleClass('active');
        let $content = $(this).find('.faq-item__content');
        $content.is(":visible") ? $content.slideUp() : $content.slideDown();
    });

    $(document).on('click', '.js-location', function(){
        let location = $(this).find('a').attr('href');
        window.location.href = location;
    });

    $(document).on('submit', 'form', function (e) {
        e.preventDefault();
        var form = $(this);
        $.ajax({
            url: "ajax/feedbacks.php",
            type: "post",
            data: form.serialize(),
            dataType: 'json',
            success: function(data) {
                form.find('input, button').attr('disabled', 'disabled');
                form.find('button').html('Запрос отправлен');
            }
        });
    });

    $('input[name=phone]').on('input blur focus', (e) => {
        var el = e.target,
            clearVal = $(el).data('phoneClear'),
            pattern = $(el).data('phonePattern'),
            matrix_def = "+7 (___) ___-__-__",
            matrix = pattern ? pattern : matrix_def,
            i = 0,
            def = matrix.replace(/\D/g, ""),
            val = $(el).val().replace(/\D/g, "");

        if (clearVal !== 'false' && e.type === 'blur') {
            if (val.length < matrix.match(/([\_\d])/g).length) {
                $(el).val('');
                return;
            }
        }

        if (def.length >= val.length)
            val = def;

        $(el).val(matrix.replace(/./g, function (a) {
            return /[_\d]/.test(a) && i < val.length ? val.charAt(i++) : i >= val.length ? "" : a;
        }));
    });
});
