(() => { const root = document.body; const themeToggle = document.getElementById('themeToggle'); const storedTheme = localStorage.getItem('pg-theme'); if (storedTheme === 'light' || storedTheme === 'dark') { root.setAttribute('data-bs-theme', storedTheme); } const syncThemeButton = () => { if (!themeToggle) return; themeToggle.textContent = root.getAttribute('data-bs-theme') === 'dark' ? 'Modo claro' : 'Modo escuro'; }; syncThemeButton(); themeToggle?.addEventListener('click', () => { const next = root.getAttribute('data-bs-theme') === 'dark' ? 'light' : 'dark'; root.setAttribute('data-bs-theme', next); localStorage.setItem('pg-theme', next); syncThemeButton(); }); const video = document.getElementById('heroVideo'); let rafId = null; const fadeWindow = 0.5; const monitorVideo = () => { if (video && Number.isFinite(video.duration) && video.duration > 0) { const current = video.currentTime || 0; const duration = video.duration; let opacity = 1; if (current < fadeWindow) opacity = current / fadeWindow; if (duration - current < fadeWindow) opacity = Math.max(0, (duration - current) / fadeWindow); video.style.opacity = String(Math.max(0, Math.min(1, opacity * 0.7))); } rafId = requestAnimationFrame(monitorVideo); }; if (video) { video.addEventListener('loadedmetadata', () => { video.currentTime = 0; video.play().catch(() => {}); if (!rafId) monitorVideo(); }); video.addEventListener('ended', () => { video.style.opacity = '0'; window.setTimeout(() => { video.currentTime = 0; video.play().catch(() => {}); }, 100); }); video.load(); } const parallaxItems = [video, document.querySelector('.stats-panel')].filter(Boolean); const handleParallax = () => { const y = window.scrollY || 0; parallaxItems.forEach((item, idx) => { const speed = idx === 0 ? 0.08 : -0.025; item.style.transform = `translate3d(0, ${y * speed}px, 0)`; }); }; if (!window.matchMedia('(prefers-reduced-motion: reduce)').matches) { window.addEventListener('scroll', handleParallax, { passive: true }); handleParallax(); } const form = document.getElementById('contactForm'); const alertBox = document.getElementById('formAlert'); const submitBtn = document.getElementById('submitBtn'); const toastEl = document.getElementById('siteToast'); const toastBody = document.getElementById('toastBody'); const showAlert = (type, message) => { if (!alertBox) return; alertBox.className = `alert alert-${type}`; alertBox.textContent = message; }; const showToast = (message) => { if (toastBody) toastBody.textContent = message; if (toastEl && window.bootstrap) new bootstrap.Toast(toastEl).show(); }; form?.addEventListener('submit', async (event) => { event.preventDefault(); showAlert('secondary', 'A enviar o teu pedido...'); if (submitBtn) { submitBtn.disabled = true; submitBtn.textContent = 'A enviar...'; } try { const response = await fetch('api/contact.php', { method: 'POST', headers: { 'Accept': 'application/json' }, body: new FormData(form) }); const data = await response.json().catch(() => ({})); if (!response.ok || !data.success) { showAlert('warning', data.message || 'Não foi possível enviar. Revê os campos e tenta novamente.'); return; } form.reset(); showAlert('success', data.message || 'Mensagem enviada com sucesso.'); showToast('Pedido recebido. Obrigado!'); } catch (error) { showAlert('danger', 'Erro de ligação. Tenta novamente dentro de instantes.'); } finally { if (submitBtn) { submitBtn.disabled = false; submitBtn.textContent = 'Enviar mensagem'; } } }); const cookieBanner = document.getElementById("cookieBanner"); const cookieAccept = document.getElementById("cookieAccept"); const cookieDecline = document.getElementById("cookieDecline"); const cookieChoice = localStorage.getItem("pg-cookie-choice"); const closeCookieBanner = (choice) => { localStorage.setItem("pg-cookie-choice", choice); cookieBanner?.classList.remove("is-visible"); }; if (cookieBanner && !cookieChoice) { window.setTimeout(() => cookieBanner.classList.add("is-visible"), 650); } cookieAccept?.addEventListener("click", () => closeCookieBanner("accepted")); cookieDecline?.addEventListener("click", () => closeCookieBanner("declined")); })();