(function () { 'use strict'; const root = document.getElementById('sbm'); if (!root || root.dataset.ready === '1') return; root.dataset.ready = '1'; const dialog = root.querySelector('[data-lead-dialog]'); const primaryForm = root.querySelector('[data-primary-form]'); const formView = root.querySelector('[data-form-view]'); const successView = root.querySelector('[data-form-success]'); const errorView = root.querySelector('[data-form-error]'); const submitButton = root.querySelector('[data-form-submit]'); const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches; function revealContent() { const items = root.querySelectorAll('[data-reveal]'); if (prefersReducedMotion || !('IntersectionObserver' in window)) return; const observer = new IntersectionObserver(function (entries) { entries.forEach(function (entry) { if (!entry.isIntersecting) return; entry.target.classList.add('reveal-in'); observer.unobserve(entry.target); }); }, { rootMargin: '0px 0px -8% 0px', threshold: 0.08 }); items.forEach(function (item) { observer.observe(item); }); } function initDemoPlayer() { const video = root.querySelector('#home-demo-video'); const button = root.querySelector('[data-demo-play]'); const label = root.querySelector('[data-demo-play-label]'); const error = root.querySelector('[data-demo-error]'); const status = root.querySelector('[data-demo-status]'); if (!video || !button || !label || !error || !status || typeof video.play !== 'function') return; const frame = video.parentElement; const dock = frame.querySelector('.demo-dock'); if (!dock) return; const play = dock.querySelector('[data-play-toggle]'); const seek = dock.querySelector('.demo-seek'); const clock = dock.querySelector('.demo-clock'); const mute = dock.querySelector('[data-mute]'); const cc = dock.querySelector('[data-cc]'); const full = dock.querySelector('[data-full]'); const track = video.textTracks && video.textTracks[0]; const time = seconds => Math.floor(seconds / 60) + ':' + String(Math.floor(seconds % 60)).padStart(2, '0'); function sync() { const running = !video.paused && !video.ended; play.dataset.playing = String(running); play.setAttribute('aria-label', running ? 'Pause' : video.ended ? 'Replay' : 'Play'); mute.setAttribute('aria-pressed', String(video.muted)); if (!Number.isFinite(video.duration) || video.duration <= 0) return; seek.max = video.duration; seek.value = video.currentTime; seek.style.setProperty('--played', (100 * video.currentTime / video.duration) + '%'); clock.textContent = time(video.currentTime) + ' / ' + time(video.duration); seek.setAttribute('aria-valuetext', time(video.currentTime) + ' of ' + time(video.duration)); } function playbackError(message) { const focusWasOnButton = document.activeElement === button; video.controls = true; dock.hidden = true; button.hidden = true; button.removeAttribute('aria-disabled'); if (message) { error.hidden = false; status.textContent = message; if (focusWasOnButton) error.querySelector('a').focus({ preventScroll: true }); } } function start() { const promise = video.play(); if (promise) promise.catch(() => playbackError('Could not start playback here.')); } function toggle() { if (!video.paused) { video.pause(); return; } if (video.duration - video.currentTime < 0.1) video.currentTime = 0; start(); } play.addEventListener('click', toggle); video.addEventListener('click', () => { if (!dock.hidden) toggle(); }); seek.addEventListener('input', () => { if (Number.isFinite(video.duration)) video.currentTime = Number(seek.value); }); mute.addEventListener('click', () => { video.muted = !video.muted; }); if (track) { cc.hidden = false; const updateCC = () => cc.setAttribute('aria-pressed', String(track.mode === 'showing')); cc.addEventListener('click', () => { track.mode = track.mode === 'showing' ? 'disabled' : 'showing'; updateCC(); }); video.textTracks.addEventListener('change', updateCC); updateCC(); } if ((document.fullscreenEnabled && frame.requestFullscreen) || video.webkitEnterFullscreen) { full.hidden = false; full.addEventListener('click', async () => { try { if (document.fullscreenElement === frame) await document.exitFullscreen(); else if (document.fullscreenEnabled && frame.requestFullscreen) await frame.requestFullscreen(); else video.webkitEnterFullscreen(); } catch { /* Inline playback remains available. */ } }); document.addEventListener('fullscreenchange', () => full.setAttribute('aria-label', document.fullscreenElement === frame ? 'Exit fullscreen' : 'Enter fullscreen')); } ['timeupdate','durationchange','play','pause','ended','volumechange','seeked'].forEach(event => video.addEventListener(event,sync)); video.controls = false; button.hidden = false; button.addEventListener('click', function () { if (button.hasAttribute('aria-disabled')) return; button.setAttribute('aria-disabled', 'true'); label.textContent = 'Loading video…'; start(); }); video.addEventListener('playing', function () { error.hidden = true; status.textContent = ''; const transferFocus = document.activeElement === button; button.hidden = true; video.controls = false; dock.hidden = false; sync(); if (transferFocus) play.focus({ preventScroll: true }); }); const showLoadError = function () { playbackError('The video could not be loaded.'); }; video.addEventListener('error', showLoadError); const source = video.querySelector('source'); if (source) source.addEventListener('error', showLoadError); } function openDialog(source, seed) { if (!dialog) return; dialog.dataset.source = source || 'homepage'; if (primaryForm && seed) { const businessInput = primaryForm.elements.business; const websiteInput = primaryForm.elements.existing_website; const looksLikeWebsite = /^(?:https?:\/\/)?(?:www\.)?[^\s.]+\.[^\s]{2,}/i.test(seed); if (looksLikeWebsite && websiteInput) websiteInput.value = seed; else if (businessInput) businessInput.value = seed; } if (typeof dialog.showModal === 'function') dialog.showModal(); else dialog.setAttribute('open', ''); window.setTimeout(function () { const target = primaryForm && primaryForm.querySelector('input:not([type="hidden"]), select'); if (target) target.focus({ preventScroll: true }); }, 60); } function closeDialog() { if (!dialog) return; if (typeof dialog.close === 'function') dialog.close(); else dialog.removeAttribute('open'); } function initDialog() { root.querySelectorAll('[data-lead-open]').forEach(function (button) { button.addEventListener('click', function () { openDialog(button.dataset.leadOpen || 'homepage'); }); }); root.querySelectorAll('[data-dialog-close]').forEach(function (button) { button.addEventListener('click', closeDialog); }); if (dialog) { dialog.addEventListener('click', function (event) { if (event.target === dialog) closeDialog(); }); } root.querySelectorAll('[data-quick-lead]').forEach(function (form) { const input = form.elements.quick_business; input.addEventListener('input', function () { input.setCustomValidity(''); }); form.addEventListener('submit', function (event) { event.preventDefault(); const value = String(new FormData(form).get('quick_business') || '').trim(); input.setCustomValidity(value.length >= 3 ? '' : 'Enter your website or business name.'); if (!form.reportValidity()) return; openDialog('quick-form', value); }); }); } function normalizePhone(value) { const raw = String(value || '').trim(); const digits = raw.replace(/\D/g, ''); if (!digits) return ''; return (raw.charAt(0) === '+' ? '+' : '') + digits; } function showError(message) { if (errorView) errorView.textContent = message || ''; } function validatePrimaryForm(event) { showError(''); const data = new FormData(primaryForm); const preferred = String(data.get('preferred_contact') || 'WhatsApp'); const email = String(data.get('email') || '').trim(); const phone = normalizePhone(data.get('phone')); const emailValid = !email || /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(email); const phoneValid = !phone || /^\+?\d{7,15}$/.test(phone); let message = ''; if (!primaryForm.checkValidity()) { event.preventDefault(); primaryForm.reportValidity(); return; } if (!emailValid) message = 'Enter a valid email address.'; else if (!phoneValid) message = 'Enter a valid phone number with country code.'; else if (preferred === 'Email' && !email) message = 'Add an email address so we can contact you.'; else if (preferred === 'WhatsApp' && !phone) message = 'Add a WhatsApp number with country code.'; if (message) { event.preventDefault(); showError(message); return; } if (root.hasAttribute('data-local-preview')) { event.preventDefault(); showError('Preview only. No request was sent.'); return; } primaryForm.elements.business_name.value = String(data.get('business') || '').trim(); primaryForm.elements.website.value = String(data.get('existing_website') || '').trim(); primaryForm.elements.phone.value = phone; primaryForm.elements.phone_update_consent.value = preferred === 'WhatsApp' ? 'yes' : 'no'; primaryForm.elements.industry.value = dialog ? (dialog.dataset.source || 'homepage') : 'homepage'; if (submitButton) { submitButton.disabled = true; submitButton.dataset.label = submitButton.textContent; submitButton.textContent = 'Sending…'; } } function resetSubmitButton() { if (!submitButton) return; submitButton.disabled = false; submitButton.textContent = submitButton.dataset.label || 'Send my request'; } function initPrimaryForm() { if (!primaryForm) return; primaryForm.addEventListener('submit', validatePrimaryForm); primaryForm.addEventListener('sb-form:success', function (event) { event.preventDefault(); resetSubmitButton(); if (formView) formView.hidden = true; if (successView) { successView.hidden = false; dialog.setAttribute('aria-labelledby', 'proposal-success-title'); successView.querySelector('h3').focus(); } }); primaryForm.addEventListener('sb-form:error', function (event) { event.preventDefault(); resetSubmitButton(); showError('We could not send the request. Please try again or continue on WhatsApp.'); }); } revealContent(); initDemoPlayer(); initDialog(); initPrimaryForm(); }());