(function($) { 'use strict'; // OAuth-Redirect-Handler (function() { try { // Prüfe ob wir von OAuth kommen (über Startseite) if (window.location.search.indexOf('wg_auth_redirect=1') !== -1) { var targetUrl = sessionStorage.getItem('wg_login_redirect'); sessionStorage.removeItem('wg_login_redirect'); sessionStorage.removeItem('wg_login_success'); if (targetUrl) { window.location.replace(targetUrl); return; } } } catch(e) {} })(); // Suche nach Login wiederherstellen (function() { try { var isLoggedIn = document.body && document.body.classList.contains('logged-in'); if (isLoggedIn) { var savedSearch = localStorage.getItem('wg_pending_search'); if (savedSearch) { localStorage.removeItem('wg_pending_search'); // Parse gespeicherte Suche var searchData = JSON.parse(savedSearch); var searchUrl = searchData.url; // Prüfe ob Suche noch gültig (max 30 Min alt) if (searchData.timestamp && (Date.now() - searchData.timestamp) < 30 * 60 * 1000) { // Redirect zur Suchseite mit Parametern (nur wenn nicht bereits dort) var currentPath = window.location.pathname; if (searchUrl && currentPath.indexOf('/entdecken/') === -1 && currentPath.indexOf('/suche/') === -1) { window.location.replace(searchUrl); return; } } } } } catch(e) { console.log('WG Search restore error:', e); } })(); // Markiere dass User eingeloggt ist - für spätere Prüfung (function() { try { if (document.body.classList.contains('logged-in')) { sessionStorage.setItem('wg_was_logged_in', '1'); } } catch(e) {} })(); // Reset Loading States bei Browser-Back (pageshow/bfcache) $(window).on('pageshow', function(event) { // Prüfe ob Seite aus dem bfcache geladen wurde if (event.originalEvent && event.originalEvent.persisted) { // Reset alle Loading-States $('.loading').removeClass('loading').prop('disabled', false); $('.wg-loading').removeClass('wg-loading'); // Reset Select-Dropdowns $('.wg-status-select').each(function() { $(this).prop('disabled', false); }); // Schließe alle offenen Modals $('.wg-modal').remove(); } // Immer bei pageshow: Reset Loading States (auch ohne bfcache) setTimeout(function() { $('.loading').removeClass('loading').prop('disabled', false); $('.wg-loading').removeClass('wg-loading'); }, 100); }); // Auch bei normalem Page Load: Stelle sicher dass keine Loading-States hängen $(document).ready(function() { $('.loading').removeClass('loading').prop('disabled', false); $('.wg-loading').removeClass('wg-loading'); }); // Toast-Benachrichtigungen function showToast(message, type = 'success') { const toast = $('
' + message + '
'); $('body').append(toast); setTimeout(() => { toast.fadeOut(300, () => toast.remove()); }, 3000); } // Button Loading States $(document).on('submit', '.wg-form, form[action*="admin-post.php"]', function() { const $btn = $(this).find('button[type="submit"]'); $btn.addClass('loading').prop('disabled', true); }); // Status-Select Loading $(document).on('change', '.wg-status-select', function() { const $select = $(this); $select.closest('form').addClass('wg-loading'); }); // AJAX fuer Watchlist-Aktionen (ohne Page Reload) $(document).on('submit', 'form[action*="wg_update_status"], form[action*="wg_remove_from_watchlist"]', function(e) { e.preventDefault(); const $form = $(this); const formData = $form.serialize(); $.post($form.attr('action'), formData, function(response) { showToast('Watchlist aktualisiert!', 'success'); // Aktualisiere UI ohne Reload if ($form.attr('action').includes('remove')) { $form.closest('.wg-card').fadeOut(300, function() { $(this).remove(); }); } }).fail(function() { showToast('Fehler beim Aktualisieren', 'error'); }); return false; }); // Modal fuer Empfehlungen $(document).on('click', '.wg-btn-recommend', function(e) { e.preventDefault(); const $btn = $(this); const media = $btn.data('media'); const id = $btn.data('id'); const title = $btn.data('title'); // Nutze admin_url fuer action const adminPostUrl = wgData.ajaxUrl.replace('admin-ajax.php', 'admin-post.php'); // Baue Detail-URL fuer Link-Sharing const detailUrl = window.location.origin + '/' + media + '/' + id + '-' + title.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '') + '/'; const modal = '
' + '
' + '
' + '' + '
' + '

"' + title + '" empfehlen

' + '
' + '
' + '' + '' + '' + '' + '' + '' + '
' + '' + '' + '
' + '
' + '' + '' + '
' + '
' + '' + '' + '
' + '' + '
' + '' + '' + '
' + '
' + '
' + '
'; $('body').append(modal); setTimeout(function() { $('#wg-rec-email').focus(); }, 100); }); // Link generieren $(document).on('click', '.wg-generate-link', function(e) { e.preventDefault(); const email = $('#wg-rec-email').val().trim(); // Validiere E-Mail if (!email || !email.match(/^[^\s@]+@[^\s@]+\.[^\s@]+$/)) { alert('Bitte gib eine g\u00fcltige E-Mail-Adresse ein.'); $('#wg-rec-email').focus(); return; } const detailUrl = $('#wg-detail-url').val(); const baseUrl = window.location.origin + '/einladung-annehmen/'; // Baue Link const shareUrl = baseUrl + '?email=' + encodeURIComponent(email) + '&next=' + encodeURIComponent(detailUrl); // Zeige Link an $('#wg-share-link').val(shareUrl); $('#wg-link-share').slideDown(300); // Fokus auf Link-Input setTimeout(function() { $('#wg-share-link').select(); }, 350); }); // Link kopieren $(document).on('click', '.wg-btn-copy', function(e) { e.preventDefault(); const $input = $('#wg-share-link'); $input.select(); try { document.execCommand('copy'); // Visuelles Feedback const $btn = $(this); const originalText = $btn.text(); $btn.text('Kopiert!'); setTimeout(function() { $btn.html('📋 Kopieren'); }, 2000); showToast('Link kopiert!', 'success'); } catch (err) { showToast('Kopieren fehlgeschlagen. Bitte manuell kopieren.', 'error'); } }); // Modal fuer Bewertungen $(document).on('click', '.wg-btn-rate', function(e) { e.preventDefault(); const $btn = $(this); const media = $btn.data('media'); const id = $btn.data('id'); const title = $btn.data('title'); const currentRating = parseInt($btn.data('rating')) || 0; const adminPostUrl = wgData.ajaxUrl.replace('admin-ajax.php', 'admin-post.php'); // Generiere Sterne HTML let starsHtml = ''; for (let i = 1; i <= 10; i++) { const activeClass = i <= currentRating ? 'active' : ''; starsHtml += ''; } const ratingText = currentRating > 0 ? currentRating + ' / 10' : 'Klicke auf die Sterne'; // Delete-Button nur wenn bereits bewertet const deleteBtn = currentRating > 0 ? '' : ''; const modal = '
' + '
' + '
' + '' + '
' + '

"' + title + '" bewerten

' + '
' + starsHtml + '
' + '

' + ratingText + '

' + '
' + '
' + '' + '' + '' + '' + '' + '' + '
' + deleteBtn + '' + '
' + '
' + '
' + '
'; $('body').append(modal); // Hover-Effekt fuer Sterne $('#wg-rating-modal .wg-star').on('mouseenter', function() { const value = $(this).data('value'); $('#wg-rating-modal .wg-star').each(function() { $(this).toggleClass('hover', $(this).data('value') <= value); }); }).on('mouseleave', function() { $('#wg-rating-modal .wg-star').removeClass('hover'); }).on('click', function() { const value = $(this).data('value'); $('#wg-rating-input').val(value); $('#wg-rating-modal .wg-rating-value').text(value + ' / 10'); $('#wg-rating-modal .wg-star').removeClass('active'); $('#wg-rating-modal .wg-star').each(function() { if ($(this).data('value') <= value) { $(this).addClass('active'); } }); }); }); // Bewertung loeschen $(document).on('click', '.wg-btn-delete-rating', function(e) { e.preventDefault(); if (!confirm('Bewertung wirklich l\u00f6schen?')) { return; } const media = $(this).data('media'); const id = $(this).data('id'); const adminPostUrl = wgData.ajaxUrl.replace('admin-ajax.php', 'admin-post.php'); // Erstelle und sende Delete-Form const $form = $('
' + '' + '' + '' + '' + '
'); $('body').append($form); $form.submit(); }); // Modal schliessen (generisch) $(document).on('click', '.wg-modal-close, .wg-modal-overlay', function() { $(this).closest('.wg-modal').remove(); }); // ESC zum Schliessen $(document).on('keydown', function(e) { if (e.key === 'Escape') { $('.wg-modal').remove(); } }); // Trailer Play Button $(document).on('click', '.wg-trailer-play-btn', function() { const $container = $(this).closest('.wg-trailer-container'); const youtubeId = $container.data('youtube-id'); if (!youtubeId) return; const iframe = $('