// Public Domain - Hauptlogik des Radioplayers (Schaltzentrale) document.addEventListener('DOMContentLoaded', function () { // --- UI ELEMENTE --- const presetContainer = document.getElementById('preset-container'); const stopButton = document.querySelector('.stop-button'); const currentStationName = document.getElementById('current-station-name'); const presetModal = document.getElementById('preset-modal'); const tableBody = document.querySelector('#sortable-tbody'); let currentSound = null; // --- INITIALISIERUNG --- if (window.UI) window.UI.initializeFooter(); initializeEventListeners(); // Manager verknüpfen if (window.SleepTimer) { window.SleepTimer.init(() => stopPlayback()); } if (window.PresetManager) { window.PresetManager.load((loadedPresets) => { displayPresets(loadedPresets); initSortable(); }); } // --- EVENT LISTENER --- function initializeEventListeners() { // Stop Button if (stopButton) { stopButton.onclick = stopPlayback; } // Preset Modal öffnen const editBtn = document.getElementById('edit-presets-button'); if (editBtn) { editBtn.onclick = openPresetModal; } // Sleep Timer Modal öffnen const sleepBtn = document.getElementById('open-sleep-timer-modal'); if (sleepBtn) { sleepBtn.onclick = openSleepModal; } // Sleep Timer Modal schließen (X-Button) const closeSleepBtn = document.getElementById('close-sleep-timer-modal'); if (closeSleepBtn) { closeSleepBtn.onclick = () => { const sm = document.getElementById('sleep-timer-modal'); sm.classList.add('hidden'); sm.style.display = 'none'; }; } // Sleep Timer wirklich ausschalten const offSleepBtn = document.getElementById('off-sleep-timer-modal'); if (offSleepBtn) { offSleepBtn.onclick = () => { if (window.SleepTimer) { window.SleepTimer.clear(); // Stoppt den Timeout und resettet die UI } const sm = document.getElementById('sleep-timer-modal'); if (sm) { sm.classList.add('hidden'); sm.style.display = 'none'; } }; } // Zeit-Optionen im Sleep Timer document.querySelectorAll('.time-option').forEach(btn => { btn.onclick = () => { const minutes = parseInt(btn.getAttribute('data-time')) / 60000; window.SleepTimer.start(minutes); const sm = document.getElementById('sleep-timer-modal'); sm.classList.add('hidden'); sm.style.display = 'none'; }; }); } // --- PLAYER LOGIK --- function playStream(url, item) { stopPlayback(); if (window.MetaHandler) window.MetaHandler.stop(); currentSound = new Howl({ src: [url], html5: true, volume: 0.5, onplay: () => { stopButton.classList.remove('hidden'); const stationName = item.querySelector('h3').textContent; if (window.UI) window.UI.updateDocumentTitle(stationName); currentStationName.textContent = 'Es läuft: ' + stationName; if (window.MetaHandler) window.MetaHandler.start(url); }, onstop: () => { stopButton.classList.add('hidden'); currentStationName.textContent = 'Wähle einen Sender aus'; if (window.UI) window.UI.updateDocumentTitle(''); if (window.MetaHandler) window.MetaHandler.stop(); } }); currentSound.play(); } function stopPlayback() { if (currentSound) { currentSound.stop(); currentSound = null; } } // --- PRESET UI --- function displayPresets(presets) { presetContainer.innerHTML = ''; presets.forEach(preset => { const presetItem = document.createElement('div'); presetItem.classList.add('preset-item'); presetItem.innerHTML = `