/*!
 * ScrollTop Script
 * Description: Scroll to top functionality
 * Author: Burger Software
 * License: GPL-3.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-3.0.txt
 */

(function ($) {
    "use strict";

    $(document).ready(function () {

        var path = document.querySelector(".scroll-to-top path");

        if (!path) return;

        var pathLength = path.getTotalLength();

        path.style.transition = path.style.WebkitTransition = "none";

        path.style.strokeDasharray = pathLength + " " + pathLength;
        path.style.strokeDashoffset = pathLength;

        path.getBoundingClientRect();

        path.style.transition = path.style.WebkitTransition = "stroke-dashoffset 10ms linear";

        function updateProgress() {
            var scrollTop = $(window).scrollTop();
            var docHeight = $(document).height() - $(window).height();

            path.style.strokeDashoffset = pathLength - (scrollTop * pathLength / docHeight);
        }

        updateProgress();

        $(window).on("scroll", function () {

            updateProgress();

            if ($(this).scrollTop() > 50) {
                $(".scroll-to-top").addClass("scroll-top-active");
            } else {
                $(".scroll-to-top").removeClass("scroll-top-active");
            }

        });

        $(".scroll-to-top").on("click", function (e) {
            e.preventDefault();

            $("html, body").animate({
                scrollTop: 0
            }, 550);

            return false;
        });

    });

})(jQuery);