var profily_ready = (callback) => {
    if (document.readyState != "loading") callback();
    else document.addEventListener("DOMContentLoaded", callback);
}

profily_ready(() => { 
    const is_reduced_motion = window.matchMedia(`(prefers-reduced-motion: reduce)`) === true || window.matchMedia(`(prefers-reduced-motion: reduce)`).matches === true;
    if( is_reduced_motion ) {
        console.info( "%cBased on the accessibility settings of your operating system, this website has limited animations.", 'font-weight: bold' );
    }

    /*
    ** Navigation - Fullscreen dropdown collapse
    */
    var navigation_items = document.querySelectorAll('.wp-block-navigation-submenu__toggle');
    if( navigation_items ) {
        navigation_items.forEach( item => {
            item.addEventListener("click", ( item2 ) => {
                item2.target.previousSibling.classList.toggle('is-menu-open');
                item2.target.nextSibling.classList.toggle('is-menu-open');
            });
        });
    }

    /*
    ** Mobile navigation menu and X syncer
    */
    var navigation_icons = document.querySelectorAll('.wp-block-navigation__responsive-container-open');
    if( navigation_icons ) {
        navigation_icons.forEach( item => {
            item.addEventListener("click", () => {
                var top = item.getBoundingClientRect().top;
                document.querySelector('.wp-block-navigation__responsive-dialog').style.paddingTop = ( top ) + 'px';
                if( top > 40 ) {
                    document.querySelector('.wp-block-navigation__responsive-container-close').style.top = top + 'px';
                }
            });
        });
    }

    /*
    ** Animation - Counter
    */
    var counters = document.querySelectorAll('.wp-block-profily-companion-counter .profily-counter-wrapper');
    if( counters ) {
        counters.forEach( counter => {
            new CountUp( counter.querySelector('.profily-counter-number'), counter.getAttribute('data-end'), {
                startVal: parseInt( counter.getAttribute('data-start') ), 
                enableScrollSpy: true,
                duration: !is_reduced_motion ? parseInt( counter.getAttribute('data-duration') ) : 0,
                scrollSpyOnce: false,
                scrollSpyDelay: 100,
            });
        });
    }


    
    /*
    ** Animation - Blocks 
    */
    var sections = document.querySelectorAll('.profily-animation');
    if( sections && !is_reduced_motion ) {
        sections.forEach( (section, index) => {
            section.classList.add( 'animation-inited' );
        });    

        function reveal() {
            sections.forEach((section) => {
                if( profily_is_element_in_viewport( section ) ) {
                    section.classList.add("animate");
                } else {
                    section.classList.remove("animate");
                }
            });
        } 

        window.addEventListener( "load", reveal );
        window.addEventListener( "scroll", reveal );
        window.addEventListener( "resize", reveal );
    }

    function profily_is_element_in_viewport( el ) {
        var rect = el.getBoundingClientRect();
        var windowHeight = ( window.innerHeight || document.documentElement.clientHeight );
        var windowWidth = ( window.innerWidth || document.documentElement.clientWidth );
        var verticallyInView = ( rect.top <= windowHeight ) && (( rect.top + rect.height ) >= 0 );
        var horizontallyInView = ( rect.left <= windowWidth ) && (( rect.left + rect.width ) >= 0 );

        return ( verticallyInView && horizontallyInView );
    }

});