// Shared JavaScript across all pages console.log('قالی شهاب - App loaded'); // Initialize Feather Icons document.addEventListener('DOMContentLoaded', function() { if (typeof feather !== 'undefined') { feather.replace(); } }); // Mobile menu functionality function toggleMobileMenu() { const mobileMenu = document.getElementById('mobile-menu'); if (mobileMenu) { mobileMenu.classList.toggle('hidden'); } } // Product filtering and search function filterProducts(category = 'all') { const products = document.querySelectorAll('.product-item'); products.forEach(product => { if (category === 'all' || product.dataset.category === category) { product.style.display = 'block'; } else { product.style.display = 'none'; } }); } // Search functionality function searchProducts(query) { const products = document.querySelectorAll('.product-item'); products.forEach(product => { const title = product.querySelector('h3').textContent.toLowerCase(); const description = product.querySelector('p').textContent.toLowerCase(); const searchTerm = query.toLowerCase(); if (title.includes(searchTerm) || description.includes(searchTerm)) { product.style.display = 'block'; } else { product.style.display = 'none'; } }); } // Add to cart functionality function addToCart(productId, productName, price) { let cart = JSON.parse(localStorage.getItem('cart')) || []; const existingItem = cart.find(item => item.id === productId); if (existingItem) { existingItem.quantity += 1; } else { cart.push({ id: productId, name: productName, price: price, quantity: 1 }); } localStorage.setItem('cart', JSON.stringify(cart)); updateCartCount(); showNotification('محصول به سبد خرید اضافه شد'); } // Update cart count in navbar function updateCartCount() { const cart = JSON.parse(localStorage.getItem('cart')) || []; const totalItems = cart.reduce((sum, item) => sum + item.quantity, 0); const cartCountElements = document.querySelectorAll('.cart-count'); cartCountElements.forEach(element => { element.textContent = totalItems; if (totalItems > 0) { element.classList.remove('hidden'); } else { element.classList.add('hidden'); } }); } // Show notification function showNotification(message) { const notification = document.createElement('div'); notification.className = 'fixed top-4 left-4 bg-green-500 text-white px-6 py-3 rounded-lg shadow-lg z-50'; notification.textContent = message; document.body.appendChild(notification); setTimeout(() => { notification.remove(); }, 3000); } // Initialize cart count on page load document.addEventListener('DOMContentLoaded', function() { updateCartCount(); }); // Form validation function validateContactForm() { const name = document.getElementById('name').value; const email = document.getElementById('email').value; const message = document.getElementById('message').value; if (!name || !email || !message) { showNotification('لطفاً تمام فیلدهای ضروری را پر کنید'); return false; } if (!isValidEmail(email)) { showNotification('لطفاً یک ایمیل معتبر وارد کنید'); return false; } return true; } function isValidEmail(email) { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); } // Image lazy loading document.addEventListener('DOMContentLoaded', function() { const lazyImages = document.querySelectorAll('img[data-src]'); const imageObserver = new IntersectionObserver((entries, observer) => { entries.forEach(entry => { if (entry.isIntersecting) { const img = entry.target; img.src = img.dataset.src; img.removeAttribute('data-src'); observer.unobserve(img); } }); }); lazyImages.forEach(img => imageObserver.observe(img)); });