function getCookie(name) {
    let cookieValue = null;
    if (document.cookie && document.cookie !== '') {
        const cookies = document.cookie.split(';');
        for (let i = 0; i < cookies.length; i++) {
            const cookie = cookies[i].trim();
            if (cookie.substring(0, name.length + 1) === (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}

// Scroll-responsive navigation functionality
document.addEventListener('DOMContentLoaded', function() {
    const nav = document.getElementById('main-nav');
    if (!nav) return;

    let lastScrollTop = 0;
    let scrollThreshold = 100; // Hide nav after scrolling down 100px
    let isNavVisible = true;

    function handleScroll() {
        const currentScrollTop = window.pageYOffset || document.documentElement.scrollTop;

        // Avoid negative scroll values on mobile
        if (currentScrollTop < 0) return;

        // Don't hide nav if we're at the top of the page
        if (currentScrollTop < scrollThreshold) {
            showNav();
            lastScrollTop = currentScrollTop;
            return;
        }

        // Determine scroll direction
        if (currentScrollTop > lastScrollTop && isNavVisible) {
            // Scrolling down - hide nav
            hideNav();
        } else if (currentScrollTop < lastScrollTop && !isNavVisible) {
            // Scrolling up - show nav
            showNav();
        }

        lastScrollTop = currentScrollTop;
    }

    function hideNav() {
        nav.classList.remove('nav-visible');
        nav.classList.add('nav-hidden');
        isNavVisible = false;
    }

    function showNav() {
        nav.classList.remove('nav-hidden');
        nav.classList.add('nav-visible');
        isNavVisible = true;
    }

    // Throttle scroll events for better performance
    let scrollTimeout;
    window.addEventListener('scroll', function() {
        if (scrollTimeout) {
            clearTimeout(scrollTimeout);
        }
        scrollTimeout = setTimeout(handleScroll, 10);
    });
});
