// Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
// for details on configuring this project to bundle and minify static web assets.

// Write your JavaScript code.

$(document).on('keydown', 'a.header-logo:not(.toggled)', function (e) { 
    if($(window).width() < 992 && e.key == 'Tab' && !e.shiftKey) {
        e.preventDefault();
        if($('#content').find('.burger-icon').length){
            $('#content').find('.burger-icon').trigger('focus')
        } else if ($('a.btn-back').length) {
            $('a.btn-back').trigger('focus')
        }
    }
});

$(document).on('keydown', '#navbar__burger-icon, a.btn-back', function (e) { 
    if($(window).width() < 992 && e.key == 'Tab' && e.shiftKey) {
        $('a.header-logo').trigger('focus')
        e.preventDefault()
    }
});

$(document).on('click', '.burger-icon, .sidebar-overlay', function (e) { 
    $('.sidebar').toggleClass('sidebar-toggled')
    $('.header > .navbar').toggleClass('sidebar-toggled')
    $('#content-wrapper').toggleClass('sidebar-toggled')
    $('a.header-logo').toggleClass('toggled')
});

$(document).on('keypress','.burger-icon', function(event) {
    if (event.which === 13 || event.which === 32) {
        $('.sidebar').toggleClass('sidebar-toggled')
        $('.header > .navbar').toggleClass('sidebar-toggled')
        $('#content-wrapper').toggleClass('sidebar-toggled')
        $('a.header-logo').toggleClass('toggled')
    var sidebarEle = document.querySelector('.sidebar')
        if (sidebarEle.classList.contains('sidebar-toggled')) {
            $('#sidebar__burger-icon').trigger('focus')
        } else {
            $('#content').find('.burger-icon').trigger('focus')
        }
    }
})

$(document).on('keypress','.nav-link', function(event) {
    if (event.which === 13 || event.which === 32) {
        if ($(event.target).next().length) {
            $(event.target).attr('aria-expanded', 'true')
            $(event.target).toggleClass('collapsed')
            $(event.target).parent().find('div.collapse').toggleClass('show')
        } else {
            window.location.href = $(event.target).attr('href');
        }
    }
})

$(document).on('keypress','.collapse-item', function(event) {
    if (event.which === 13 || event.which === 32) {
        window.location.href = $(event.target).attr('href');
    }
})