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

    const chooseLinks = document.querySelectorAll('.submenu-link');
    console.log('chooseLinks: ', chooseLinks);
    
    // Check if submenu links exist before proceeding
    if (chooseLinks.length > 0) {
        chooseLinks.forEach(function(link) {
            link.addEventListener('click', function(event) {
                event.preventDefault(); 
                var region = link.getAttribute('data-region');
                document.cookie = 'userSitePreference=' + encodeURIComponent(region) + '; path=/';
                window.location.href = link.href;
            });
        });
    }
    
    // Check if the element with ID 'stay-global' exists
    if (document.getElementById('stay-global')) {
        document.getElementById('stay-global').addEventListener('click', function() {
            document.cookie = "userSitePreference=US; path=/";
            location.reload();
        });
    }
    
    // Check if the element with ID 'visit-india' exists
    if (document.getElementById('visit-india')) {
        document.getElementById('visit-india').addEventListener('click', function() {
            document.cookie = "userSitePreference=IN; path=/";
            location.reload();
        });
    }
    
  
 // Check if the popup has been closed previously
 if (!localStorage.getItem('popupClosed')) {
    const locationPopup = document.getElementById('location-popup');
    if (locationPopup) {
        locationPopup.style.display = 'block';

        // Define buttons and popup
        const closeBtn = document.getElementById('close-popup');
        const inBtn = document.getElementById('visit-india');
        const usBtn = document.getElementById('stay-global');

        const buttons = [closeBtn, inBtn, usBtn];
        buttons.forEach(button => {
            if (button) {
                button.addEventListener('click', () => {
                    locationPopup.style.display = 'none';
                    localStorage.setItem('popupClosed', 'true');
                });
            }
        });
    }
}




     // Region switcher dropdown functionality

     const dropdownItems = document.querySelectorAll(".new-dropdown-item");

     // Add event listener for each dropdown item
     dropdownItems.forEach(function (dropdownItem) {
         dropdownItem.addEventListener("click", function (event) {
             event.preventDefault(); 
             dropdownItem.classList.toggle("active");
         });
     });
 
     // Close dropdown when clicking outside
     document.addEventListener("click", function (event) {
         dropdownItems.forEach(function (dropdownItem) {
             if (!dropdownItem.contains(event.target)) {
                 dropdownItem.classList.remove("active");
             }
         });
     });
 
});

