document.addEventListener('DOMContentLoaded', () => { // Create stars for background effect createStars(); // Set up button event listeners document.getElementById('apodBtn').addEventListener('click', loadAPOD); document.getElementById('marsBtn').addEventListener('click', loadMarsPhotos); // Load default content (APOD) loadAPOD(); }); function createStars() { const starsContainer = document.createElement('div'); starsContainer.className = 'stars'; for (let i = 0; i < 150; i++) { const star = document.createElement('div'); star.className = 'star'; star.style.left = `${Math.random() * 100}%`; star.style.top = `${Math.random() * 100}%`; star.style.width = `${Math.random() * 3}px`; star.style.height = star.style.width; star.style.setProperty('--duration', `${Math.random() * 5 + 3}s`); star.style.animationDelay = `${Math.random() * 5}s`; starsContainer.appendChild(star); } document.body.appendChild(starsContainer); } async function loadAPOD() { showLoading(); const contentSection = document.getElementById('content-section'); try { const response = await fetch('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY&count=6'); const data = await response.json(); contentSection.innerHTML = ''; data.forEach(item => { const card = createCard( item.title, item.explanation, item.url, item.date, 'Astronomy', item.media_type === 'video' ? 'Video' : 'Image' ); contentSection.appendChild(card); }); } catch (error) { console.error('Error fetching APOD:', error); contentSection.innerHTML = '

Failed to load cosmic images. Please try again later.

'; } finally { hideLoading(); } } async function loadMarsPhotos() { showLoading(); const contentSection = document.getElementById('content-section'); try { const response = await fetch('https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos?sol=1000&api_key=DEMO_KEY'); const data = await response.json(); contentSection.innerHTML = ''; data.photos.slice(0, 6).forEach(photo => { const card = createCard( photo.camera.full_name, `Taken by ${photo.rover.name} rover on Martian sol ${photo.sol}`, photo.img_src, new Date(photo.earth_date).toLocaleDateString(), 'Mars', 'Photo' ); contentSection.appendChild(card); }); } catch (error) { console.error('Error fetching Mars photos:', error); contentSection.innerHTML = '

Failed to load Mars photos. Please try again later.

'; } finally { hideLoading(); } } function createCard(title, description, imageUrl, date, category, type) { const card = document.createElement('div'); card.className = 'card'; card.innerHTML = `
${title}
${type}

${title}

${category}

${description}

${date}
`; return card; } function showLoading() { document.getElementById('loading').classList.remove('hidden'); } function hideLoading() { document.getElementById('loading').classList.add('hidden'); feather.replace(); }