File size: 6,107 Bytes
351839d
135fe83
 
 
 
 
351839d
135fe83
 
 
 
 
 
351839d
135fe83
 
 
 
 
351839d
135fe83
 
 
 
 
 
 
 
 
 
 
 
 
 
351839d
135fe83
 
 
 
 
 
351839d
135fe83
 
 
 
 
351839d
135fe83
 
 
 
351839d
135fe83
 
 
 
 
 
 
 
351839d
135fe83
 
 
 
 
 
 
 
 
 
 
351839d
135fe83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
351839d
135fe83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
351839d
 
135fe83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
351839d
 
135fe83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

// Global state
let currentSlide = 0;
let cartCount = 0;
let selectedColor = 'Midnight Black';
let selectedSize = 'Medium';

// Initialize
document.addEventListener('DOMContentLoaded', () => {
    // Feather icons
    if (typeof feather !== 'undefined') {
        feather.replace();
    }

    // Carousel
    const track = document.getElementById('carousel-track');
    if (track) {
        const slides = document.querySelectorAll('.carousel-slide');
        const totalSlides = slides.length;

        function updateCarousel() {
            track.style.transform = `translateX(-${currentSlide * 100}%)`;
        
            // Update dots
            document.querySelectorAll('.carousel-dot').forEach((dot, index) => {
                if (index === currentSlide) {
                    dot.classList.add('bg-white');
                    dot.classList.remove('bg-white/50');
            } else {
                dot.classList.remove('bg-white');
                dot.classList.add('bg-white/50');
            }
        });
        }

        function nextSlide() {
            const slides = document.querySelectorAll('.carousel-slide');
            const totalSlides = slides.length;
            currentSlide = (currentSlide + 1) % totalSlides;
            updateCarousel();
        }

        function prevSlide() {
            const slides = document.querySelectorAll('.carousel-slide');
            const totalSlides = slides.length;
            currentSlide = (currentSlide - 1 + totalSlides) % totalSlides;
            updateCarousel();
        }

        function goToSlide(index) {
            currentSlide = index;
            updateCarousel();
        }

        // Auto-play
        setInterval(nextSlide, 5000);

        // Expose functions globally
        window.nextSlide = nextSlide;
        window.prevSlide = prevSlide;
        window.goToSlide = goToSlide;
    }

    // Color selector
    window.selectColor = (color, className) => {
        selectedColor = color;
        const el = document.getElementById('selected-color');
        if (el) el.textContent = color;
        
            // Update border colors
            document.querySelectorAll('.color-btn').forEach(btn => {
                btn.classList.remove('border-indigo-600');
                btn.classList.add('border-gray-300');
            });
            event.target.classList.remove('border-gray-300');
            event.target.classList.add('border-indigo-600');
    };

    // Size selector
    window.selectSize = (size) => {
        selectedSize = size;
        
            // Update button styles
            document.querySelectorAll('.size-btn').forEach(btn => {
                btn.classList.remove('border-indigo-600', 'bg-indigo-50');
            btn.classList.add('border-gray-300');
        });
        event.target.classList.remove('border-gray-300');
        event.target.classList.add('border-indigo-600', 'bg-indigo-50');
    };

    // Quantity
    window.increaseQuantity = () => {
        const input = document.getElementById('quantity');
        if (input) input.value = parseInt(input.value) + 1;
    };

    window.decreaseQuantity = () => {
        const input = document.getElementById('quantity');
        if (input && parseInt(input.value) > 1) {
            input.value = parseInt(input.value) - 1;
        }
    };

    // Cart
    window.addToCart = (productName, price) => {
        let quantity = 1;
        const qtyInput = document.getElementById('quantity');
        if (qtyInput) quantity = parseInt(qtyInput.value);
        cartCount += quantity;
        
            // Update cart badge in navbar component
            const badge = document.querySelector('custom-navbar')?.shadowRoot?.querySelector('.cart-badge');
            if (badge) badge.textContent = cartCount;
        
            // Also update in header if present
            const headerBadge = document.getElementById('cart-count');
            if (headerBadge) headerBadge.textContent = cartCount;
        
            // Toast
            showToast(`Added ${quantity} × ${productName || 'item'} to cart!`);
    };

    // Toast
    window.showToast = (message) => {
        const toast = document.createElement('div');
        toast.className = 'toast';
        toast.textContent = message;
        document.body.appendChild(toast);
        
            setTimeout(() => {
                toast.style.animation = 'slideOut 0.3s ease-in forwards';
                setTimeout(() => {
                    document.body.removeChild(toast);
                }, 300);
            }, 3000);
    };

    // Event listeners for custom components
    document.addEventListener('open-cart', () => {
        // Open cart drawer (simulate)
        showToast('Cart opened!');
    });

    document.addEventListener('open-search', () => {
        // Open search modal
        const modal = document.createElement('div');
        modal.className = 'fixed inset-0 bg-black/50 z-50 flex items-center justify-center">
            <div class="bg-white p-8 rounded-2xl max-w-2xl w-full mx-4">
                <h3 class="text-2xl font-bold mb-4">Search Products</h3>
                <input type="text" class="w-full border p-4 rounded-lg" placeholder="Type product name...">
            </div>
        );
        document.body.appendChild(modal);
        modal.querySelector('input').focus();
        modal.addEventListener('click', (e) => {
            if (e.target === modal) {
                document.body.removeChild(modal);
            }
        });
    });

    // Lazy load images
    const lazyImages = document.querySelectorAll('img[data-src]');
    if (lazyImages.length > 0) {
        const imageObserver = new IntersectionObserver((entries) => {
            entries.forEach(entry => {
                if (entry.isIntersecting) {
                    const img = entry.target;
                    img.src = img.dataset.src;
                img.classList.remove('loading');
                imageObserver.unobserve(img);
            }
        });
        lazyImages.forEach(img => imageObserver.observe(img));
    }
});