// AgroPulverize Pro - Main JavaScript document.addEventListener('DOMContentLoaded', function() { // Initialize Feather Icons if (typeof feather !== 'undefined') { feather.replace(); } // Load News from RSS loadNews(); // Initialize Forms initForms(); // Initialize Scroll Effects initScrollEffects(); // Initialize Mobile Menu initMobileMenu(); }); // News Loading Function async function loadNews() { const newsContainer = document.getElementById('news-container'); if (!newsContainer) return; // Using a CORS proxy to fetch RSS feed const rssUrls = [ 'https://www.noticiasagricolas.com.br/rss/ultimas-noticias.xml', 'https://g1.globo.com/rss/g1/agronegocios/' ]; try { // Fallback to mock news if RSS fails const mockNews = [ { title: 'Drones agrícolas aumentam produtividade em 40% no Mato Grosso', description: 'Estudo mostra que pulverização aérea com drones reduziu custos e aumentou eficiência na aplicação de defensivos.', image: 'http://static.photos/agriculture/640x360/101', date: '15 Jan 2025', category: 'Tecnologia' }, { title: 'Governo libera novos créditos para modernização do campo', description: 'Produtores rurais terão acesso a financiamentos com taxas reduzidas para aquisição de equipamentos de precisão.', image: 'http://static.photos/finance/640x360/202', date: '14 Jan 2025', category: 'Política' }, { title: 'Sustentabilidade: práticas agrícolas de baixo carbono crescem no Brasil', description: 'Movimento por agricultura regenerativa ganha força entre grandes produtores do Centro-Oeste.', image: 'http://static.photos/nature/640x360/303', date: '13 Jan 2025', category: 'Sustentabilidade' } ]; // Try to fetch real news, fallback to mock let news = mockNews; try { const response = await fetch('https://api.rss2json.com/v1/api.json?rss_url=' + encodeURIComponent(rssUrls[0])); const data = await response.json(); if (data.items && data.items.length > 0) { news = data.items.slice(0, 3).map((item, index) => ({ title: item.title, description: item.description ? item.description.replace(/<[^>]*>/g, '').substring(0, 120) + '...' : 'Leia mais sobre esta notícia...', image: `http://static.photos/agriculture/640x360/${400 + index}`, date: new Date(item.pubDate).toLocaleDateString('pt-BR', { day: '2-digit', month: 'short', year: 'numeric' }), category: 'Agronegócio', link: item.link })); } } catch (e) { console.log('Using mock news data'); } renderNews(news); } catch (error) { console.error('Error loading news:', error); newsContainer.innerHTML = '

Não foi possível carregar as notícias.

'; } } function renderNews(news) { const newsContainer = document.getElementById('news-container'); newsContainer.innerHTML = news.map((item, index) => `
${item.title}
${item.category}
${item.date}

${item.title}

${item.description}

Ler mais
`).join(''); // Re-initialize Feather Icons for new content if (typeof feather !== 'undefined') { feather.replace(); } } // Form Initialization function initForms() { // Contact Form const contactForm = document.getElementById('contact-form'); if (contactForm) { contactForm.addEventListener('submit', handleContactSubmit); } // Login Form const loginForm = document.getElementById('login-form'); if (loginForm) { loginForm.addEventListener('submit', handleLoginSubmit); } } function handleContactSubmit(e) { e.preventDefault(); const formData = new FormData(e.target); const data = Object.fromEntries(formData); // Show success message showToast('Mensagem enviada com sucesso! Entraremos em contato em breve.', 'success'); // Reset form e.target.reset(); // Here you would typically send to a server console.log('Contact form data:', data); } function handleLoginSubmit(e) { e.preventDefault(); // Simulate login showToast('Área do cliente em desenvolvimento. Entre em contato pelo WhatsApp!', 'error'); e.target.reset(); } // Toast Notification function showToast(message, type = 'success') { // Remove existing toasts const existingToast = document.querySelector('.toast'); if (existingToast) { existingToast.remove(); } const toast = document.createElement('div'); toast.className = `toast toast-${type}`; toast.innerHTML = `
${type === 'success' ? 'Sucesso!' : 'Atenção'}
${message}
`; document.body.appendChild(toast); // Initialize feather icons in toast if (typeof feather !== 'undefined') { feather.replace(); } // Show toast requestAnimationFrame(() => { toast.classList.add('show'); }); // Hide after 4 seconds setTimeout(() => { toast.classList.remove('show'); setTimeout(() => toast.remove(), 300); }, 4000); } // Scroll Effects function initScrollEffects() { const navbar = document.querySelector('agro-navbar'); if (!navbar) return; const shadowRoot = navbar.shadowRoot; if (!shadowRoot) return; const nav = shadowRoot.querySelector('nav'); if (!nav) return; let lastScroll = 0; window.addEventListener('scroll', () => { const currentScroll = window.pageYOffset; if (currentScroll > 100) { nav.classList.add('bg-white/95', 'backdrop-blur-md', 'shadow-lg'); nav.classList.remove('bg-transparent'); } else { nav.classList.remove('bg-white/95', 'backdrop-blur-md', 'shadow-lg'); nav.classList.add('bg-transparent'); } lastScroll = currentScroll; }, { passive: true }); // Smooth scroll for anchor links document.querySelectorAll('a[href^="#"]').forEach(anchor => { anchor.addEventListener('click', function(e) { const href = this.getAttribute('href'); if (href === '#') return; const target = document.querySelector(href); if (target) { e.preventDefault(); target.scrollIntoView({ behavior: 'smooth', block: 'start' }); } }); }); } // Mobile Menu function initMobileMenu() { // Mobile menu functionality is handled in the navbar component } // Utility: Format Phone Number function formatPhone(value) { return value .replace(/\D/g, '') .replace(/(\d{2})(\d)/, '($1) $2') .replace(/(\d{5})(\d)/, '$1-$2') .replace(/(-\d{4})\d+?$/, '$1'); } // Utility: Format CPF/CNPJ function formatDocument(value) { const cleaned = value.replace(/\D/g, ''); if (cleaned.length <= 11) { // CPF return cleaned .replace(/(\d{3})(\d)/, '$1.$2') .replace(/(\d{3})(\d)/, '$1.$2') .replace(/(\d{3})(\d{1,2})$/, '$1-$2'); } else { // CNPJ return cleaned .replace(/(\d{2})(\d)/, '$1.$2') .replace(/(\d{3})(\d)/, '$1.$2') .replace(/(\d{3})(\d)/, '$1/$2') .replace(/(\d{4})(\d{1,2})$/, '$1-$2'); } } // Auto-format inputs document.addEventListener('input', function(e) { if (e.target.matches('input[type="tel"]')) { e.target.value = formatPhone(e.target.value); } if (e.target.matches('input[placeholder*="CPF/CNPJ"]')) { e.target.value = formatDocument(e.target.value); } });