// source --> https://footstepsoffreedom.nl/wp-content/plugins/to-top/public/js/to-top-public.js?ver=3.1 
(function($) {
    "use strict";
    $(function() {
        var container = $("#to_top_scrollup").css({
            'opacity': 0
        });
        var data = to_top_options;

        var mouse_over = false;
        var hideEventID = 0;

        var fnHide = function() {
            clearTimeout(hideEventID);
            if (container.is(":visible")) {
                container.stop().fadeTo(200, 0, function() {
                    container.hide();
                    mouse_over = false;
                });
            }
        };

        var fnHideEvent = function() {
            if (!mouse_over && data.enable_autohide == 1 ) {
                clearTimeout(hideEventID);
                hideEventID = setTimeout(function() {
                    fnHide();
                }, data.autohide_time * 1000);
            }
        };

        var scrollHandled = false;
        var fnScroll = function() {
            if (scrollHandled)
                return;

            scrollHandled = true;

            if ($(window).scrollTop() > data.scroll_offset) {
                container.stop().css("opacity", mouse_over ? 1 : parseFloat(data.icon_opacity/100)).show();

                    fnHideEvent();

            } else {
                fnHide();
            }

            scrollHandled = false;
        };

        if ("undefined" != typeof to_top_options.enable_hide_small_device && "1" == to_top_options.enable_hide_small_device) {
            if ($(window).width() > to_top_options.small_device_max_width) {
                $(window).on( "scroll", fnScroll);
                $(document).on( "scroll", fnScroll);
            }
        }else{
            $(window).on( "scroll", fnScroll);
            $(document).on( "scroll", fnScroll);
        }

        container.on( "hover", function() {
                clearTimeout(hideEventID);
                mouse_over = true;
                $(this).css("opacity", 1);
            }, function() {
                $(this).css("opacity", parseFloat(data.icon_opacity/100));
                mouse_over = false;
                fnHideEvent();
            })
            .on( "click", function() {
                $("html, body").animate({
                    scrollTop: 0
                }, 400);
                return false;
            });
    });
})(jQuery);
// source --> https://footstepsoffreedom.nl/wp-content/plugins/sphere-post-views/assets/js/post-views.js?ver=1.0.1 
/**
 * Sphere Post Views - log AJAX count.
 * 
 * @copyright 2022 ThemeSphere
 */
'use strict';

(() => {
	const STORAGE_KEY = 'sphere-post-views';
	let configs;

	function init(postID) {
		configs = Sphere_PostViews;
		postID  = postID || configs.postID || null;

		if (!window.fetch || !configs || !postID) {
			return;
		}
	
		if (configs.sampling) {
			const rand = Math.floor(Math.random() * configs.samplingRate) + 1;
			if (rand !== 1)  {
				return;
			}
		}

		if (isCrawler()) {
			return;
		}

		// Already counted.
		if (recentlyCounted(postID)) {
			return;
		}

		const params = {
			method: 'POST',
			headers: {
				'Content-type': 'application/x-www-form-urlencoded'
			},
			body: [
				'post_id=' + postID,
				'action=update_views_ajax',
				'token=' + configs.token
			].join('&')
		};

		fetch(configs.ajaxUrl, params)
			.then(resp => resp.text())
			.then(data => logViewCount(postID));
	}

	/**
	 * Check if post count was recently counted.
	 */
	function recentlyCounted(id) {
		if (!configs.repeatCountDelay) {
			return false;
		}

		// Seconds in Hours converted to ms.
		const repeatCountDelay = 3600 * parseFloat(configs.repeatCountDelay) * 1000;
		const viewed = getStorage() || {};

		if (!viewed || !viewed.posts || (!id in viewed.posts)) {
			return false;
		}

		const lastViewed = parseInt(viewed.posts[id]);
		if ((Date.now() - lastViewed) < repeatCountDelay) {
			return true;
		}

		return false;
	}

	/**
	 * @returns {Boolean|Object}
	 */
	function getStorage() {
		let viewed = localStorage.getItem(STORAGE_KEY);
		if (!viewed) {
			return false;
		}

		try {
			viewed = JSON.parse(viewed);

			// Grown too large.
			if (viewed.posts && Object.keys(viewed.posts).length > 10000) {
				viewed = {};
			}

		} catch(e) {
			return false;
		}

		return viewed;
	}

	/**
	 * Add a view count to storage, if needed.
	 * 
	 * @param {Number} id 
	 */
	function logViewCount(id) {
		if (!configs.repeatCountDelay) {
			return;
		}

		const viewed = getStorage() || {};
		viewed.posts = viewed.posts || {};
		viewed.posts[id] = Date.now();

		localStorage.setItem(STORAGE_KEY, JSON.stringify(viewed));
	}

	/**
	 * Minimal crawler detection of popular bots.
	 */
	function isCrawler() {
		if (navigator.webdriver) {
			return true;
		}

		const isBot = /headless|bot|spider|crawl|google|baidu|bing|msn|teoma|slurp|yandex/i.test(navigator.userAgent);
		return isBot;
	}

	document.readyState !== 'loading' 
		? init()
		: document.addEventListener('DOMContentLoaded', () => init());

	document.addEventListener('spc-alp-pageview', e => {
		if (!e.detail.id) {
			return;
		}

		init(e.detail.id);
	});
})();