function regularScrollToTop() {
    document.body.scrollTop = 0;
    document.documentElement.scrollTop = 0;
    if (window.CSTT_scrollableEl) {
        window.CSTT_scrollableEl.scrollTop = 0;
    }
}

function smoothScrollToTop() {
    window.scroll({top: 0, left: 0, behavior: 'smooth'});
    var el = window.CSTT_scrollableEl;
    if (el && el !== document.documentElement && el !== document.body) {
        if (el.scrollTo) {
            el.scrollTo({top: 0, left: 0, behavior: 'smooth'});
        } else {
            el.scrollTop = 0;
        }
    }
}

function fadeOut() {
    var el = document.getElementById('effectiveAppsScrollToTopBtn');
    el.style.opacity = 1;
    (function fade() {
        if ((el.style.opacity -= .1) < 0) {
            el.style.display = "none";
        } else {
            requestAnimationFrame(fade);
        }
    })();
};

function fadeIn(time) {
  var el = document.getElementById('effectiveAppsScrollToTopBtn');
  el.style.opacity = 0;
  var last = +new Date();
  var tick = function() {
    el.style.opacity = +el.style.opacity + (new Date() - last) / time;
    last = +new Date();

    if (+el.style.opacity < 1) {
      (window.requestAnimationFrame && requestAnimationFrame(tick)) || setTimeout(tick, 16);
    }
    else {
        el.style.display = "block";
    }
  };

  tick();
}

function loadSmoothScrollPolyfill(callback) {
    var script = document.createElement('script');
    script.src = 'https://unpkg.com/smoothscroll-polyfill@0.4.4/dist/smoothscroll.min.js';
    var head = document.getElementsByTagName('head')[0],
    done = false;
    head.appendChild(script);
    script.onload = script.onreadystatechange = function() {
        if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
            done = true;
            callback();
            script.onload = script.onreadystatechange = null;
            head.removeChild(script);
        }
    };
}

function CSTT_main() {
    console.group("%cScrolly: Infinite Scroll by Effective Apps", "background: linear-gradient(158deg, rgba(254,0,0,1) 0%, rgba(255,168,0,1) 100%); color: #ffffff; font-weight: 700; font-size: 14px; padding: 4px 8px; border-radius: 6px;");
    console.log(`%c♾️ Infinite Scroll — automatically load the next page as customers scroll\r\n⬆️ Scroll To Top Button — a customizable button to scroll back to the top\r\n📱 Mobile-friendly — works seamlessly on all devices\r\n🎨 Fully customizable — colors, position, animation and more\r\n\r\nLearn more at https://apps.shopify.com/custom-scroll-to-top?utm_source=browser-console&utm_medium=console&utm_campaign=cstt (Need help? Contact us at support@effectify.io)`, "font-size: 14px;");
    console.groupEnd();

    var css = '.js-drawer-open #effectiveAppsScrollToTopBtn { display: none !important; } #effectiveAppsScrollToTopBtn{fill: #783f2a; fill-rule: evenodd;} #effectiveAppsScrollToTopBtn:hover {fill: #9f602d; fill-rule: evenodd;}';

    var head = document.head || document.getElementsByTagName('head')[0], style = document.createElement('style');
    document.head.appendChild(style);
    style.innerHTML = css;
    if (window.CSTT_SCRIPT_INJECTED === undefined) {
        window.CSTT_SCRIPT_INJECTED = true;
        if ('2' === '1') {
            if (ShopifyAnalytics.meta.page.pageType !== "home") {
                return;
            }
        }
        else if ('3' === '1') {
            if (ShopifyAnalytics.meta.page.pageType !== "product") {
                return;
            }
        }
        else if ('4' === '1') {
            if (ShopifyAnalytics.meta.page.pageType !== "collection") {
                return;
            }
        }

        var scrollerConrainerElement = document.createElement('div');

        scrollerConrainerElement.innerHTML = '<svg width="41px" height="41px" role="button" aria-label="Scroll to Top" id="effectiveAppsScrollToTopBtn" onclick="smoothScrollToTop()" style="width: 41px !important; height: 41px !important; display: none; pointer-events: all !important; cursor: pointer; position: fixed; z-index: 100021; left: 50%; bottom: 68px; margin: 0px; padding: 0px; background-color: transparent; margin-left: -20.5px;" version="1.1" viewBox="0 0 26 26" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><g><polygon points="0.046,24.418 2.13,26.502 12.967,15.666 23.803,26.502 25.887,24.418 12.967,11.498  "/><polygon points="0.046,13.418 2.13,15.502 12.967,4.666 23.803,15.502 25.887,13.418 12.967,0.498  "/></g></svg>';

        document.body.appendChild(scrollerConrainerElement);
        document.addEventListener('scroll', function(e) {
            // Some themes scroll an inner wrapper div (not window/document), which never
            // fires on a plain window scroll listener since scroll events don't bubble.
            // Listening on capture catches those too; we then read the position from
            // whichever element actually scrolled.
            var target = e.target;
            var scrollTop;
            if (target === document || target === document.documentElement || target === document.body) {
                scrollTop = window.scrollY || document.documentElement.scrollTop || document.body.scrollTop;
                window.CSTT_scrollableEl = null;
            } else {
                scrollTop = target.scrollTop;
                window.CSTT_scrollableEl = target;
            }

            if (scrollTop > 50 && document.getElementById('effectiveAppsScrollToTopBtn').style.display === "none") {
                fadeIn(300);
            } else if (scrollTop < 50 && document.getElementById('effectiveAppsScrollToTopBtn').style.display === "block") {
                fadeOut();
            }
        }, true);
    }
}

if ('scrollBehavior' in document.documentElement.style) {
    CSTT_main();
}
else {
    loadSmoothScrollPolyfill(CSTT_main);
}