/**
 * Theme functions file.
 *
 * Contains handlers for navigation.
 */

jQuery(function($){
 	"use strict";
   	jQuery('.main-menu-navigation > ul').superfish({
		delay:       500,
		animation:   {opacity:'show',height:'show'},
		speed:       'fast'
   	});

   	$( window ).scroll( function() {
		if ( $( this ).scrollTop() > 200 ) {
			$( '.back-to-top' ).addClass( 'show-back-to-top' );
		} else {
			$( '.back-to-top' ).removeClass( 'show-back-to-top' );
		}
	});

	// Click event to scroll to top.
	$( '.back-to-top' ).click( function() {
		$( 'html, body' ).animate( { scrollTop : 0 }, 500 );
		return false;
	});

});



function luzuk_wood_house_construction_open() {
	jQuery(".sidenav").addClass('show');
}
function luzuk_wood_house_construction_close() {
	jQuery(".sidenav").removeClass('show');
    jQuery( '.mobile-menu' ).focus();
}

function luzuk_wood_house_construction_menuAccessibility() {

    var luzuk_wood_house_construction_menu = document.querySelector('.nav-menu');

    // ✅ FIX: check first
    if (!luzuk_wood_house_construction_menu) {
        return;
    }

    var links, i, len,
        luzuk_wood_house_construction_iconToggle = document.querySelector('.nav-menu ul li:first-child a');

    let luzuk_wood_house_construction_focusableElements = 'button, a, input';
    let luzuk_wood_house_construction_firstFocusableElement = luzuk_wood_house_construction_iconToggle;

    let luzuk_wood_house_construction_focusableContent = luzuk_wood_house_construction_menu.querySelectorAll(luzuk_wood_house_construction_focusableElements);

    let luzuk_wood_house_construction_lastFocusableElement = luzuk_wood_house_construction_focusableContent[luzuk_wood_house_construction_focusableContent.length - 1];

    links = luzuk_wood_house_construction_menu.getElementsByTagName('a');

    for (i = 0, len = links.length; i < len; i++) {
        links[i].addEventListener('focus', toggleFocus, true);
        links[i].addEventListener('blur', toggleFocus, true);
    }

    function toggleFocus() {
        var self = this;

        while (self && self.className.indexOf('nav-menu') === -1) {
            if (self.tagName && self.tagName.toLowerCase() === 'li') {
                if (self.className.indexOf('focus') !== -1) {
                    self.className = self.className.replace(' focus', '');
                } else {
                    self.className += ' focus';
                }
            }
            self = self.parentElement;
        }
    }

    document.addEventListener('keydown', function (e) {
        let isTabPressed = e.key === 'Tab' || e.keyCode === 9;

        if (!isTabPressed) return;

        if (e.shiftKey) {
            if (document.activeElement === luzuk_wood_house_construction_firstFocusableElement) {
                luzuk_wood_house_construction_lastFocusableElement.focus();
                e.preventDefault();
            }
        } else {
            if (document.activeElement === luzuk_wood_house_construction_lastFocusableElement) {
                luzuk_wood_house_construction_firstFocusableElement.focus();
                e.preventDefault();
            }
        }
    });
}

jQuery(function($){
	$('.mobile-menu').click(function () {
	    luzuk_wood_house_construction_menuAccessibility();
  	});
});