( function( window, document ) {

  function timber_work_keepFocusInSidebar() {
    document.addEventListener( 'keydown', function( e ) {
      const timber_work_nav = document.querySelector( '.header-widgets' );

      if ( ! timber_work_nav || ! timber_work_nav.classList.contains( 'show' ) ) {
        return;
      }
      const elements = [...timber_work_nav.querySelectorAll( 'input, a, button' )],
        timber_work_lastEl = elements[ elements.length - 1 ],
        timber_work_firstEl = elements[0],
        timber_work_activeEl = document.activeElement,
        tabKey = e.keyCode === 9,
        shiftKey = e.shiftKey;

      if ( ! shiftKey && tabKey && timber_work_lastEl === timber_work_activeEl ) {
        e.preventDefault();
        timber_work_firstEl.focus();
      }

      if ( shiftKey && tabKey && timber_work_firstEl === timber_work_activeEl ) {
        e.preventDefault();
        timber_work_lastEl.focus();
      }
    } );
  }

  timber_work_keepFocusInSidebar();
} )( window, document );

function timber_work_openSidebar() {
  jQuery(".header-widgets").addClass('show');
}
function timber_work_closeSidebar() {
  jQuery(".header-widgets").removeClass('show');
}

function timber_work_openNav() {
  jQuery(".sidenav").addClass('show');
}
function timber_work_closeNav() {
  jQuery(".sidenav").removeClass('show');
}

var timber_work_btn = jQuery('#button');

jQuery(window).scroll(function() {
  if (jQuery(window).scrollTop() > 300) {
    timber_work_btn.addClass('show');
  } else {
    timber_work_btn.removeClass('show');
  }
});

timber_work_btn.on('click', function(e) {
  e.preventDefault();
  jQuery('html, body').animate({scrollTop:0}, '300');
});

window.addEventListener('load', (event) => {
  jQuery(".loading").delay(2000).fadeOut("slow");
});

jQuery(document).ready(function ($) {
// Menu Js
    $('.submenu-toggle').click(function () {
        $(this).toggleClass('button-toggle-active');
        var currentClass = $(this).attr('data-toggle-target');
        $(currentClass).toggleClass('submenu-toggle-active');
    });
    $('.skip-link-menu-start').focus(function () {
        if (!$("#offcanvas-menu #primary-nav-offcanvas").length == 0) {
            $("#offcanvas-menu #primary-nav-offcanvas ul li:last-child a").focus();
        }
    });
    // Menu Toggle Js
    $('.navbar-control-offcanvas').click(function () {
        $(this).addClass('active');
        $('body').addClass('body-scroll-locked');
        $('#offcanvas-menu').toggleClass('offcanvas-menu-active');
        $('.button-offcanvas-close').focus();
    });
    $('.offcanvas-close .button-offcanvas-close').click(function () {
        $('#offcanvas-menu').removeClass('offcanvas-menu-active');
        $('.navbar-control-offcanvas').removeClass('active');
        $('body').removeClass('body-scroll-locked');
        setTimeout(function () {
            $('.navbar-control-offcanvas').focus();
        }, 300);
    });
    $('#offcanvas-menu').click(function () {
        $('#offcanvas-menu').removeClass('offcanvas-menu-active');
        $('.navbar-control-offcanvas').removeClass('active');
        $('body').removeClass('body-scroll-locked');
    });
    $(".offcanvas-wraper").click(function (e) {
        e.stopPropagation(); //stops click event from reaching document
    });
    $('.skip-link-menu-end').on('focus', function () {
        $('.button-offcanvas-close').focus();
    });
});

jQuery('.header-search-wrapper .search-main').click(function(){
  jQuery('.search-form-main').toggleClass('active-search');
  jQuery('.search-form-main .search-field').focus();
});

if (document.getElementById("openModalButton")) {
  if (document.getElementById("openModalButton").getAttribute("data-modal-src")) {

    var modal = document.getElementById("myModal");
    var openModalButton = document.getElementById("openModalButton");
    var closeModalButton = document.getElementById("closeModalButton");
    var videoPlayer = document.getElementById("videoPlayer");

    openModalButton.addEventListener("click", function () {
      let src = jQuery(this).attr("data-modal-src");
      videoPlayer.src = src;
      modal.style.display = "block";
    });

    closeModalButton.addEventListener("click", function () {
      videoPlayer.src = "";
      modal.style.display = "none";
    });

    window.addEventListener("click", function (e) {
      if (e.target === modal) {
        videoPlayer.src = "";
        modal.style.display = "none";
      }
    });

  }
}

jQuery(window).scroll(function() {
  var data_sticky = jQuery('.main-nav').attr('data-sticky');

  if (data_sticky == "true") {
    if (jQuery(this).scrollTop() > 1){
      jQuery('.main-nav').addClass("stick_header");
    } else {
      jQuery('.main-nav').removeClass("stick_header");
    }
  }
});