// Global variables let videos = []; let currentVideoId = null; let currentPage = 1; const videosPerPage = 12; let filteredVideos = []; // DOM elements const videoGrid = document.getElementById('videoGrid'); const suggestionsContainer = document.getElementById('suggestions'); const videoTitle = document.getElementById('videoTitle'); const videoDescription = document.getElementById('videoDescription'); const searchInput = document.getElementById('searchInput'); const searchBtn = document.getElementById('searchBtn'); const paginationContainer = document.getElementById('pagination'); // Initialize the app based on current page document.addEventListener('DOMContentLoaded', () => { loadVideos(); if (window.location.pathname.includes('player.html')) { initPlayer(); } else { // Setup search functionality for index page searchInput.addEventListener('input', handleSearch); searchBtn.addEventListener('click', handleSearch); } }); // Load videos from database.json async function loadVideos() { try { const response = await fetch('database.json'); videos = await response.json(); filteredVideos = [...videos]; if (window.location.pathname.includes('player.html')) { // Player page - load current video and suggestions const urlParams = new URLSearchParams(window.location.search); currentVideoId = urlParams.get('v'); if (currentVideoId) { loadVideo(currentVideoId); renderSuggestions(); } else { // Redirect to home if no video specified window.location.href = 'index.html'; } } else { // Home page - render video grid renderVideoGrid(); renderPagination(); } } catch (error) { console.error('Error loading videos:', error); alert('Failed to load video data. Please try again later.'); } } // Handle search functionality function handleSearch() { const searchTerm = searchInput.value.toLowerCase(); if (searchTerm.trim() === '') { filteredVideos = [...videos]; } else { filteredVideos = videos.filter(video => video.title.toLowerCase().includes(searchTerm) || video.description.toLowerCase().includes(searchTerm) ); } currentPage = 1; renderVideoGrid(); renderPagination(); } // Render video grid on home page with pagination function renderVideoGrid() { if (!videoGrid) return; videoGrid.innerHTML = ''; const startIndex = (currentPage - 1) * videosPerPage; const endIndex = startIndex + videosPerPage; const videosToShow = filteredVideos.slice(startIndex, endIndex); if (videosToShow.length === 0) { videoGrid.innerHTML = '
No videos found matching your search.
'; return; } videosToShow.forEach(video => { const videoCard = document.createElement('div'); videoCard.className = 'video-card'; videoCard.addEventListener('click', () => { window.location.href = `player.html?v=${video.id}`; }); videoCard.innerHTML = `${video.description.substring(0, 100)}...
The video format may not be supported by your browser.
Try using a different browser or check the video file.
${video.duration}
` : ''}