// Make sure we show the right year for copyright
document.getElementById("year").textContent = new Date().getFullYear();

// Hamburger Helper
function toggleMobileMenu() {
    const menu = document.getElementById('mobile-menu');
    const button = document.querySelector('[aria-controls="mobile-menu"]');
    const isExpanded = button.getAttribute('aria-expanded') === 'true';

    if (isExpanded) {
        menu.style.display = 'none';
        button.setAttribute('aria-expanded', 'false');
    } else {
        menu.style.display = 'block';
        button.setAttribute('aria-expanded', 'true');
    }
}

// Hide/Show for the submenus that are only visible on mobile
function toggleMobileSubMenu(subMenuId) {
    const subMenu = document.getElementById(subMenuId);
    const isExpanded = subMenu.style.display === 'block';
    subMenu.style.display = isExpanded ? 'none' : 'block';
}
  
  // Close mobile menu when clicking outside
document.addEventListener("click", function(event) {
    const menu = document.getElementById("mobile-menu");
    const button = event.target.closest("button[aria-label=\"Open navigation menu\"]");

    if (!menu.contains(event.target) && !button) {
        menu.style.display = 'none';
    }
});

// Menu highlighting functionality to show users what is currently selected on navigation
document.addEventListener('DOMContentLoaded', () => {
    const currentPath = window.location.pathname;

    // Function to check if an element is visible
    const isVisible = (elem) => !!(elem.offsetWidth || elem.offsetHeight || elem.getClientRects().length);

    // Handle desktop menu highlighting
    const desktopNavLinks = document.querySelectorAll('nav a');
    desktopNavLinks.forEach(link => {
        if (isVisible(link)) {
            const linkPath = link.getAttribute('href');
            if (currentPath.startsWith(linkPath) && linkPath !== '/') {
                link.classList.remove('text-slate-300');
                link.classList.add('text-green-400', 'font-bold');

                // If it's a sub-menu item, highlight the parent button
                const parentGroup = link.closest('.relative.group');
                if (parentGroup) {
                    const parentButton = parentGroup.querySelector('button');
                    parentButton.classList.remove('text-slate-300');
                    parentButton.classList.add('text-green-400', 'font-bold');
                }
            }
        }
    });

    // Handle mobile menu highlighting and accordion
    const mobileNavLinks = document.querySelectorAll('#mobile-menu a');
    mobileNavLinks.forEach(link => {
        const linkPath = link.getAttribute('href');
        if (currentPath.startsWith(linkPath) && linkPath !== '/') {
            link.classList.remove('text-slate-300');
            link.classList.add('text-green-400', 'font-bold');

            // If it's a sub-menu item, open the accordion
            const parentMenu = link.closest('.pl-4');
            if (parentMenu) {
                parentMenu.style.display = 'block';
                const parentButton = parentMenu.previousElementSibling;
                parentButton.classList.remove('text-slate-300');
                parentButton.classList.add('text-green-400', 'font-bold');
            }
        }
    });
});