(function() {
    let scrollStep = 0.5;
    let delay = 120;
    let pause = false;

    document.addEventListener('mouseover', () => pause = true);
    document.addEventListener('mouseout', () => pause = false);

    let scrolling = setInterval(() => {
        if (pause) return;

        window.scrollBy(0, scrollStep);

        if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight - 2) {
            clearInterval(scrolling);

            setTimeout(() => {
                goToNextPost();
            }, 3000);
        }

    }, delay);

    function goToNextPost() {
        let nextLink = document.querySelector('a[rel="next"], .nav-next a, .next-post a');

        if (nextLink && nextLink.href) {
            window.location.href = nextLink.href;
        } else {
            setTimeout(() => {
                location.reload();
            }, 3000);
        }
    }
})();
