(function ($) {
  "use strict";

    $(document).ready(function() {
    $('.search-field').on({
      focus: function() {
        $('.search-form').addClass('fixed');
      },
      blur: function() {
        $('.search-form').removeClass('fixed');
      }
    });
  });

  $(document).ready(function () {
    var $header = $('.header');

    // 3. Menu Mobile
    var $btnMenu = $('.menu-mobile'),
    $hideMenu = $('.hide-menu'),
    $ShowClose = $('.close-block');

    function setExpanded(open) {
      $btnMenu.find('button').attr('aria-expanded', open ? 'true' : 'false');
    }

    function closeMenu() {
      $header.removeClass('active');
      $hideMenu.removeClass('active');
      $ShowClose.removeClass('active');
      setExpanded(false);
      $btnMenu.find('button').trigger('focus');
    }

    $btnMenu.on('click', function () {
      $header.toggleClass('active');
      if ($header.hasClass('active')) {
        $hideMenu.addClass('active');
        $ShowClose.addClass('active');
        setExpanded(true);
      }
      else {
        closeMenu();
      }
    });

    $hideMenu.on('click', closeMenu);
    $ShowClose.on('click', closeMenu);

    // Escape closes the panel, as it does for any other overlay.
    $(document).on('keyup', function (e) {
      if ((e.key === 'Escape' || e.keyCode === 27) && $header.hasClass('active')) {
        closeMenu();
      }
    });

    var mainMenuListDropdown = $('.main-menu ul li:has(ul)');

    mainMenuListDropdown.each(function () {
        var label = livelyScreenReaderText.expand || 'Expand child menu';
        $(this).append(
            '<button type="button" class="dropdown-plus" aria-expanded="false">' +
            '<span class="screen-reader-text">' + label + '</span></button>'
        );
        $(this).addClass('dropdown_menu');
    });

    $('.dropdown-plus').on('click', function () {
        var $toggle = $(this),
            open = $toggle.attr('aria-expanded') !== 'true';

        $toggle.prev('ul').slideToggle(300);
        $toggle.toggleClass('dropdown-open');
        $toggle.attr('aria-expanded', open ? 'true' : 'false');
        $toggle.find('.screen-reader-text').text(
            open
                ? (livelyScreenReaderText.collapse || 'Collapse child menu')
                : (livelyScreenReaderText.expand || 'Expand child menu')
        );
    });

         //$('.menu-item-has-children a').append('<span></span>');

    $('.btn-close').on('click', function (event) {
        event.preventDefault();
        $header.removeClass('active');
        $hideMenu.removeClass('active');
        $btnMenu.focus();
      });

    // 5. Preload

    var $preload = $('#preload');

    if ($preload.length) {
      $(window).on('load', function () {
        $preload.fadeOut(400);
      });
    }

  });
 
  $('.header').on('keydown', function(e) {
        if ($(window).width() < 992) {
            if($('.header ').hasClass('active')) {
                var focusableEls = $('.header a[href]:not([disabled]), .header button');
                var firstFocusableEl = focusableEls[0];
                var lastFocusableEl = focusableEls[focusableEls.length - 1];
                var KEYCODE_TAB = 9;
                if (e.key === 'Tab' || e.keyCode === KEYCODE_TAB) {
                    if ( e.shiftKey ) /* shift + tab */ {
                        if (document.activeElement === firstFocusableEl) {
                            lastFocusableEl.focus();
                            e.preventDefault();
                        }
                    }
                    else /* tab */ {
                        if (document.activeElement === lastFocusableEl) {
                            firstFocusableEl.focus();
                            e.preventDefault();
                        }
                    }
                }
            }
        }
    });

})(jQuery);
