jQuery(function($) {

    /* -----------------------------------------
    Preloader
    ----------------------------------------- */
    $('#preloader').delay(1000).fadeOut();
    $('#loader').delay(1000).fadeOut("slow");

    /* -----------------------------------------
    Navigation
    ----------------------------------------- */
    $('.menu-toggle').click(function() {
        $(this).toggleClass('open');
    });

    /* -----------------------------------------
    Keyboard Navigation
    ----------------------------------------- */
    $(window).on('load resize', charitable_funds_navigation)

    function charitable_funds_navigation(event) {
        if ($(window).width() < 1200) {
            $('.main-navigation').find("li").last().bind('keydown', function(e) {
                if (e.shiftKey && e.which === 9) {
                    if ($(this).hasClass('focus')) {
                    }

                } else if (e.which === 9) {
                    e.preventDefault();
                    $('#masthead').find('.menu-toggle').focus();
                }                
            })
        } else {
            $('.main-navigation').find("li").unbind('keydown')
        }
    }

    charitable_funds_navigation()

    var charitable_funds_primary_menu_toggle = $('#masthead .menu-toggle');
    charitable_funds_primary_menu_toggle.on('keydown', function(e) {
        var tabKey = e.keyCode === 9;
        var shiftKey = e.shiftKey;

        if (charitable_funds_primary_menu_toggle.hasClass('open')) {
            if (shiftKey && tabKey) {
                e.preventDefault();
                const $the_last_li = $('.main-navigation').find("li").last()
                $the_last_li.find('a').focus()
                if (!$the_last_li.hasClass('focus')) {

                    const $is_parent_on_top = true
                    let $the_parent_ul = $the_last_li.closest('ul.sub-menu')

                    let count = 0

                    while (!!$the_parent_ul.length) {
                        ++count

                        const $the_parent_li = $the_parent_ul.closest('li')

                        if (!!$the_parent_li.length) {
                            $the_parent_li.addClass('focus')
                            $the_parent_ul = $the_parent_li.closest('ul.sub-menu')

                            // Blur the cross
                            $(this).blur()
                            $the_last_li.addClass('focus')
                        }

                        if (!$the_parent_ul.length) {
                            break;
                        }
                    }

                }

            };
        }
    })

    // Determine if the document is RTL
    var isRtl = $('html').attr('dir') === 'rtl';
    
    /* -----------------------------------------
    Scroll Top
    ----------------------------------------- */
    var charitable_funds_scrollToTopBtn = $('.charitable-funds-scroll-to-top');

    $(window).scroll(function() {
        if ($(window).scrollTop() > 400) {
            charitable_funds_scrollToTopBtn.addClass('show');
        } else {
            charitable_funds_scrollToTopBtn.removeClass('show');
        }
    });

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

    /* -----------------------------------------
    Scroll Top
    ----------------------------------------- */

    document.addEventListener('DOMContentLoaded', function() {
    const header = document.querySelector('.sticky-header');
    if (header) { // Check if header exists
        window.addEventListener('scroll', function() {
        if (window.scrollY > 0) {
            header.classList.add('is-sticky');
        } else {
            header.classList.remove('is-sticky');
        }
        });
    }
    });

    /* ===============================================
    TABS
    =============================================== */

    function charitable_funds_event_tab(evt, tabName) {
        var charitable_funds_i, charitable_funds_tabcontent, charitable_funds_tablinks;

        charitable_funds_tabcontent = document.getElementsByClassName("tabcontent");
        for (charitable_funds_i = 0; charitable_funds_i < charitable_funds_tabcontent.length; charitable_funds_i++) {
            charitable_funds_tabcontent[charitable_funds_i].style.display = "none";
        }

        charitable_funds_tablinks = document.getElementsByClassName("tablinks");
        for (charitable_funds_i = 0; charitable_funds_i < charitable_funds_tablinks.length; charitable_funds_i++) {
            charitable_funds_tablinks[charitable_funds_i].className = charitable_funds_tablinks[charitable_funds_i].className.replace(" active", "");
        }

        document.getElementById(tabName).style.display = "block";
        evt.currentTarget.className += " active";
    }

    // Auto open first tab
    document.addEventListener("DOMContentLoaded", function() {
        const charitable_funds_firstTab = document.querySelector(".tablinks");
        if(charitable_funds_firstTab) charitable_funds_firstTab.click();
    });

/* Progress Bar */
document.addEventListener("DOMContentLoaded", function () {
    const charitable_funds_progressBar =
        document.getElementById("charitable_funds_aster_progress_bar");
    if (!charitable_funds_progressBar) return;
    window.addEventListener("scroll", function () {
        const charitable_funds_scrollTop =
            document.documentElement.scrollTop || document.body.scrollTop;
        const charitable_funds_height =
            document.documentElement.scrollHeight -
            document.documentElement.clientHeight;
        const charitable_funds_scrolled =
            (charitable_funds_scrollTop / charitable_funds_height) * 100;
        charitable_funds_progressBar.style.width =
            charitable_funds_scrolled + "%";
    });
});