// Custom JavaScript for Supreme Cycle Store $(document).ready(function() { // Initialize page initializePage(); // Shopping cart functionality let cart = JSON.parse(localStorage.getItem('cart')) || []; updateCartCounter(); // Add to cart functionality $(document).on('click', '.add-to-cart', function(e) { e.preventDefault(); const productId = $(this).data('id'); const productName = $(this).closest('.product-card, .card-hover').find('h3').text(); const productPrice = $(this).closest('.product-card, .card-hover').find('.price, .font-bold').text(); addToCart(productId, productName, productPrice); showNotification('Product added to cart!', 'success'); }); // Wishlist functionality let wishlist = JSON.parse(localStorage.getItem('wishlist')) || []; $(document).on('click', '.add-to-wishlist', function(e) { e.preventDefault(); const productId = $(this).data('id'); toggleWishlist(productId, $(this)); }); // Search functionality $('#search-btn').click(function() { const searchTerm = $('#search-input').val(); if (searchTerm) { window.location.href = `/search?q=${encodeURIComponent(searchTerm)}`; } }); $('#search-input').keypress(function(e) { if (e.which === 13) { $('#search-btn').click(); } }); // Lazy loading for images initializeLazyLoading(); // Smooth scroll to top $(window).scroll(function() { if ($(this).scrollTop() > 300) { showScrollToTop(); } else { hideScrollToTop(); } }); }); function initializePage() { // Add scroll to top button if (!$('#scroll-to-top').length) { $('body').append(` `); $('#scroll-to-top').click(function() { $('html, body').animate({scrollTop: 0}, 600); }); } // Initialize tooltips $('[data-tooltip]').each(function() { $(this).attr('title', $(this).data('tooltip')); }); } function addToCart(productId, productName, productPrice) { const existingItem = cart.find(item => item.id === productId); if (existingItem) { existingItem.quantity += 1; } else { cart.push({ id: productId, name: productName, price: productPrice, quantity: 1 }); } localStorage.setItem('cart', JSON.stringify(cart)); updateCartCounter(); } function updateCartCounter() { const totalItems = cart.reduce((sum, item) => sum + item.quantity, 0); $('.cart-counter').text(totalItems); } function toggleWishlist(productId, button) { const index = wishlist.indexOf(productId); if (index > -1) { wishlist.splice(index, 1); button.find('i').removeClass('fas').addClass('far'); button.removeClass('text-red-500').addClass('text-gray-500'); showNotification('Removed from wishlist', 'info'); } else { wishlist.push(productId); button.find('i').removeClass('far').addClass('fas'); button.removeClass('text-gray-500').addClass('text-red-500'); showNotification('Added to wishlist!', 'success'); } localStorage.setItem('wishlist', JSON.stringify(wishlist)); } function showNotification(message, type = 'info') { const colors = { success: 'bg-green-500', error: 'bg-red-500', warning: 'bg-yellow-500', info: 'bg-blue-500' }; const notification = $(`