// Wait for the DOM to be fully loaded document.addEventListener('DOMContentLoaded', function() { // Dark mode toggle functionality const darkModeToggle = document.getElementById('darkModeToggle'); // Check for saved theme preference or use device preference const savedTheme = localStorage.getItem('theme'); const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches; // Set initial theme if (savedTheme === 'dark' || (!savedTheme && prefersDark)) { document.documentElement.setAttribute('data-theme', 'dark'); } // Toggle dark mode darkModeToggle.addEventListener('click', function() { const currentTheme = document.documentElement.getAttribute('data-theme'); const newTheme = currentTheme === 'dark' ? 'light' : 'dark'; document.documentElement.setAttribute('data-theme', newTheme); localStorage.setItem('theme', newTheme); }); // Mobile menu toggle const mobileMenuBtn = document.querySelector('.mobile-menu-btn'); const navLinks = document.querySelector('.nav-links'); if (mobileMenuBtn) { mobileMenuBtn.addEventListener('click', function() { // Create mobile menu if it doesn't exist if (!document.querySelector('.mobile-nav')) { const mobileNav = document.createElement('div'); mobileNav.className = 'mobile-nav'; // Clone the navigation links const navLinksClone = navLinks.cloneNode(true); // Get all links const links = navLinksClone.querySelectorAll('a'); // Convert to individual links for mobile links.forEach(link => { mobileNav.appendChild(link); }); // Add to the DOM document.querySelector('header').appendChild(mobileNav); } // Toggle the mobile menu const mobileNav = document.querySelector('.mobile-nav'); mobileNav.classList.toggle('active'); // Animate the hamburger icon const spans = this.querySelectorAll('span'); if (mobileNav.classList.contains('active')) { spans[0].style.transform = 'rotate(45deg) translate(5px, 5px)'; spans[1].style.opacity = '0'; spans[2].style.transform = 'rotate(-45deg) translate(7px, -6px)'; } else { spans[0].style.transform = 'none'; spans[1].style.opacity = '1'; spans[2].style.transform = 'none'; } }); } // Smooth scrolling for anchor links document.querySelectorAll('a[href^="#"]').forEach(anchor => { anchor.addEventListener('click', function(e) { e.preventDefault(); const targetId = this.getAttribute('href'); if (targetId === '#') return; const targetElement = document.querySelector(targetId); if (targetElement) { // Close mobile menu if open const mobileNav = document.querySelector('.mobile-nav'); if (mobileNav && mobileNav.classList.contains('active')) { mobileMenuBtn.click(); } // Scroll to the target element window.scrollTo({ top: targetElement.offsetTop - 80, behavior: 'smooth' }); } }); }); // Form validation const contactForm = document.getElementById('contactForm'); if (contactForm) { contactForm.addEventListener('submit', function(e) { e.preventDefault(); // Get form values const name = document.getElementById('name').value.trim(); const email = document.getElementById('email').value.trim(); const message = document.getElementById('message').value.trim(); // Simple validation if (name === '') { showError('name', 'Please enter your name'); return; } if (email === '') { showError('email', 'Please enter your email'); return; } if (!isValidEmail(email)) { showError('email', 'Please enter a valid email'); return; } if (message === '') { showError('message', 'Please enter your message'); return; } // If all validations pass, show success message contactForm.innerHTML = `
Your message has been sent successfully. We'll get back to you soon.