// Cart functionality let cart = []; let cartTotal = 0; // Initialize document.addEventListener('DOMContentLoaded', () => { initializeFilters(); initializeNavbar(); }); // Filter functionality function initializeFilters() { const filterBtns = document.querySelectorAll('.filter-btn'); const products = document.querySelectorAll('.product-card'); filterBtns.forEach(btn => { btn.addEventListener('click', () => { // Update active state filterBtns.forEach(b => { b.classList.remove('bg-primary', 'text-white', 'border-primary'); b.classList.add('text-gray-700', 'border-gray-300'); }); btn.classList.remove('text-gray-700', 'border-gray-300'); btn.classList.add('bg-primary', 'text-white', 'border-primary'); // Filter products const filter = btn.dataset.filter; products.forEach(product => { if (filter === 'all' || product.dataset.category === filter) { product.style.display = 'block'; product.classList.add('fade-in'); } else { product.style.display = 'none'; } }); }); }); } // Navbar scroll effect function initializeNavbar() { const navbar = document.getElementById('navbar'); let lastScroll = 0; window.addEventListener('scroll', () => { const currentScroll = window.pageYOffset; if (currentScroll > 100) { navbar.classList.add('shadow-md'); } else { navbar.classList.remove('shadow-md'); } lastScroll = currentScroll; }); } // Mobile menu toggle function toggleMobileMenu() { const menu = document.getElementById('mobile-menu'); menu.classList.toggle('hidden'); } // Cart toggle function toggleCart() { const sidebar = document.getElementById('cart-sidebar'); const overlay = document.getElementById('cart-overlay'); if (sidebar.classList.contains('cart-closed')) { sidebar.classList.remove('cart-closed'); sidebar.classList.add('cart-open'); overlay.classList.remove('hidden'); document.body.style.overflow = 'hidden'; } else { sidebar.classList.remove('cart-open'); sidebar.classList.add('cart-closed'); overlay.classList.add('hidden'); document.body.style.overflow = 'auto'; } } // Add to cart function addToCart(id, name, price, image) { const existingItem = cart.find(item => item.id === id); if (existingItem) { existingItem.quantity += 1; } else { cart.push({ id: id, name: name, price: price, image: image, quantity: 1 }); } updateCart(); showToast(`${name} added to cart!`); // Open cart automatically on first add if (cart.length === 1 && cart[0].quantity === 1) { setTimeout(() => { const sidebar = document.getElementById('cart-sidebar'); if (sidebar.classList.contains('cart-closed')) { toggleCart(); } }, 500); } } // Remove from cart function removeFromCart(id) { cart = cart.filter(item => item.id !== id); updateCart(); } // Update quantity function updateQuantity(id, change) { const item = cart.find(item => item.id === id); if (item) { item.quantity += change; if (item.quantity <= 0) { removeFromCart(id); } else { updateCart(); } } } // Update cart UI function updateCart() { const cartItems = document.getElementById('cart-items'); const cartCount = document.getElementById('cart-count'); const cartTotalEl = document.getElementById('cart-total'); // Update count const totalItems = cart.reduce((sum, item) => sum + item.quantity, 0); if (totalItems > 0) { cartCount.textContent = totalItems; cartCount.classList.remove('hidden'); } else { cartCount.classList.add('hidden'); } // Calculate total cartTotal = cart.reduce((sum, item) => sum + (item.price * item.quantity), 0); cartTotalEl.textContent = `$${cartTotal.toFixed(2)}`; // Render items if (cart.length === 0) { cartItems.innerHTML = `

Your cart is empty

`; } else { cartItems.innerHTML = cart.map(item => `
${item.name}

${item.name}

$${item.price.toFixed(2)}

${item.quantity}
`).join(''); } // Reinitialize Lucide icons lucide.createIcons(); } // Checkout function checkout() { if (cart.length === 0) { showToast('Your cart is empty!', 'error'); return; } showToast('Proceeding to checkout...', 'success'); setTimeout(() => { alert('Thank you for your order! This is a demo checkout. Total: $' + cartTotal.toFixed(2)); cart = []; updateCart(); toggleCart(); }, 1000); } // Toast notification function showToast(message, type = 'success') { const container = document.getElementById('toast-container'); const toast = document.createElement('div'); const bgColor = type === 'error' ? 'bg-red-500' : 'bg-green-500'; const icon = type === 'error' ? 'alert-circle' : 'check-circle'; toast.className = `toast ${bgColor} text-white px-6 py-3 rounded-full shadow-lg flex items-center space-x-2 min-w-[250px]`; toast.innerHTML = ` ${message} `; container.appendChild(toast); lucide.createIcons(); setTimeout(() => { toast.style.opacity = '0'; toast.style.transform = 'translateX(100%)'; toast.style.transition = 'all 0.3s ease'; setTimeout(() => toast.remove(), 300); }, 3000); } // Newsletter function handleNewsletter(e) { e.preventDefault(); const email = e.target.querySelector('input[type="email"]').value; showToast('Thank you for subscribing! Check your email for 15% off.'); e.target.reset(); } // Smooth scroll for anchor links document.querySelectorAll('a[href^="#"]').forEach(anchor => { anchor.addEventListener('click', function (e) { e.preventDefault(); const target = document.querySelector(this.getAttribute('href')); if (target) { target.scrollIntoView({ behavior: 'smooth', block: 'start' }); // Close mobile menu if open document.getElementById('mobile-menu').classList.add('hidden'); } }); });