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
 
 
 
 
 
 
 
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// Main application JavaScript for FotoSphere Collective

class PhotoGallery {
    constructor() {
        this.photos = [];
        this.currentPage = 1;
        this.isLoggedIn = false;
        this.currentUser = null;
        this.sortBy = 'latest';
        this.init();
    }
    init() {
        this.loadSamplePhotos();
        this.setupEventListeners();
        this.checkAuthStatus();
        this.setupGlobalEventListeners();
    }

    setupEventListeners() {
        // Auth buttons
        const registerBtn = document.getElementById('registerBtn');
        const loginBtn = document.getElementById('loginBtn');
        const closeModal = document.getElementById('closeModal');
        const switchAuth = document.getElementById('switchAuth');
        const authForm = document.getElementById('authForm');

        if (registerBtn) {
            registerBtn.addEventListener('click', () => this.showAuthModal('register'));
        }
        if (loginBtn) {
            loginBtn.addEventListener('click', () => this.showAuthModal('login'));
        }
        if (closeModal) {
            closeModal.addEventListener('click', () => this.hideAuthModal());
        }
        if (switchAuth) {
            switchAuth.addEventListener('click', () => this.switchAuthMode());
        }
        if (authForm) {
            authForm.addEventListener('submit', (e) => this.handleAuthSubmit(e));
        }

        // Sort buttons
        const sortLatest = document.getElementById('sortLatest');
        const sortPopular = document.getElementById('sortPopular');

        if (sortLatest) {
            sortLatest.addEventListener('click', () => this.sortPhotos('latest'));
        }
        if (sortPopular) {
            sortPopular.addEventListener('click', () => this.sortPhotos('popular'));
        }

        // Load more button
        const loadMore = document.getElementById('loadMore');
        if (loadMore) {
            loadMore.addEventListener('click', () => this.loadMorePhotos());
        }

        // Upload modal
        const closeUploadModal = document.getElementById('closeUploadModal');
        if (closeUploadModal) {
            closeUploadModal.addEventListener('click', () => this.hideUploadModal());
        }

        // File upload
        const dropZone = document.getElementById('dropZone');
        const fileInput = document.getElementById('fileInput');

        if (dropZone && fileInput) {
            dropZone.addEventListener('click', () => fileInput.click());
            dropZone.addEventListener('dragover', (e) => this.handleDragOver(e));
            dropZone.addEventListener('drop', (e) => this.handleFileDrop(e));
            fileInput.addEventListener('change', (e) => this.handleFileSelect(e));
        }

        // Close modals on outside click
        const authModal = document.getElementById('authModal');
        const uploadModal = document.getElementById('uploadModal');

        if (authModal) {
            authModal.addEventListener('click', (e) => {
                if (e.target.id === 'authModal') this.hideAuthModal();
            }
        });

        if (uploadModal) {
            uploadModal.addEventListener('click', (e) => {
                if (e.target.id === 'uploadModal') this.hideUploadModal();
            }
        });
    }

    setupGlobalEventListeners() {
        // Listen for events from navbar
        document.addEventListener('showAuthModal', (e) => {
            this.showAuthModal(e.detail.mode);
        });

        document.addEventListener('showUploadModal', () => {
            this.showUploadModal();
        });
    }
loadSamplePhotos() {
        // Using Picsum API for sample photos
        const categories = ['nature', 'cityscape', 'travel', 'aerial', 'minimal'];
        const samplePhotos = [];

        for (let i = 1; i <= 12; i++) {
            const category = categories[Math.floor(Math.random() * categories.length)];
            samplePhotos.push({
                id: i,
                title: `Fotografia ${i}`,
                description: 'Krásny záber zachytený našou komunitou',
                url: `http://static.photos/${category}/640x360/${i}`,
                author: `Fotograf ${i}`,
                likes: Math.floor(Math.random() * 100),
                date: new Date(Date.now() - Math.random() * 7 * 24 * 60 * 60 * 1000),
                views: Math.floor(Math.random() * 500)
            });
        }

        this.photos = samplePhotos.sort((a, b) => new Date(b.date) - new Date(a.date));
        this.renderGallery();
    }

    renderGallery() {
        const galleryGrid = document.getElementById('galleryGrid');
        galleryGrid.innerHTML = '';

        this.photos.forEach(photo => {
            const photoCard = this.createPhotoCard(photo);
            galleryGrid.appendChild(photoCard);
        });
    }

    createPhotoCard(photo) {
        const card = document.createElement('div');
        card.className = 'photo-card bg-gray-800 rounded-xl overflow-hidden fade-in';
        
        card.innerHTML = `
            <div class="relative group cursor-pointer">
                <img src="${photo.url}" alt="${photo.title}" class="w-full h-64 object-cover">
                <div class="absolute inset-0 bg-black bg-opacity-0 group-hover:bg-opacity-40 transition-all duration-300">
                    <div class="absolute bottom-0 left-0 right-0 p-4 photo-overlay">
                    <h3 class="font-semibold text-white truncate">${photo.title}</h3>
                    <p class="text-gray-300 text-sm truncate">${photo.description}</p>
                    <div class="flex items-center justify-between mt-2">
                        <div class="flex items-center gap-2 text-gray-300">
                            <i data-feather="eye" class="w-4 h-4"></i>
                            <span class="text-sm">${photo.views}</span>
                        </div>
                        <div class="flex items-center gap-2">
                            <button class="text-gray-300 hover:text-red-500 transition-colors">
                                <i data-feather="heart" class="w-4 h-4"></i>
                            </button>
                            <span class="text-sm text-gray-300">${photo.likes}</span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="p-4">
                <div class="flex items-center justify-between">
                    <span class="text-cyan-400 text-sm">${photo.author}</span>
                    <span class="text-gray-400 text-sm">${this.formatDate(photo.date)}</span>
                </div>
            </div>
        `;

        // Add event listeners for interactive elements
        const likeBtn = card.querySelector('button');
        likeBtn.addEventListener('click', (e) => {
            e.stopPropagation();
            this.handleLike(photo.id);
        });

        card.addEventListener('click', () => this.showPhotoDetail(photo));

        return card;
    }

    formatDate(date) {
        return new Date(date).toLocaleDateString('sk-SK');
    }
    showAuthModal(mode) {
        const modal = document.getElementById('authModal');
        const title = document.getElementById('modalTitle');
        const nameField = document.getElementById('nameField');
        const switchText = document.getElementById('switchAuthText');
        const switchBtn = document.getElementById('switchAuth');

        if (modal && title && nameField && switchText && switchBtn) {
            if (mode === 'register') {
                title.textContent = 'Registrácia';
                nameField.classList.remove('hidden');
                switchText.textContent = 'Už máte účet?';
            switchBtn.textContent = 'Prihlásiť sa';
            } else {
                title.textContent = 'Prihlásenie';
                nameField.classList.add('hidden');
                switchText.textContent = 'Nemáte účet?';
                switchBtn.textContent = 'Zaregistrovať sa';
            }

            modal.classList.remove('hidden');
            setTimeout(() => feather.replace(), 100);
        }
    }
hideAuthModal() {
        document.getElementById('authModal').classList.add('hidden');
    }

    switchAuthMode() {
        const currentTitle = document.getElementById('modalTitle').textContent;
        if (currentTitle === 'Prihlásenie') {
            this.showAuthModal('register');
        } else {
            this.showAuthModal('login');
        }
    }

    handleAuthSubmit(e) {
        e.preventDefault();
        // Simulate auth process
        this.isLoggedIn = true;
        this.currentUser = {
            name: 'Fotograf',
            email: 'user@example.com'
        };
        
        this.hideAuthModal();
        this.updateUIForAuth();
        
        // Show success message
        this.showNotification('Úspešne prihlásený!', 'success');
    }
    showUploadModal() {
        const uploadModal = document.getElementById('uploadModal');
        if (uploadModal) {
            uploadModal.classList.remove('hidden');
            setTimeout(() => feather.replace(), 100);
        }
    }
    hideUploadModal() {
        const uploadModal = document.getElementById('uploadModal');
        if (uploadModal) {
            uploadModal.classList.add('hidden');
        }
    }
handleDragOver(e) {
        e.preventDefault();
        document.getElementById('dropZone').classList.add('dragover');
    }

    handleFileDrop(e) {
        e.preventDefault();
        document.getElementById('dropZone').classList.remove('dragover');
        this.handleFiles(e.dataTransfer.files);
    }

    handleFileSelect(e) {
        this.handleFiles(e.target.files);
    }

    handleFiles(files) {
        if (files.length > 0) {
            const file = files[0];
            if (file.type.startsWith('image/')) {
                // Simulate file upload
                this.showNotification('Fotografia bola úspešne nahraná!', 'success');
            this.hideUploadModal();
        }
    }

    handleLike(photoId) {
        const photo = this.photos.find(p => p.id === photoId);
        if (photo) {
            photo.likes += 1;
            this.renderGallery();
            setTimeout(() => feather.replace(), 100);
        }
    }
    sortPhotos(method) {
        this.sortBy = method;
        
        if (method === 'latest') {
            this.photos.sort((a, b) => new Date(b.date) - new Date(a.date));
        } else {
            this.photos.sort((a, b) => b.likes - a.likes);
        }
        
        this.renderGallery();
        this.updateSortButtons();
    }
updateSortButtons() {
        const latestBtn = document.getElementById('sortLatest');
        const popularBtn = document.getElementById('sortPopular');

        if (this.sortBy === 'latest') {
            latestBtn.classList.add('bg-cyan-600');
            latestBtn.classList.remove('bg-gray-700');
            popularBtn.classList.remove('bg-cyan-600');
            popularBtn.classList.add('bg-gray-700');
        } else {
            popularBtn.classList.add('bg-cyan-600');
            popularBtn.classList.remove('bg-gray-700');
            latestBtn.classList.add('bg-gray-700');
            latestBtn.classList.remove('bg-cyan-600');
        }
    }

    loadMorePhotos() {
        // Simulate loading more photos
        const btn = document.getElementById('loadMore');
        btn.textContent = 'Načítava sa...';
        btn.disabled = true;

        setTimeout(() => {
            const newPhotos = [];
            const categories = ['nature', 'cityscape', 'travel', 'aerial', 'minimal'];
        
        for (let i = this.photos.length + 1; i <= this.photos.length + 4; i++) {
                const category = categories[Math.floor(Math.random() * categories.length)];
                newPhotos.push({
                    id: i,
                    title: `Fotografia ${i}`,
                    description: 'Ďalší úžasný záber od našej komunity',
                    url: `http://static.photos/${category}/640x360/${i}`,
                    author: `Fotograf ${i}`,
                    likes: Math.floor(Math.random() * 100),
                    date: new Date(Date.now() - Math.random() * 14 * 24 * 60 * 60 * 1000),
                    views: Math.floor(Math.random() * 500)
                });
            }

            this.photos = [...this.photos, ...newPhotos];
            if (this.sortBy === 'latest') {
                this.photos.sort((a, b) => new Date(b.date) - new Date(a.date));
            
            this.renderGallery();
            btn.textContent = 'Načítať viac fotografií';
            btn.disabled = false;
            
            setTimeout(() => feather.replace(), 100);
            this.showNotification('Nové fotografie načítané!', 'info');
        }, 1000);
    }

    showPhotoDetail(photo) {
        // In a real app, this would show a detailed view or lightbox
        this.showNotification(`Zobrazuje sa: ${photo.title}`, 'info');
    }

    checkAuthStatus() {
        // Check if user is logged in (in real app, check localStorage/tokens)
        this.updateUIForAuth();
    }
    updateUIForAuth() {
        const navbar = document.querySelector('custom-navbar');
        if (navbar && navbar.shadowRoot) {
            const uploadBtn = navbar.shadowRoot.querySelector('#uploadBtn');
            const authSection = navbar.shadowRoot.querySelector('#authSection');
            
            if (this.isLoggedIn) {
                if (uploadBtn) {
                    uploadBtn.classList.remove('hidden');
                }
                if (authSection) {
                    authSection.classList.add('hidden');
                }
            }
        }
    }
    showNotification(message, type = 'info') {
        // Create notification element
        const notification = document.createElement('div');
        const bgColor = type === 'success' ? 'bg-green-600' : 
                          type === 'error' ? 'bg-red-600' : 'bg-cyan-600';
        
        notification.className = `fixed top-4 right-4 ${bgColor} text-white px-6 py-3 rounded-lg shadow-lg z-50 fade-in';
        notification.textContent = message;
        
        document.body.appendChild(notification);
        
        // Remove after 3 seconds
        setTimeout(() => {
            notification.classList.add('opacity-0', 'transition-opacity', 'duration-300');
            setTimeout(() => {
                if (notification.parentNode) {
                    notification.parentNode.removeChild(notification);
                }
            }, 3000);
        }, 100);
    }
}

// Initialize the application when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
    new PhotoGallery();
});

// Theme management
function toggleTheme() {
    const html = document.documentElement;
    if (html.classList.contains('dark')) {
        html.classList.remove('dark');
        html.classList.add('light');
        localStorage.setItem('theme', 'light');
    } else {
        html.classList.remove('light');
        html.classList.add('dark');
        localStorage.setItem('theme', 'dark');
    }
}

// Check for saved theme preference
if (localStorage.getItem('theme') === 'light') {
    document.documentElement.classList.remove('dark');
    document.documentElement.classList.add('light');
}