/**
 * Golpo Features JS (Dark Mode, Back to Top)
 */

document.addEventListener('DOMContentLoaded', function () {

    // Back To Top
    var backToTopBtn = document.getElementById("golpo-back-to-top");

    if (backToTopBtn) {
        // Initial check
        toggleBackToTop();

        // Scroll listener
        window.addEventListener('scroll', toggleBackToTop);

        backToTopBtn.addEventListener('click', function (e) {
            e.preventDefault();
            window.scrollTo({
                top: 0,
                behavior: "smooth"
            });
        });
    }

    function toggleBackToTop() {
        if (window.pageYOffset > 300 || document.documentElement.scrollTop > 300) {
            backToTopBtn.style.display = "block";
        } else {
            backToTopBtn.style.display = "none";
        }
    }

    // Dark Mode Toggle
    var darkModeToggle = document.getElementById("golpo-dark-mode-toggle");
    var body = document.body;

    // Check Local Storage on load
    if (localStorage.getItem('golpo_dark_mode') === 'enabled') {
        body.classList.add('golpo-dark-mode');
    }

    if (darkModeToggle) {
        darkModeToggle.addEventListener('click', function (e) {
            e.preventDefault();
            body.classList.toggle('golpo-dark-mode');

            if (body.classList.contains('golpo-dark-mode')) {
                localStorage.setItem('golpo_dark_mode', 'enabled');
            } else {
                localStorage.setItem('golpo_dark_mode', 'disabled');
            }
        });
    }
});
