document.addEventListener('DOMContentLoaded', function() {
    // Delay showing the popup by a few seconds (e.g., 5 seconds)
    setTimeout(function() {
        // Check if the popup should be shown based on the cookie
        if (!getCookie('spn_popupcookie')) {
            // Show the modal (assuming you have a function to show the modal)
            showModal('spnModalSection');
        }
    }, 5000); // 5000 milliseconds (5 seconds)

    // Add click event for the close button
    if( jQuery('#spnModalSection').length ){
        document.getElementById('spnClosePopup').addEventListener('click', function() {
            // Hide the modal (assuming you have a function to hide the modal)
            hideModal('spnModalSection');
    
            var cookieExpired = simppono_ajax_object.simppono_setcookieexpire;
            if(cookieExpired != 'never'){
                // Set a cookie to remember that the popup has been closed
                setCookie('spn_popupcookie', 'true', cookieExpired); // Cookie will expire in 1 day
            }else{
                // Set a cookie to remember that the popup has been closed
                setCookie('spn_popupcookie', 'true', 365); // Cookie will expire in 1 day
            }
        });
    }
    

    // Function to set a cookie
    function setCookie(name, value, days) {
        var expires = '';
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            expires = '; expires=' + date.toUTCString();
        }
        document.cookie = name + '=' + value + expires + '; path=/';
    }

    // Function to get a cookie
    function getCookie(name) {
        var nameEQ = name + '=';
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = cookies[i].trim(); // Trim whitespace
            if (cookie.indexOf(nameEQ) === 0) {
                return cookie.substring(nameEQ.length, cookie.length);
            }
        }
        return null;
    }

    // Function to show the modal (you need to implement this)
    function showModal(modalId) {
        var modal = document.getElementById(modalId);
        if (modal) {
            modal.style.display = 'block'; // Show the modal (you may need to adjust this based on your modal implementation)
        }
    }

    // Function to hide the modal (you need to implement this)
    function hideModal(modalId) {
        var modal = document.getElementById(modalId);
        if (modal) {
            modal.style.display = 'none'; // Hide the modal (you may need to adjust this based on your modal implementation)
        }
    }
});