// JavaScript to handle opening and closing of the login popup
const loginButton = document.getElementById('loginButton');
const loginPopup = document.getElementById('loginPopup');
const closePopupButton = document.getElementById('closePopupButton');
const usernameInput = document.getElementById('username');
const errorMessage = document.getElementById('errorMessage');

if (loginButton) { // Überprüfen, ob das Element vorhanden ist
    loginButton.addEventListener('click', function () {
        loginPopup.classList.remove('hidden');
        usernameInput.focus(); // Set focus to username field
    });
}

if (closePopupButton) { // Überprüfen, ob das Element vorhanden ist
    closePopupButton.addEventListener('click', function () {
        loginPopup.classList.add('hidden');
    });
}

window.addEventListener('click', function (event) {
    if (event.target === loginPopup) {
        loginPopup.classList.add('hidden');
    }
});

// Schließe Popup mit ESC
document.addEventListener('keydown', function (event) {
    if (event.key === "Escape" && !loginPopup.classList.contains('hidden')) {
        loginPopup.classList.add('hidden');
    }
});

// Zeige Fehlermeldung beim Login an
window.addEventListener('DOMContentLoaded', (event) => {
    const urlParams = new URLSearchParams(window.location.search);
    if (urlParams.has('error') && urlParams.get('error') === '1') {
        loginPopup.classList.remove('hidden');
        errorMessage.classList.remove('hidden');
    }
});