document.addEventListener('DOMContentLoaded', function() { const featuredImagesContainer = document.getElementById('featured-images'); const exploreBtn = document.getElementById('exploreBtn'); // Loading spinner function showLoading() { featuredImagesContainer.innerHTML = `
`; } // Fetch NASA images async function fetchNASAImages() { showLoading(); try { const response = await fetch('https://images-api.nasa.gov/search?q=galaxy&media_type=image'); const data = await response.json(); if (data.collection && data.collection.items) { displayImages(data.collection.items.slice(0, 6)); } else { featuredImagesContainer.innerHTML = '

Failed to load images. Please try again later.

'; } } catch (error) { console.error('Error fetching NASA images:', error); featuredImagesContainer.innerHTML = '

Error loading images. Check your connection.

'; } } // Display images in grid function displayImages(images) { featuredImagesContainer.innerHTML = ''; images.forEach((item, index) => { const imageData = item.data[0]; const imageUrl = item.links ? item.links[0].href : 'http://static.photos/space/640x360/' + (index + 1); const card = document.createElement('div'); card.className = 'image-card overflow-hidden'; card.innerHTML = `
${imageData.title || 'Space Image'}

${imageData.title || 'Untitled'}

${new Date(imageData.date_created).getFullYear() || 'Unknown date'}

${imageData.description || 'No description available'}

`; featuredImagesContainer.appendChild(card); }); } // Initialize fetchNASAImages(); // Button event exploreBtn.addEventListener('click', function() { this.innerHTML = 'Loading...'; setTimeout(() => { this.innerHTML = 'Explore Gallery'; window.scrollTo({ top: document.querySelector('#featured-images').offsetTop - 100, behavior: 'smooth' }); }, 500); }); });