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

function smoothScrollToTop() {
    window.scroll({top: 0, left: 0, behavior: 'smooth'});
}

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.log('%c------ Colorful Scroll To Top Button by Effective Apps is Initializing ------', 'color: cyan');
    console.log('%c------ Contact us at support@effectify.co for help and questions about the app ------', 'color: cyan');
    var css = '.js-drawer-open #effectiveAppsScrollToTopBtn { display: none !important; } #effectiveAppsScrollToTopBtn{fill: #FF6128; fill-rule: evenodd;} #effectiveAppsScrollToTopBtn:hover {fill: #FF6128; 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="60px" height="60px" role="button" aria-label="Scroll to Top" id="effectiveAppsScrollToTopBtn" onclick="smoothScrollToTop()" style="width: 60px !important; height: 60px !important; display: none; pointer-events: all !important; cursor: pointer; position: fixed; z-index: 100021; right: 10px; bottom: 10px !important; margin: 0px; padding: 0px; background-color: transparent; " version="1.1" viewBox="0 0 32 32" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><g data-name="Layer 2" id="Layer_2"><path d="M1,16A15,15,0,1,1,16,31,15,15,0,0,1,1,16Zm2,0A13,13,0,1,0,16,3,13,13,0,0,0,3,16Z"/><path d="M10.41,19.87,16,14.29l5.59,5.58a1,1,0,0,0,1.41,0h0a1,1,0,0,0,0-1.41L16.64,12.1a.91.91,0,0,0-1.28,0L9,18.46a1,1,0,0,0,0,1.41H9A1,1,0,0,0,10.41,19.87Z"/></g></svg>';
        document.body.appendChild(scrollerConrainerElement);
        window.addEventListener('scroll', function() {
            if (window.scrollY > 50 && document.getElementById('effectiveAppsScrollToTopBtn').style.display === "none") {
                fadeIn(300);
            } else if (window.scrollY < 50 && document.getElementById('effectiveAppsScrollToTopBtn').style.display === "block") {
                fadeOut();
            }
        }, false);
    }
}

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