function setCookie(name, value, minutes) {
    let expires = "";
    if (minutes) {
        const date = new Date();
        date.setTime(date.getTime() + (minutes * 60 * 1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = `${name}=${value || ""}${expires}; path=/`;
}

function getCookie(name) {
    const nameEQ = `${name}=`;
    const ca = document.cookie.split(';');
    for (let i = 0; i < ca.length; i++) {
        let c = ca[i].trim();
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length);
    }
    return null;
}

jQuery(document).ready(function($) {
    // Popup and Scrolling Announcement Common Actions
    const checkAndShowAnnouncements = () => {
        if (!getCookie('popup-announcement-seen')) {
            $('.popup-overlay, .popup-announcement').show();
        }
        if (getCookie('scrolling-announcement-hidden')) {
            $('.scrolling-announcement').hide();
        } else {
            $('.scrolling-announcement').wrapAll('<div class="scrolling-container"></div>');
            $('.scrolling-container').css({
                position: 'fixed',
                bottom: 0,
                width: '100%',
                backgroundColor: 'rgba(0, 0, 0, 0.7)',
                color: '#fff',
                textAlign: 'center',
				'z-index': 1, // Corrected by adding quotes around z-index
            });
        }
    };

    checkAndShowAnnouncements();

    // Popup Close Handler
    $('.close-popup').on('click', function() {
        $(this).closest('.popup-overlay').hide();
        setCookie('popup-announcement-seen', 'true', 30);
    });

    // Outside Click for Popup Announcement
    $(document).on('click', function(e) {
        if ($(e.target).is('.popup-overlay')) {
            $('.popup-overlay, .popup-announcement').hide();
        }
    });

    // Scrolling Announcement Handlers
    $('.close-scrolling').click(function() {
        $('.scrolling-announcement').hide();
        $('.show-scrolling').show();
    });

    $('.show-scrolling').click(function() {
        $('.scrolling-announcement').show();
        $(this).hide();
    });

    // Dynamic Duration for Scrolling Announcement
    $('.scrolling-announcement.content-area div').each(function() {
        const contentWidth = $(this).width();
        const viewportWidth = $(this).parent().width();
        const duration = (contentWidth / viewportWidth) * 10; // Base duration calculation
        $(this).css('animation-duration', duration + 's');
    });
});
