forked from RemiHiya/MovieFinder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
665 lines (549 loc) · 22 KB
/
Copy pathscript.js
File metadata and controls
665 lines (549 loc) · 22 KB
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
const GRID_SIZE = 100;
const CELL_WIDTH = 145;
const CELL_HEIGHT = 210;
const CARD_WIDTH = 110;
const CARD_HEIGHT = 165;
const viewport = document.getElementById('viewport');
const container = document.getElementById('grid-container');
const searchBar = document.getElementById('search-bar');
const clearSearchBtn = document.getElementById('clear-search-btn');
const randomBtn = document.getElementById('random-btn');
// Modal Elements
const modal = document.getElementById('movie-modal');
const modalTitle = document.getElementById('modal-title');
const modalDate = document.getElementById('modal-date');
const modalDuration = document.getElementById('modal-duration');
const modalTrailer = document.getElementById('modal-trailer');
const modalGenres = document.getElementById('modal-genres');
const modalOverview = document.getElementById('modal-overview');
const closeModalBtn = document.querySelector('.close-btn');
const modalPosterPlaceholder = document.querySelector('.modal-poster-placeholder');
// Video Modal Elements
const videoModal = document.getElementById('video-modal');
const youtubeIframe = document.getElementById('youtube-iframe');
const videoCloseBtn = document.querySelector('.video-close-btn');
// Filters elements
const filterToggleBtn = document.getElementById('filter-toggle-btn');
const filterSidebar = document.getElementById('filter-sidebar');
const sidebarCloseBtn = document.getElementById('sidebar-close-btn');
const genresContainer = document.getElementById('genres-container');
const resetFiltersBtn = document.getElementById('reset-filters-btn');
const yearMinInput = document.getElementById('year-min');
const yearMaxInput = document.getElementById('year-max');
const yearLabel = document.getElementById('year-range-label');
const runtimeMinInput = document.getElementById('runtime-min');
const runtimeMaxInput = document.getElementById('runtime-max');
const runtimeLabel = document.getElementById('runtime-range-label');
let moviesData = [];
let cardsElements = [];
// Filters states
let activeFilters = {
genres: new Set(),
minYear: -1,
maxYear: -1,
minRuntime: -1,
maxRuntime: -1
};
// Dataset extremum values
let datasetBounds = {
minYear: -1, maxYear: -1,
minRuntime: -1, maxRuntime: -1
};
let targetPanX = window.innerWidth / 2 - (GRID_SIZE * CELL_WIDTH) / 2;
let targetPanY = window.innerHeight / 2 - (GRID_SIZE * CELL_HEIGHT) / 2;
let currentPanX = targetPanX;
let currentPanY = targetPanY;
let isMouseDown = false;
let isDragging = false;
let hasDragged = false;
let startX, startY;
let mouseDownX, mouseDownY;
const DRAG_THRESHOLD = 5;
let activeMovieRecord = null;
let modalTargetMovie = null;
let modalRect = { width: 0, height: 0 };
(function lockViewportZoom() {
const meta = document.querySelector('meta[name="viewport"]');
if (meta) {
meta.setAttribute('content', 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover');
}
})();
function isMobileViewport() {
return window.innerWidth <= 768;
}
const adultToggle = document.getElementById('adult-toggle');
let detailsMapCache = null;
let standardGridCache = null;
let adultGridCache = null;
Promise.all([
fetch('data/movie_details.json').then(res => res.json()),
fetch('data/movie_grid.json').then(res => res.json()),
fetch('data/movie_grid_adult.json').then(res => res.json()).catch(() => null)
]).then(([detailsMap, standardGrid, adultGrid]) => {
detailsMapCache = detailsMap;
standardGridCache = standardGrid;
adultGridCache = adultGrid;
loadGridDataset(false);
requestAnimationFrame(updateLoop);
}).catch(err => console.error("Unable to load data :", err));
function loadGridDataset(isAdultEnabled) {
const gridData = (isAdultEnabled && adultGridCache) ? adultGridCache : standardGridCache;
moviesData = gridData.map(gridItem => {
const details = detailsMapCache[gridItem.id.toString()];
return {
...gridItem,
...details
};
});
analyzeDatasetAndSetupFilters();
renderGrid();
}
adultToggle.addEventListener('change', (e) => {
closeMovieModal();
loadGridDataset(e.target.checked);
});
function analyzeDatasetAndSetupFilters() {
const allGenres = new Set();
moviesData.forEach(movie => {
movie.year = movie.release_date ? parseInt(movie.release_date.split('-')[0]) : null;
movie.runtime = movie.runtime || 0;
if (movie.genres) movie.genres.forEach(g => allGenres.add(g));
if (movie.year && (movie.year < datasetBounds.minYear || datasetBounds.minYear === -1)) datasetBounds.minYear = movie.year;
if (movie.year && (movie.year > datasetBounds.maxYear || datasetBounds.minYear === -1)) datasetBounds.maxYear = movie.year;
if (movie.runtime < datasetBounds.minRuntime || datasetBounds.minRuntime === -1) datasetBounds.minRuntime = movie.runtime;
if (movie.runtime > datasetBounds.maxRuntime || datasetBounds.maxRuntime === -1) datasetBounds.maxRuntime = movie.runtime;
});
activeFilters.minYear = datasetBounds.minYear;
activeFilters.maxYear = datasetBounds.maxYear;
activeFilters.minRuntime = datasetBounds.minRuntime;
activeFilters.maxRuntime = datasetBounds.maxRuntime;
setupSliderElement(yearMinInput, yearMaxInput, datasetBounds.minYear, datasetBounds.maxYear, yearLabel, '');
setupSliderElement(runtimeMinInput, runtimeMaxInput, datasetBounds.minRuntime, datasetBounds.maxRuntime, runtimeLabel, 'min');
genresContainer.innerHTML = "";
Array.from(allGenres).sort().forEach(genre => {
const btn = document.createElement('button');
btn.className = 'genre-filter-btn';
btn.textContent = genre;
btn.addEventListener('click', () => {
if (activeFilters.genres.has(genre)) {
activeFilters.genres.delete(genre);
btn.classList.remove('active');
} else {
activeFilters.genres.add(genre);
btn.classList.add('active');
}
closeMovieModal();
});
genresContainer.appendChild(btn);
});
}
function setupSliderElement(minInput, maxInput, minVal, maxVal, labelEl, unit) {
minInput.min = minVal; minInput.max = maxVal; minInput.value = minVal;
maxInput.min = minVal; maxInput.max = maxVal; maxInput.value = maxVal;
const updateLabel = () => {
labelEl.textContent = `${minInput.value} - ${maxInput.value} ${unit}`;
};
minInput.addEventListener('input', () => {
if (parseInt(minInput.value) > parseInt(maxInput.value)) {
minInput.value = maxInput.value;
}
if (unit === 'ans') activeFilters.minYear = parseInt(minInput.value);
else activeFilters.minRuntime = parseInt(minInput.value);
updateLabel();
closeMovieModal();
});
maxInput.addEventListener('input', () => {
if (parseInt(maxInput.value) < parseInt(minInput.value)) {
maxInput.value = minInput.value;
}
if (unit === 'ans') activeFilters.maxYear = parseInt(maxInput.value);
else activeFilters.maxRuntime = parseInt(maxInput.value);
updateLabel();
closeMovieModal();
});
updateLabel();
}
function movieMatchesFilters(movie) {
if (activeFilters.genres.size > 0) {
const hasGenre = movie.genres && movie.genres.some(g => activeFilters.genres.has(g));
if (!hasGenre) return false;
}
if (movie.year && (movie.year < activeFilters.minYear || movie.year > activeFilters.maxYear)) {
return false;
}
if (movie.runtime < activeFilters.minRuntime || movie.runtime > activeFilters.maxRuntime) {
return false;
}
return true;
}
function renderGrid() {
container.innerHTML = '';
cardsElements = [];
moviesData.forEach((movie, index) => {
const card = document.createElement('div');
card.className = 'movie-card';
const safeTitle = (movie.title || "").replace(/"/g, '"');
card.innerHTML = `<img src="${movie.poster}" alt="${safeTitle}" title="${safeTitle}" draggable="false">`;
movie.isVisible = false;
movie.isActiveMovie = false;
card.addEventListener('click', () => {
if (hasDragged) return;
recenterOnMovie(movie);
openMovieModal(movie, card);
});
container.appendChild(card);
cardsElements.push(card);
});
}
function recenterOnMovie(movie) {
const targetX = movie.x * CELL_WIDTH + CELL_WIDTH / 2;
const targetY = movie.y * CELL_HEIGHT + CELL_HEIGHT / 2;
const GRID_WIDTH_PX = GRID_SIZE * CELL_WIDTH;
const GRID_HEIGHT_PX = GRID_SIZE * CELL_HEIGHT;
let currentWorldCenterX = window.innerWidth / 2 - targetPanX;
let currentWorldCenterY = window.innerHeight / 2 - targetPanY;
let dx = targetX - currentWorldCenterX;
let dy = targetY - currentWorldCenterY;
dx = ((dx + GRID_WIDTH_PX / 2) % GRID_WIDTH_PX + GRID_WIDTH_PX) % GRID_WIDTH_PX - GRID_WIDTH_PX / 2;
dy = ((dy + GRID_HEIGHT_PX / 2) % GRID_HEIGHT_PX + GRID_HEIGHT_PX) % GRID_HEIGHT_PX - GRID_HEIGHT_PX / 2;
targetPanX -= dx;
targetPanY -= dy;
}
const modalBackdrop = document.createElement('div');
modalBackdrop.id = 'modal-backdrop';
document.body.appendChild(modalBackdrop);
modalBackdrop.addEventListener('click', closeMovieModal);
const sidebarBackdrop = document.createElement('div');
sidebarBackdrop.id = 'sidebar-backdrop';
document.body.appendChild(sidebarBackdrop);
sidebarBackdrop.addEventListener('click', closeSidebar);
const modalPosterImg = document.createElement('img');
modalPosterImg.id = 'modal-poster-img';
modalPosterImg.alt = '';
modalPosterImg.draggable = false;
modalPosterPlaceholder.appendChild(modalPosterImg);
// --- FONCTIONS VIDEO ---
function getYouTubeId(url) {
const regExp = /^.*(youtu\.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
const match = url.match(regExp);
return (match && match[2].length === 11) ? match[2] : null;
}
function openVideoModal(url) {
const videoId = getYouTubeId(url);
if (videoId) {
youtubeIframe.src = `https://www.youtube.com/embed/${videoId}?autoplay=1`;
videoModal.classList.add('active');
} else {
window.open(url, '_blank');
}
}
function closeVideoModal() {
videoModal.classList.remove('active');
setTimeout(() => {
youtubeIframe.src = '';
}, 300);
}
videoCloseBtn.addEventListener('click', closeVideoModal);
videoModal.addEventListener('click', (e) => {
if (e.target === videoModal) {
closeVideoModal();
}
});
function openMovieModal(movie, card) {
if (activeMovieRecord) {
activeMovieRecord.card.classList.remove('modal-active');
activeMovieRecord.movie.isActiveMovie = false;
}
movie.isActiveMovie = true;
card.classList.add('modal-active');
activeMovieRecord = { movie, card };
modalTargetMovie = movie;
modalTitle.textContent = movie.title;
modalOverview.textContent = movie.overview || "Aucun synopsis disponible.";
modalPosterImg.src = movie.poster || '';
if (movie.release_date) {
const dateObj = new Date(movie.release_date);
const day = String(dateObj.getDate()).padStart(2, '0');
const month = String(dateObj.getMonth() + 1).padStart(2, '0');
const year = dateObj.getFullYear();
modalDate.textContent = `${day}/${month}/${year}`;
} else {
modalDate.textContent = "N/C";
}
if (movie.runtime && movie.runtime > 0) {
const hours = Math.floor(movie.runtime / 60);
const minutes = movie.runtime % 60;
modalDuration.textContent = hours > 0 ? `${hours}h ${minutes}min` : `${minutes}min`;
} else {
modalDuration.textContent = "N/C";
}
if (movie.trailer) {
modalTrailer.style.display = 'inline-flex';
modalTrailer.onclick = (e) => {
e.preventDefault();
openVideoModal(movie.trailer);
};
} else {
modalTrailer.style.display = 'none';
modalTrailer.onclick = null;
}
modalGenres.innerHTML = '';
if (movie.genres) {
movie.genres.forEach(genre => {
const span = document.createElement('span');
span.className = 'genre-tag';
span.textContent = genre;
modalGenres.appendChild(span);
});
}
modal.classList.add('active');
modalBackdrop.classList.add('active');
modalRect.width = modal.offsetWidth;
modalRect.height = modal.offsetHeight;
}
function closeMovieModal() {
if (activeMovieRecord) {
activeMovieRecord.card.classList.remove('modal-active');
activeMovieRecord.movie.isActiveMovie = false;
activeMovieRecord = null;
}
modal.classList.remove('active');
modalBackdrop.classList.remove('active');
}
function closeSidebar() {
filterSidebar.classList.remove('open');
sidebarBackdrop.classList.remove('open');
}
closeModalBtn.addEventListener('click', closeMovieModal);
randomBtn.addEventListener('click', () => {
const validMoviesIndices = [];
moviesData.forEach((movie, idx) => {
if (movieMatchesFilters(movie)) {
validMoviesIndices.push(idx);
}
});
if (validMoviesIndices.length === 0) return;
closeMovieModal();
const randomIndex = validMoviesIndices[Math.floor(Math.random() * validMoviesIndices.length)];
const luckyMovie = moviesData[randomIndex];
recenterOnMovie(luckyMovie);
setTimeout(() => {
const card = cardsElements[randomIndex];
if(card) openMovieModal(luckyMovie, card);
}, 80);
});
function updateLoop() {
const centerX = window.innerWidth / 2;
const centerY = window.innerHeight / 2;
const screenWidth = window.innerWidth;
const screenHeight = window.innerHeight;
const mobile = isMobileViewport();
const effectRadius = mobile ? Math.min(280, screenWidth * 0.6) : 550;
const maxScale = mobile ? 2.2 : 2.8;
const cullingMargin = mobile ? 120 : 200;
const GRID_WIDTH_PX = GRID_SIZE * CELL_WIDTH;
const GRID_HEIGHT_PX = GRID_SIZE * CELL_HEIGHT;
const easeFactor = isMouseDown ? 0.25 : 0.08;
currentPanX += (targetPanX - currentPanX) * easeFactor;
currentPanY += (targetPanY - currentPanY) * easeFactor;
moviesData.forEach((movie, index) => {
const card = cardsElements[index];
if (!card) return;
const isFilteredOut = !movieMatchesFilters(movie);
const movieCenterX = movie.x * CELL_WIDTH + CELL_WIDTH / 2;
const movieCenterY = movie.y * CELL_HEIGHT + CELL_HEIGHT / 2;
const rawX = currentPanX + movieCenterX;
const rawY = currentPanY + movieCenterY;
let relX = rawX - centerX;
let relY = rawY - centerY;
relX = ((relX + GRID_WIDTH_PX / 2) % GRID_WIDTH_PX + GRID_WIDTH_PX) % GRID_WIDTH_PX - GRID_WIDTH_PX / 2;
relY = ((relY + GRID_HEIGHT_PX / 2) % GRID_HEIGHT_PX + GRID_HEIGHT_PX) % GRID_HEIGHT_PX - GRID_HEIGHT_PX / 2;
const undistortedScreenX = centerX + relX;
const undistortedScreenY = centerY + relY;
const distX = undistortedScreenX - centerX;
const distY = undistortedScreenY - centerY;
const distance = Math.sqrt(distX * distX + distY * distY);
let scale = 1;
let pushFactor = 1;
let brightness = 0.25;
if (distance < effectRadius) {
const ratio = distance / effectRadius;
const zoomFactor = Math.pow(1 - ratio, 2.2);
scale = 1 + (maxScale - 1) * zoomFactor;
pushFactor = 1 + (maxScale - 1) * Math.pow(1 - ratio, 3.2);
brightness = 0.25 + 0.75 * Math.pow(1 - ratio, 1.2);
}
const distortedScreenX = centerX + distX * pushFactor;
const distortedScreenY = centerY + distY * pushFactor;
if (movie === modalTargetMovie) {
const modalScale = scale / maxScale;
if (mobile) {
const w = modalRect.width || (window.innerWidth * 0.9);
const h = modalRect.height || 400;
const mx = distortedScreenX - w / 2;
const my = distortedScreenY - h / 2;
modal.style.transform = `translate3d(${mx}px, ${my}px, 0) scale(${modalScale})`;
} else {
const mx = distortedScreenX - 154;
const my = distortedScreenY - 231;
modal.style.transform = `translate3d(${mx}px, ${my}px, 0) scale(${modalScale})`;
}
}
if (isFilteredOut) {
if (movie.isVisible) {
card.style.display = 'none';
movie.isVisible = false;
}
return;
}
const inViewport = (
distortedScreenX >= -cullingMargin &&
distortedScreenX <= screenWidth + cullingMargin &&
distortedScreenY >= -cullingMargin &&
distortedScreenY <= screenHeight + cullingMargin
);
if (inViewport) {
if (!movie.isVisible) {
card.style.display = 'flex';
movie.isVisible = true;
}
const finalX = distortedScreenX - CARD_WIDTH / 2;
const finalY = distortedScreenY - CARD_HEIGHT / 2;
card.style.transform = `translate3d(${finalX}px, ${finalY}px, 0) scale(${scale})`;
card.style.filter = `brightness(${brightness})`;
if (movie.isActiveMovie) {
card.style.zIndex = 25000;
} else {
card.style.zIndex = Math.round(scale * 1000);
}
} else {
if (movie.isVisible) {
card.style.display = 'none';
movie.isVisible = false;
}
}
});
requestAnimationFrame(updateLoop);
}
filterToggleBtn.addEventListener('click', () => {
if (filterSidebar.classList.contains('open')) {
closeSidebar();
} else {
filterSidebar.classList.add('open');
sidebarBackdrop.classList.add('open');
}
});
sidebarCloseBtn.addEventListener('click', closeSidebar);
resetFiltersBtn.addEventListener('click', () => {
activeFilters.genres.clear();
document.querySelectorAll('.genre-filter-btn').forEach(btn => btn.classList.remove('active'));
yearMinInput.value = datasetBounds.minYear;
yearMaxInput.value = datasetBounds.maxYear;
activeFilters.minYear = datasetBounds.minYear;
activeFilters.maxYear = datasetBounds.maxYear;
yearMinInput.dispatchEvent(new Event('input'));
runtimeMinInput.value = datasetBounds.minRuntime;
runtimeMaxInput.value = datasetBounds.maxRuntime;
activeFilters.minRuntime = datasetBounds.minRuntime;
activeFilters.maxRuntime = datasetBounds.maxRuntime;
runtimeMinInput.dispatchEvent(new Event('input'));
closeMovieModal();
});
function getPointerPosition(e) {
if (e.touches && e.touches.length > 0) {
return { x: e.touches[0].clientX, y: e.touches[0].clientY };
}
if (e.changedTouches && e.changedTouches.length > 0) {
return { x: e.changedTouches[0].clientX, y: e.changedTouches[0].clientY };
}
return { x: e.clientX, y: e.clientY };
}
function handleDragStart(e) {
if (e.type === 'mousedown' && e.button !== 0) return;
if (e.touches && e.touches.length > 1) {
isMouseDown = false;
isDragging = false;
return;
}
const pos = getPointerPosition(e);
isMouseDown = true;
hasDragged = false;
isDragging = false;
mouseDownX = pos.x;
mouseDownY = pos.y;
startX = pos.x - targetPanX;
startY = pos.y - targetPanY;
}
function handleDragMove(e) {
if (!isMouseDown) return;
if (e.touches && e.touches.length > 1) return;
const pos = getPointerPosition(e);
if (!isDragging) {
const dx = pos.x - mouseDownX;
const dy = pos.y - mouseDownY;
if (Math.sqrt(dx * dx + dy * dy) > DRAG_THRESHOLD) {
isDragging = true;
hasDragged = true;
closeMovieModal();
}
}
if (isDragging) {
if (e.cancelable) e.preventDefault();
targetPanX = pos.x - startX;
targetPanY = pos.y - startY;
}
}
function handleDragEnd() {
isMouseDown = false;
isDragging = false;
}
viewport.addEventListener('mousedown', handleDragStart);
document.addEventListener('mousemove', handleDragMove);
document.addEventListener('mouseup', handleDragEnd);
viewport.addEventListener('touchstart', handleDragStart, { passive: true });
document.addEventListener('touchmove', handleDragMove, { passive: false });
document.addEventListener('touchend', handleDragEnd, { passive: true });
document.addEventListener('touchcancel', handleDragEnd, { passive: true });
let lastWindowWidth = window.innerWidth;
let lastWindowHeight = window.innerHeight;
window.addEventListener('resize', () => {
const deltaX = (window.innerWidth - lastWindowWidth) / 2;
const deltaY = (window.innerHeight - lastWindowHeight) / 2;
targetPanX += deltaX;
targetPanY += deltaY;
currentPanX += deltaX;
currentPanY += deltaY;
lastWindowWidth = window.innerWidth;
lastWindowHeight = window.innerHeight;
if (modal.classList.contains('active')) {
modalRect.width = modal.offsetWidth;
modalRect.height = modal.offsetHeight;
}
});
searchBar.addEventListener('input', (e) => {
const rawVal = e.target.value;
clearSearchBtn.style.display = rawVal.length > 0 ? 'flex' : 'none';
closeMovieModal();
const query = rawVal.toLowerCase().trim();
cardsElements.forEach(card => card.classList.remove('highlighted'));
if (query.length < 2) return;
const matchedIndex = moviesData.findIndex(movie =>
movieMatchesFilters(movie) && movie.title && movie.title.toLowerCase().includes(query)
);
if (matchedIndex !== -1) {
const matchedMovie = moviesData[matchedIndex];
const card = cardsElements[matchedIndex];
card.classList.add('highlighted');
recenterOnMovie(matchedMovie);
setTimeout(() => {
openMovieModal(matchedMovie, card);
}, 150);
}
});
clearSearchBtn.addEventListener('click', () => {
searchBar.value = '';
clearSearchBtn.style.display = 'none';
cardsElements.forEach(card => card.classList.remove('highlighted'));
closeMovieModal();
searchBar.focus();
});