// Auto year and last updated
document.addEventListener("DOMContentLoaded", () => {
    const yearSpan = document.getElementById("footer-year");
    const updatedSpan = document.getElementById("last-updated");

    const now = new Date();

    // Auto year
    if (yearSpan) {
        yearSpan.textContent = now.getFullYear();
    }

    // Auto last updated (e.g., May 7, 2026)
    if (updatedSpan) {
        const options = { year: "numeric", month: "long", day: "numeric" };
        updatedSpan.textContent = now.toLocaleDateString(undefined, options);
    }
});



// Scroll animations 

document.addEventListener("DOMContentLoaded", () => {
    const observer = new IntersectionObserver(
        entries => {
            entries.forEach(entry => {
                if (entry.isIntersecting) {
                    entry.target.classList.add("visible");
                }
            });
        },
        { threshold: 0.2 }
    );

    document.querySelectorAll(".fade-in, .stagger").forEach(el => {
        observer.observe(el);
    });
});


// Parallax

document.addEventListener("mousemove", (e) => {
    const bg = document.querySelector(".addons-hero .parallax-bg");
    if (!bg) return;

    const x = (e.clientX / window.innerWidth - 0.5) * 20;
    const y = (e.clientY / window.innerHeight - 0.5) * 20;

    bg.style.transform = `translate(${x}px, ${y}px)`;
});


// Language system 

document.addEventListener("DOMContentLoaded", () => {
    const lang = localStorage.getItem("lang") || "en";

    function applyTranslations(langCode) {
        fetch("lang.json")
            .then(res => res.json())
            .then(data => {
                const block = data[langCode];
                if (!block) return;

                document.querySelectorAll("[data-i18n]").forEach(el => {
                    const key = el.getAttribute("data-i18n");
                    if (block[key]) {
                        el.textContent = block[key];
                    }
                });
            });
    }

    // Apply on load
    applyTranslations(lang);

    // Language buttons
    document.querySelectorAll(".lang").forEach(btn => {
        btn.addEventListener("click", () => {
            const selected = btn.getAttribute("data-lang");
            localStorage.setItem("lang", selected);

            // Update active class
            document.querySelectorAll(".lang").forEach(b => b.classList.remove("active"));
            btn.classList.add("active");

            applyTranslations(selected);
        });
    });
});

        // Mobile footer sitemap toggle
    document.addEventListener("DOMContentLoaded", () => {
        const toggle = document.getElementById("sitemap-trigger");
        const links = document.getElementById("sitemap-dropdown-menu");

        // Toggle menu on click/top
        toggle.addEventListener("click", (event) => {
            event.stopPropagation();   // prevents immediate closing from othe document listener below
            links.classList.toggle ("is-active");
        });

        // Close menu automatically if clicking anywhere outise of it
        document.addEventListener("click", (event) => {
            if (links.contains(event.target) && event.target !==toggle) {
                links.classList.remove("is-active");
            }
        });
    });