document.addEventListener('DOMContentLoaded', function () { // 1. Vision / Mission Tabs Switcher const tabButtons = document.querySelectorAll('[data-tab]'); const tabPanels = document.querySelectorAll('[data-panel]'); function activateTab(button) { const target = button.getAttribute('data-tab'); // Update tab buttons tabButtons.forEach(btn => { const isActive = btn === button; btn.classList.toggle('is-active', isActive); btn.setAttribute('aria-selected', String(isActive)); btn.setAttribute('tabindex', isActive ? '0' : '-1'); }); // Show the selected panel and hide the others tabPanels.forEach(panel => { const isActive = panel.getAttribute('data-panel') === target; panel.classList.toggle('is-active', isActive); panel.hidden = !isActive; }); } // Attach click events to all tabs tabButtons.forEach(button => { button.addEventListener('click', function () { activateTab(button); }); }); // Activate the initially selected tab const initialTab = document.querySelector('[data-tab][aria-selected="true"]') || tabButtons[0]; if (initialTab) { activateTab(initialTab); } // 2. Dynamic Animated Counter Engine const statNumbers = document.querySelectorAll( '.ussia-stat-number' ); // Format numbers with commas, e.g. 1000 -> 1,000 const formatNumber = number => { return Math.floor(number).toLocaleString(); }; // Animate an individual counter function animateCounter(counter) { // Prevent the same counter from animating twice if (counter.dataset.animated === 'true') { return; } counter.dataset.animated = 'true'; const target = Number(counter.getAttribute('data-target')); // Ignore invalid or negative target values if (!Number.isFinite(target) || target < 0) { return; } const duration = 2000; // Animation duration in milliseconds const startTime = performance.now(); // Store the original content for the final display const suffix = counter.dataset.suffix || '+'; counter.textContent = '0'; // Ease-out animation for a smooth finish function updateCounter(currentTime) { const elapsed = currentTime - startTime; const progress = Math.min(elapsed / duration, 1); // Ease-out cubic effect const easedProgress = 1 - Math.pow(1 - progress, 3); const currentValue = target * easedProgress; counter.textContent = formatNumber(currentValue) + suffix; // Continue until the target is reached if (progress < 1) { requestAnimationFrame(updateCounter); } else { // Display the exact final target counter.textContent = formatNumber(target) + suffix; } } requestAnimationFrame(updateCounter); } // 3. Start counters when they become visible if ('IntersectionObserver' in window) { const observer = new IntersectionObserver( function (entries, observer) { entries.forEach(entry => { if (entry.isIntersecting) { animateCounter(entry.target); // Stop observing after animation starts observer.unobserve(entry.target); } }); }, { threshold: 0.3 } ); statNumbers.forEach(counter => { observer.observe(counter); }); } else { // Fallback for older browsers statNumbers.forEach(counter => { animateCounter(counter); }); } });