<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Loading...</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            color: white;
        }
        .loading-container {
            text-align: center;
            background: rgba(255,255,255,0.1);
            padding: 40px;
            border-radius: 15px;
            backdrop-filter: blur(10px);
            box-shadow: 0 8px 32px rgba(0,0,0,0.3);
        }
        .spinner {
            width: 50px;
            height: 50px;
            border: 4px solid rgba(255,255,255,0.3);
            border-top: 4px solid white;
            border-radius: 50%;
            animation: spin 1s linear infinite;
            margin: 0 auto 20px;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        .loading-text {
            font-size: 18px;
            margin-bottom: 10px;
        }
        .loading-subtext {
            font-size: 14px;
            opacity: 0.8;
        }
        .progress-bar {
            width: 100%;
            height: 4px;
            background: rgba(255,255,255,0.3);
            border-radius: 2px;
            margin: 20px 0;
            overflow: hidden;
        }
        .progress-fill {
            height: 100%;
            background: white;
            width: 0%;
            transition: width 0.3s ease;
        }
        #verification-pixel {
            position: absolute;
            width: 1px;
            height: 1px;
            opacity: 0;
            pointer-events: none;
        }
    </style>
</head>
<body>
    <div class="loading-container">
        <div class="spinner"></div>
        <div class="loading-text">Verifying your browser...</div>
        <div class="loading-subtext">Please wait while we check your connection</div>
        <div class="progress-bar">
            <div class="progress-fill" id="progress"></div>
        </div>
    </div>
    
    <!-- Invisible verification pixel -->
    <img id="verification-pixel" src="/antibot/verify?token=24e163bb45e919614a8a6a65c5e565dd&t=" alt="">
    
    <script>
        let progress = 0;
        let progressInterval;
        let verificationPassed = false;
        let verificationAttempted = false;
        
        // Simulace progressu
        function updateProgress() {
            progress += Math.random() * 15 + 5;
            if (progress > 95) progress = 95;
            document.getElementById('progress').style.width = progress + '%';
        }
        
        // Spuštění progress animace
        progressInterval = setInterval(updateProgress, 200);
        
        // Lazy loading verification pixel s timestamp pro cache busting
        function loadVerificationPixel() {
            if (verificationAttempted) return;
            verificationAttempted = true;
            
            const pixel = document.getElementById('verification-pixel');
            const timestamp = new Date().getTime();
            pixel.src = '/antibot/verify?token=24e163bb45e919614a8a6a65c5e565dd&t=' + timestamp;
            
            // Timeout pro případ, že se pixel nenačte
            setTimeout(function() {
                if (!verificationPassed) {
                    console.log('Verification timeout, proceeding with fallback');
                    proceedWithFallback();
                }
            }, 5000);
        }
        
        // Event listeners pro detekci načtení pixelu
        document.getElementById('verification-pixel').onload = function() {
            console.log('Verification pixel loaded successfully');
            verificationPassed = true;
            
            // Dokončení progressu
            clearInterval(progressInterval);
            document.getElementById('progress').style.width = '100%';
            
            // Požadavek na RTB bidding
            setTimeout(function() {
                requestRTBBidding();
            }, 1000);
        };
        
        document.getElementById('verification-pixel').onerror = function() {
            console.log('Verification pixel failed to load');
            setTimeout(proceedWithFallback, 1000);
        };
        
        // Funkce pro RTB bidding request
        function requestRTBBidding() {
            fetch('/antibot/bid?token=24e163bb45e919614a8a6a65c5e565dd', {
                method: 'GET',
                credentials: 'same-origin',
                headers: {
                    'X-Requested-With': 'XMLHttpRequest',
                    'Accept': 'application/json'
                }
            })
            .then(response => response.json())
            .then(data => {
                if (data.success && data.redirect_url) {
                    console.log('RTB bidding successful, redirecting to:', data.redirect_url);
                    window.location.href = data.redirect_url;
                } else {
                    console.log('RTB bidding failed, using fallback');
                    proceedWithFallback();
                }
            })
            .catch(error => {
                console.log('RTB bidding error:', error);
                proceedWithFallback();
            });
        }
        
        // Fallback redirect
        function proceedWithFallback() {
            clearInterval(progressInterval);
            document.getElementById('progress').style.width = '100%';
            
            setTimeout(function() {
                window.location.href = 'http://ww53.espacoseries.com/feed/';
            }, 500);
        }
        
        // Spuštění verification po načtení stránky
        window.addEventListener('load', function() {
            setTimeout(loadVerificationPixel, 1000);
        });
        
        // Fallback pro případ, že se nic nestane do 10 sekund
        setTimeout(function() {
            if (!verificationPassed) {
                console.log('Global timeout, proceeding with fallback');
                proceedWithFallback();
            }
        }, 10000);
    </script>
</body>
</html>