(function() { 'use strict'; // Check for reduced motion preference const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches; // Navigation const nav = document.querySelector('.main-nav'); const navToggle = document.querySelector('.nav-toggle'); const navLinks = document.querySelector('.nav-links'); // Scroll behavior for nav let lastScrollY = 0; let ticking = false; function updateNav() { const scrollY = window.scrollY; if (scrollY > 50) { nav.classList.add('scrolled'); } else { nav.classList.remove('scrolled'); } lastScrollY = scrollY; ticking = false; } window.addEventListener('scroll', () => { if (!ticking) { requestAnimationFrame(updateNav); ticking = true; } }, { passive: true }); // Mobile nav toggle navToggle.addEventListener('click', () => { const isOpen = navToggle.getAttribute('aria-expanded') === 'true'; navToggle.setAttribute('aria-expanded', !isOpen); navToggle.setAttribute('aria-label', isOpen ? 'פתח תפריט ניווט' : 'סגור תפריט ניווט'); navLinks.classList.toggle('open'); document.body.style.overflow = isOpen ? '' : 'hidden'; }); // Close mobile nav on link click navLinks.querySelectorAll('a').forEach(link => { link.addEventListener('click', () => { navToggle.setAttribute('aria-expanded', 'false'); navToggle.setAttribute('aria-label', 'פתח תפריט ניווט'); navLinks.classList.remove('open'); document.body.style.overflow = ''; }); }); // Smooth scroll for anchor links document.querySelectorAll('a[href^="#"]').forEach(anchor => { anchor.addEventListener('click', function(e) { const targetId = this.getAttribute('href'); if (targetId === '#') return; const target = document.querySelector(targetId); if (target) { e.preventDefault(); target.scrollIntoView({ behavior: prefersReducedMotion ? 'auto' : 'smooth', block: 'start' }); } }); }); // Reveal on scroll const revealElements = document.querySelectorAll('.reveal-text, .reveal-up'); if (!prefersReducedMotion && revealElements.length > 0) { const revealObserver = new IntersectionObserver((entries) => { entries.forEach((entry) => { if (entry.isIntersecting) { entry.target.classList.add('visible'); revealObserver.unobserve(entry.target); } }); }, { threshold: 0.1, rootMargin: '0px 0px -40px 0px' }); revealElements.forEach(el => revealObserver.observe(el)); } else { // Show all elements immediately if reduced motion is preferred revealElements.forEach(el => { el.classList.add('visible'); }); } // Active section highlighting const sections = document.querySelectorAll('section[id]'); const navItems = document.querySelectorAll('.nav-links a:not(.nav-cta)'); if (sections.length > 0 && navItems.length > 0) { const sectionObserver = new IntersectionObserver((entries) => { entries.forEach(entry => { if (entry.isIntersecting) { const id = entry.target.getAttribute('id'); navItems.forEach(item => { item.classList.remove('active'); if (item.getAttribute('href') === `#${id}`) { item.classList.add('active'); } }); } }); }, { threshold: 0.2, rootMargin: '-80px 0px -50% 0px' }); sections.forEach(section => sectionObserver.observe(section)); } // Form handling const form = document.querySelector('.contact-form'); if (form) { form.addEventListener('submit', function(e) { e.preventDefault(); const btn = form.querySelector('.submit-btn'); const originalText = btn.querySelector('span').textContent; const requiredFields = form.querySelectorAll('[required]'); let isValid = true; // Basic validation requiredFields.forEach(field => { if (!field.value.trim()) { isValid = false; field.style.borderColor = 'var(--color-accent)'; } else { field.style.borderColor = ''; } }); if (!isValid) { return; } btn.querySelector('span').textContent = 'שולח...'; btn.disabled = true; // Simulate submission (replace with actual form handling) setTimeout(() => { btn.querySelector('span').textContent = 'נשלח בהצלחה!'; btn.style.background = '#2e7d32'; setTimeout(() => { btn.querySelector('span').textContent = originalText; btn.style.background = ''; btn.disabled = false; form.reset(); }, 2500); }, 1200); }); // Clear error styling on input form.querySelectorAll('input, textarea, select').forEach(field => { field.addEventListener('input', () => { field.style.borderColor = ''; }); }); } // Keyboard navigation enhancement document.addEventListener('keydown', (e) => { if (e.key === 'Escape' && navLinks.classList.contains('open')) { navToggle.setAttribute('aria-expanded', 'false'); navToggle.setAttribute('aria-label', 'פתח תפריט ניווט'); navLinks.classList.remove('open'); document.body.style.overflow = ''; navToggle.focus(); } }); // Trap focus in mobile menu when open navLinks.addEventListener('keydown', (e) => { if (!navLinks.classList.contains('open')) return; const focusableElements = navLinks.querySelectorAll('a'); const firstElement = focusableElements[0]; const lastElement = focusableElements[focusableElements.length - 1]; if (e.key === 'Tab') { if (e.shiftKey && document.activeElement === firstElement) { e.preventDefault(); navToggle.focus(); } else if (!e.shiftKey && document.activeElement === lastElement) { e.preventDefault(); navToggle.focus(); } } }); })();