/* This file is used as dependency in:
   ../../modules/blog-post.module/module.html */

pwr.dom.ready(() => {

    // Blog: Social Icon Float
    if (document.querySelector('.vanilla .pwr-post-social--is-float')) {
        var container = document.querySelector(".vanilla.pwr-post-body"),
            fixedHeader = document.querySelector('.vanilla#pwr-header-fixed'),
            offset = 10,
            fixedHeaderHeight = 0,
            startPos = pwr.vanilla.getOffset(container).top,
            fadeOutStart = startPos + pwr.vanilla.getOuterHeight(container) * .96,
            authorProfile = document.querySelector('.vanilla .pwr-author-profile'),
            postComments = document.querySelector('.vanilla .pwr-post-comments');
        if (authorProfile)
            fadeOutStart = pwr.vanilla.getOffset(authorProfile).top;
        else if (postComments)
            fadeOutStart = pwr.vanilla.getOffset(postComments).top;

        if (fixedHeader)
            fixedHeaderHeight = pwr.vanilla.getOffset(fixedHeader).height;

        function socialFloatScrollHandler() {
            document.querySelectorAll(".vanilla .pwr-post-social--is-float .at-share-btn-elements")
                .forEach(target => {
                    if (getComputedStyle(target).position != 'absolute') {
                        return;
                    }
                    var scrollPos = window.scrollY;
                    if (scrollPos + fixedHeaderHeight >= startPos)
                        target.style.top = (scrollPos - startPos + fixedHeaderHeight + offset) + 'px';
                    else
                        target.style.top = offset + 'px';

                    if (scrollPos + pwr.vanilla.getOffset(target).height >= fadeOutStart)
                        pwr.animation.fadeOut(target, 600);
                    else
                        pwr.animation.fadeIn(target, 600);
                });
        };
        socialFloatScrollHandler();
        window.addEventListener('scroll', socialFloatScrollHandler);
    }
});

pwr.dom.ready(() => {
    // Progress Bar
    function progressBar() {
        var winHeight = window.innerHeight,
            postHeight = document.querySelector('.vanilla .pwr-post-content').offsetTop + document.querySelector('.vanilla .pwr-post-content').offsetHeight,
            progressBar = document.querySelector('.vanilla #pwr-progress-bar'),
            max, value;

        if (document.querySelector('.vanilla .pwr-post-comments'))
            postHeight -= document.querySelector('.vanilla .pwr-post-comments').offsetHeight;
        max = postHeight - winHeight;
        progressBar.setAttribute('max', max);
        value = window.scrollY || document.documentElement.scrollTop;
        progressBar.setAttribute('value', value);
    }

    if (document.querySelectorAll('.vanilla #pwr-progress-bar').length > 0) {
        window.addEventListener('scroll', progressBar);
        window.addEventListener('resize', progressBar);
    }

});