jQuery(document).ready(function($) { function fetchListener() { console.log("Fetching listener data..."); $.ajax({ url: "https://online.radiokiks.sk/api/station/3/listeners", method: "GET", headers: { 'Accept': 'application/json', 'Authorization': 'Bearer 6ff3977277a8b5d2:50b7733ac66086b839ccc6362d1bec49' }, success: function(response) { console.log("API Response:", response); if (Array.isArray(response) && response.length > 0) { const latestListener = response[response.length - 1]; console.log("Latest Listener:", latestListener); if (latestListener.location && latestListener.location.country && latestListener.location.city) { const location = latestListener.location; const flagUrl = `https://flagcdn.com/w40/${location.country.toLowerCase()}.png`; let stationName = "Naše rádio"; if (latestListener.mount_name === "KIKS 128" || latestListener.mount_name === "KIKS 64") { stationName = "Rádio KIKS"; } else if (latestListener.mount_name === "KIKS Rock 128" || latestListener.mount_name === "Kiks Rock 64") { stationName = "Rádio KIKS - Rock Music"; } else if (latestListener.mount_name === "KIKS BIG80s") { stationName = "Rádio KIKS - BIG 80s"; } else if (latestListener.mount_name === "KIKS BIG90s") { stationName = "Rádio KIKS - BIG 90s"; } const message = `${stationName} teraz počúvajú aj v meste ${location.city}, ${location.country}`; $('#listener-flag').attr('src', flagUrl).on('error', function() { $(this).attr('src', 'https://via.placeholder.com/30x20'); // Fallback image }); $('#listener-message').html(message); } else { console.error("Incomplete location data in listener:", latestListener); $('#listener-message').text("Chyba: Dáta o lokalite nie sú dostupné."); } } else { console.warn("API response does not contain valid listener data.", response); $('#listener-message').text("Žiadny nový poslucháč momentálne nie je pripojený."); } }, error: function(xhr, status, error) { console.error("Failed to fetch listener data:", xhr.responseText, status, error); if (xhr.status === 403) { $('#listener-message').text("Prístup k API bol zamietnutý. Skontrolujte autentifikáciu."); } else if (xhr.status === 500) { $('#listener-message').text("Chyba na strane servera API."); } else { $('#listener-message').text("Chyba pri načítaní dát."); } } }); } fetchListener(); // Fetch immediately on load setInterval(fetchListener, 10000); // Check every 10 seconds });