/* ============================================================ AUSTIN TURF & STONE — Main JavaScript ============================================================ */ (function() { 'use strict'; // ----- Header scroll behavior ----- const header = document.getElementById('siteHeader'); if (header) { let lastScroll = 0; window.addEventListener('scroll', function() { const y = window.scrollY; header.classList.toggle('scrolled', y > 60); lastScroll = y; }, { passive: true }); } // ----- Mobile nav toggle ----- const navToggle = document.getElementById('navToggle'); const mainNav = document.getElementById('mainNav'); let overlay = document.querySelector('.nav-overlay'); if (navToggle && mainNav) { if (!overlay) { overlay = document.createElement('div'); overlay.className = 'nav-overlay'; document.body.appendChild(overlay); } function openNav() { mainNav.classList.add('open'); navToggle.classList.add('active'); overlay.classList.add('visible'); document.body.style.overflow = 'hidden'; } function closeNav() { mainNav.classList.remove('open'); navToggle.classList.remove('active'); overlay.classList.remove('visible'); document.body.style.overflow = ''; } navToggle.addEventListener('click', function() { mainNav.classList.contains('open') ? closeNav() : openNav(); }); overlay.addEventListener('click', closeNav); // Mobile dropdown toggles document.querySelectorAll('.nav-item.has-dropdown > .nav-link').forEach(function(link) { link.addEventListener('click', function(e) { if (window.innerWidth <= 768) { e.preventDefault(); this.parentElement.classList.toggle('dropdown-open'); } }); }); } // ----- FAQ Accordion ----- document.querySelectorAll('.faq-question').forEach(function(btn) { btn.addEventListener('click', function() { const item = this.closest('.faq-item'); const answer = item.querySelector('.faq-answer'); const isActive = item.classList.contains('active'); // Close all others in same list const list = item.closest('.faq-list'); if (list) { list.querySelectorAll('.faq-item.active').forEach(function(other) { if (other !== item) { other.classList.remove('active'); other.querySelector('.faq-answer').style.maxHeight = '0'; } }); } if (isActive) { item.classList.remove('active'); answer.style.maxHeight = '0'; } else { item.classList.add('active'); answer.style.maxHeight = answer.scrollHeight + 'px'; } }); }); // ----- Scroll animations ----- const animatedEls = document.querySelectorAll('.animate-on-scroll'); if (animatedEls.length && 'IntersectionObserver' in window) { const observer = new IntersectionObserver(function(entries) { entries.forEach(function(entry) { if (entry.isIntersecting) { entry.target.classList.add('visible'); observer.unobserve(entry.target); } }); }, { threshold: 0.1, rootMargin: '0px 0px -40px 0px' }); animatedEls.forEach(function(el) { observer.observe(el); }); } // ----- ROI Calculator ----- const calcForm = document.getElementById('roiCalculator'); if (calcForm) { const calcBtn = document.getElementById('calcBtn'); const resultBox = document.getElementById('calcResult'); function runCalc() { const sqft = parseFloat(document.getElementById('calcSqft').value) || 0; const waterBill = parseFloat(document.getElementById('calcWater').value) || 0; const maintenance = parseFloat(document.getElementById('calcMaint').value) || 0; if (sqft <= 0) return; const irrigationShare = 0.35; const monthlyIrrigation = waterBill * irrigationShare; const annualSavings = (monthlyIrrigation + maintenance) * 12; const installCost = sqft * 10.00; const paybackYears = annualSavings > 0 ? installCost / annualSavings : 0; const roi15yr = (annualSavings * 15) - installCost; const monthlySavings = monthlyIrrigation + maintenance; document.getElementById('resultCost').textContent = '$' + installCost.toLocaleString('en-US', { maximumFractionDigits: 0 }); document.getElementById('resultAnnual').textContent = '$' + annualSavings.toLocaleString('en-US', { maximumFractionDigits: 0 }); document.getElementById('resultPayback').textContent = paybackYears > 0 ? paybackYears.toFixed(1) + ' yrs' : '—'; document.getElementById('resultROI').textContent = '$' + roi15yr.toLocaleString('en-US', { maximumFractionDigits: 0 }); resultBox.classList.add('visible'); resultBox.scrollIntoView({ behavior: 'smooth', block: 'nearest' }); } if (calcBtn) { calcBtn.addEventListener('click', function(e) { e.preventDefault(); runCalc(); }); } } // ----- Smooth scroll for anchor links ----- document.querySelectorAll('a[href^="#"]').forEach(function(link) { link.addEventListener('click', function(e) { const target = document.querySelector(this.getAttribute('href')); if (target) { e.preventDefault(); const offset = 100; const pos = target.getBoundingClientRect().top + window.scrollY - offset; window.scrollTo({ top: pos, behavior: 'smooth' }); } }); }); // ----- Form submission placeholder ----- document.querySelectorAll('.estimate-form').forEach(function(form) { form.addEventListener('submit', function(e) { e.preventDefault(); var btn = form.querySelector('button[type="submit"]'); var origText = btn.textContent; btn.textContent = 'Sent! We\'ll be in touch.'; btn.disabled = true; btn.style.background = 'var(--green-600)'; setTimeout(function() { btn.textContent = origText; btn.disabled = false; btn.style.background = ''; form.reset(); }, 4000); }); }); })();