(function($) {
    "use strict";

    /* ===============================
       NAVBAR FIXED
    =============================== */
    function minigolf_navbarFixed() {
        if ($('.main-header.is-sticky-on').length) {
            $(window).on('scroll', function() {
                var scroll = $(window).scrollTop();
                if (scroll >= 295) {
                    $(".main-header.is-sticky-on").addClass("header-fixed");
                } else {
                    $(".main-header.is-sticky-on").removeClass("header-fixed");
                }
            });
        }
    }

    /* ===============================
       HOME SLIDER
    =============================== */
    function minigolf_homeSlider() {
        var $owlHome = $('.slider-inner');

        if ($owlHome.length) {
            $owlHome.owlCarousel({
                items: 1,
                autoplay: true,
                autoplayTimeout: 5000,
                margin: 0,
                loop: true,
                dots: false,
                nav: true,
                navText: [
                    '<i class="fa fa-angle-left"></i>',
                    '<i class="fa fa-angle-right"></i>'
                ],
                smartSpeed: 150,
                touchDrag: true,
                mouseDrag: false,
                autoHeight: true,
                responsive: {
                    0: { nav: false },
                    768: { nav: true },
                    992: { nav: true }
                }
            });

            $owlHome.on('translate.owl.carousel', function () {
                var data_anim = $("[data-animation]");
                data_anim.each(function() {
                    var anim_name = $(this).data('animation');
                    $(this).removeClass('animated ' + anim_name).css('opacity', '0');
                });
            });

            $owlHome.on('translated.owl.carousel', function() {
                var data_anim = $owlHome.find('.owl-item.active').find("[data-animation]");
                data_anim.each(function() {
                    var anim_name = $(this).data('animation');
                    $(this).addClass('animated wow ' + anim_name).css('opacity', '1');
                });
            });
        }
    }

    /* ===============================
       SCROLL TO TOP
    =============================== */
    function minigolf_scrollToTop() {
        if ($('.scroll-top').length) {
            $(window).on('scroll', function () {
                if ($(this).scrollTop() > 200) {
                    $('.scroll-top').fadeIn();
                } else {
                    $('.scroll-top').fadeOut();
                }
            });

            $('.scroll-top').on('click', function () {
                $('html, body').animate({ scrollTop: 0 }, 600);
                return false;
            });
        }
    }

    /* ===============================
       MENU ACCESSIBILITY
    =============================== */
    function minigolf_menuAccessibility() {
        $('.navbar-nav').find('a').on('focus blur', function() {
            $(this).parents('ul, li').toggleClass('focus');
        });
    }

    /* ===============================
       DOCUMENT READY
    =============================== */
    $(document).ready(function() {

        // WOW animation
        if (typeof WOW === "function") {
            new WOW().init();
        }

        // Core functions
        minigolf_navbarFixed();
        minigolf_homeSlider();
        minigolf_scrollToTop();
        minigolf_menuAccessibility();

        /* ===============================
           NAVBAR TOGGLER
        =============================== */
        $(".navbar-toggler").on("click", function(e) {

            if ($(this).attr('aria-expanded') === 'false') {
                $(".navbar-menubar").removeClass('active');
                $(".navbar-toggler:not(.navbar-toggler-close)").focus();
            } else {
                $(".navbar-menubar").addClass('active');
                $(".navbar-toggler.navbar-toggler-close").focus();
                e.preventDefault();

                var container = document.querySelector(".navbar-menu");
                if (!container) return;

                let focusable = container.querySelectorAll(
                    'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
                );

                let first = document.querySelector(".navbar-toggler-close");
                let last = focusable[focusable.length - 1];

                document.addEventListener("keydown", function(ev) {
                    if (ev.key === "Tab") {
                        if (ev.shiftKey && document.activeElement === first) {
                            last.focus();
                            ev.preventDefault();
                        } else if (document.activeElement === last) {
                            first.focus();
                            ev.preventDefault();
                        }
                    }
                });
            }
        });

        /* ===============================
           DROPDOWN MENU
        =============================== */
        var dropdownToggle = $('.navbar-nav.main-nav .dropdown > a.nav-link');

        dropdownToggle.after('<button type="button" class="dropdown-icon"></button>');
        dropdownToggle.removeAttr('data-bs-toggle data-bs-target aria-expanded aria-haspopup');

        $(document).on('click', '.dropdown-icon', function() {
            $(this).parent(".menu-item").toggleClass("show");
            $(this).next(".sub-menu").slideToggle();
        });

        $(window).on('resize', function() {
            if (window.matchMedia("(min-width: 992px)").matches) {
                $('.sub-menu.collapse').removeAttr('style');
            }
        });

    });

})(jQuery);