jQuery(document).ready(function () {
/*
    document.addEventListener("keydown", function(event) {
        console.log('key:'+ event.which);
    });
*/
    $(window).off('keydown.mm'); // Disable mmenu's keydown
    // Write our own keydown method which prevents tabbing outside the menu when it is open

    // $(document).mouseup(function(e){
    //     var container = $(".mainNavi .nav");
    //     if (!container.is(e.target) && container.has(e.target).length === 0){
    //         container.find('.dropdown-menu:visible').css({'opacity':'0','visibility':'hidden' });
    //     }
    // });

    //$('.dropdown-menu').find('.dropdown-menu-container-right a.dropdown-menu-image-link').addClass('nav-keyboard-last');
    //$('.dropdown-menu').find('.dropdown-menu-container-right a.dropdown-menu-image-link, ul li:last-child a').addClass('nav-keyboard-last');
    //$('.mainNavi').find('.nav-item:last-child .dropdown-menu .dropdown-menu-level-1 .dropdown-col:last-child a').addClass('nav-keyboard-last-closeall');

    $('.nav-keyboard-toggle').on('keydown', function(e) {
        $target = $(e.target);
        $dropdown = $(e.target).next();
        $dropdownUl = $(e.target).find('ul');
        if (e.keyCode == 9) {
            e.preventDefault();
            $($dropdownUl).attr('aria-expanded','true');
            $($dropdownUl).attr('aria-hidden','false');
            $($dropdown).css({'opacity':'1','visibility':'visible', 'top':'100%', 'height':'auto'});
            $($dropdown).find('a:not([tabindex])').attr('tabindex','0');
            $($dropdown).find('.dropdown-item:first >  .dropdown-menu-subnav > li:first > a, .dropdown-item:first > a, .area-subnavi__item:first .area-subnavi__item-link > a').focus();
        }
    });

    $('.nav-keyboard-last').on('keydown', function(e) {
        if( $(this).hasClass('nav-keyboard-last-closeall') ){
            $target = $(e.target);
            $dropdown = $(e.target).next();
            if (e.keyCode == 9) {
                $('.nav-keyboard-toggle').attr('aria-expanded','false');
                $('.dropdown-menu').attr('aria-hidden','true');
                $('.dropdown-menu').removeAttr("style");
            }
            $('.breadcrumb .breadcrumb-item:first-child a').focus();
        }else{
            $target = $(e.target);
            $dropdown = $(e.target).next();
            if (e.keyCode == 9) {
                $('.nav-keyboard-toggle').attr('aria-expanded','false');
                $('.dropdown-menu').attr('aria-hidden','true');
                $('.dropdown-menu').removeAttr("style");
            }
        }

    });

    $('a').on('keydown', function(e) {
        $target = $(e.target);
        $dropdown = $(e.target).next();
        if (e.keyCode == 27) {
            $('.nav-keyboard-toggle').attr('aria-expanded','false');
            $('.dropdown-menu').attr('aria-hidden','true');
            $('.dropdown-menu').removeAttr("style");
        }
    });

    $('.infobar__item-link').on('focus', function(e) {
        $('.infobar.hide-bottom').removeClass('hide-bottom');
    });

    /*
    $('.imageboxslider-slide-button-link').attr('tabindex','-1');

    $('.imageboxslider-slide-button-link').on('keydown', function(e) {
        if (e.keyCode == 9) {

        }
    });
    */





    // $(window).on('keydown', function (e) {
    //     if ($('body').hasClass('nav-main-open')) {
    //         if (e.keyCode == 27) { // esc
    //             $('body').removeClass('nav-main-open');
    //             $('.nav-main-toggle').attr('aria-expanded', 'false');
    //             $('.nav-main-toggle').focus();
    //         } else if (e.keyCode == 9) { // tab
    //             $target = $(e.target);
    //             // // If the event target doesn't have the menu as its parent or it is the last link in the last li
    //             if (!$target.closest('.nav-main').length || ($target.closest('li.nav-item-first').is(':last-child'))) {
    //                 $('.nav-main ul:visible li.nav-item-first:first-child a').focus(); // focus the first menu item link
    //                 $('.nav-main-first-toggle').attr('tabindex','0');
    //                 e.preventDefault();
    //                 return false;
    //             }
    //         }
    //     }
    //     if($('.om-cookie-panel-overlay.active').length > 0){
    //
    //         if (e.keyCode == 9) { // tab
    //
    //             $target = $(e.target);
    //             // // If the event target doesn't have the menu as its parent or it is the last link in the last li
    //             if (!$target.closest('.om-cookie-panel-overlay').length || ($target.closest('.om-cookie-panel-container > div').is(':last-child')) ) {
    //                 $('#cookie-panel__main-headline').focus();
    //                 e.preventDefault();
    //                 return false;
    //             } else if($target.closest('.om-cookie-panel-container > div').is(':last-child')){
    //                 $('.om-cookie-panel-overlay').focus();
    //             }
    //         }
    //     }
    // });
    //
    // $('.nav-item-first > span > a').on('keydown', function(e){
    //    if( $(this).hasClass('hasSubmenu') ){
    //
    //     }else{
    //        if (e.keyCode == 9) {
    //            $('.nav-item-first').removeClass('openD');
    //        }
    //     }
    //     if ($(this).parents('.nav-item-first').is(':first-child')){
    //         if (e.shiftKey && e.keyCode == 9) {
    //             $('#logo a').focus();
    //             $('body').removeClass('nav-main-open');
    //             $('.nav-main-toggle').attr('aria-expanded', 'false');
    //         }
    //     }
    //     if(e.keyCode == 13 ||  e.keyCode == 32 ) {
    //         $(this).trigger('click');
    //     }
    // });
    //
    // $('.nav-item-second > span > a').on('keydown', function(e){
    //     if( $(this).hasClass('hasSubmenuThird') ){
    //         if (e.keyCode == 9) {
    //             if(e.keyCode == 32 ) {
    //                 $(this).trigger('click');
    //             }
    //             if (e.shiftKey && e.keyCode == 9) {
    //                 $(this).parents('.nav-item-second').removeClass('openD');
    //             } else if (e.keyCode == 9) {
    //                 // $('.nav-item-second').removeClass('openD');
    //                 // $(this).parents('.nav-item-second').addClass('openD');
    //                 //$(this).parents('.nav-item-second').find('ul > li.nav-item-third:first-child > a').focus();
    //             }
    //         }
    //     }else{
    //         if (e.keyCode == 9) {
    //             $('.nav-item-second').removeClass('openD');
    //         }
    //     }
    //     if ($(this).parents('.nav-item-second').is(':first-child')){
    //         if (e.shiftKey && e.keyCode == 9) {
    //             $('.nav-item-first').removeClass('openD');
    //         }
    //     }
    //     if ($(this).parents('.nav-item-second').is(':last-child')){
    //         if (e.keyCode == 9) {
    //             $(this).parents('.nav-item-first').removeClass('openD');
    //             $(this).parents('.nav-item-first').find('.nav-main-second, .nav-main-first-toggle').attr('aria-expanded', 'false');
    //             $(this).parents('.nav-item-first').find('ul').attr('aria-hidden', 'true');
    //             $(this).parents('.nav-item-first').find('ul > .nav-item-second > span > a, ul > .nav-item-second > span > .nav-main-second-toggle').attr('tabindex','-1');
    //         }
    //     }
    //     if(e.keyCode == 13 ||  e.keyCode == 32 ) {
    //         $(this).trigger('click');
    //     }
    // });
    //
    // $('.nav-item-third > a').on('keydown', function(e){
    //     if ($(this).parents('.nav-item-third').is(':last-child')){
    //         if (e.keyCode == 9) {
    //             $(this).parents('.nav-item-second').removeClass('openD');
    //             $(this).parents('.nav-item-first').find('.nav-main-third, .nav-main-second-toggle').attr('aria-expanded', 'false');
    //             $(this).parents('.nav-item-second').find('ul').attr('aria-hidden', 'true');
    //             $(this).parents('.nav-item-second').find('ul > .nav-item-third > a').attr('tabindex','-1');
    //         }
    //     }
    //     if(e.keyCode == 13 ||  e.keyCode == 32 ) {
    //         $(this).trigger('click');
    //     }
    // });
    //
    // $('.om-cookie-info-cookiegroup-headline').on('click', function(e) {
    //    if($(this).hasClass('collapsed')){
    //         $(this).next().attr('aria-expanded','true');
    //         $(this).next().find('.om-cookie-info-cookiegroup-description').focus();
    //    }else{
    //        $(this).next().attr('aria-expanded','false');
    //    }
    // });


});