/* =========================================================
DECK THE MARKET HALL · voting page script (Webflow)
1. Settings 2. Vote sending 3. Merchant cards + search
4. Vote window, photo window, QR link (?m=slug)
5. Page states (before / open / closed / voted today)
6. Gingerbread man 7. Test panel (?demo=1 only)
Texts live in the Designer; this only shows/hides and fills names.
========================================================= */
(function () {
'use strict';
/* ---------- 1. SETTINGS ---------- */
var OPENS_AT = Date.UTC(2026, 10, 28, 5, 0, 0); // Nov 28, 12:00 AM Ohio time
var CLOSES_AT = Date.UTC(2026, 11, 14, 4, 59, 59); // Dec 13, 11:59:59 PM Ohio time
var API_URL = '/vote-admin/api/vote'; // backend on Webflow Cloud
var PLACEHOLDER = 'https://cdn.prod.website-files.com/6977696970f63bcc0ee33828/6b05adc5564ed23aabe3366e_vad-vote-placeholder.svg';
var DEMO = /[?&]demo=1/.test(location.search); // test mode: votes stay in this browser
var $ = function (s, r) { return (r || document).querySelector(s); };
var $$ = function (s, r) { return Array.prototype.slice.call((r || document).querySelectorAll(s)); };
var store = {
get: function (k) { try { return JSON.parse(localStorage.getItem(k) || 'null'); } catch (e) { return null; } },
set: function (k, v) { try { localStorage.setItem(k, JSON.stringify(v)); } catch (e) {} },
del: function (k) { try { localStorage.removeItem(k); } catch (e) {} }
};
// This form sends votes to our backend, not to Webflow Forms. Webflow's own spam check
// (data-turnstile-sitekey) would lock the Vote button, so it is switched off for this form
// only, before Webflow starts. Our own Turnstile box (below) still blocks bots.
$$('[data-hv-form]').forEach(function (f) { f.removeAttribute('data-turnstile-sitekey'); });
// Webflow's default "Example text" placeholders are cleared
$$('[data-hv-form] input[placeholder="Example text"]').forEach(function (i) { i.removeAttribute('placeholder'); });
var demo = Object.assign({ state: 'open', dayOffset: 0 }, DEMO ? store.get('hv-demo') || {} : {});
function now() { return Date.now() + (DEMO ? demo.dayOffset * 864e5 : 0); }
function todayET() {
return new Intl.DateTimeFormat('en-CA', { timeZone: 'America/New_York', year: 'numeric', month: '2-digit', day: '2-digit' }).format(new Date(now()));
}
function votingState() {
if (DEMO && demo.state !== 'auto') return demo.state;
var t = now();
return t < OPENS_AT ? 'before' : t > CLOSES_AT ? 'closed' : 'open';
}
function normalizeEmail(raw) {
var email = String(raw || '').trim().toLowerCase(), p = email.split('@');
if (p.length !== 2) return email;
var user = p[0].split('+')[0], domain = p[1] === 'googlemail.com' ? 'gmail.com' : p[1];
if (domain === 'gmail.com') user = user.replace(/\./g, '');
return user + '@' + domain;
}
function show(el, on) { if (el) el.style.display = on ? '' : 'none'; }
function showAs(el, on, display) { if (el) el.style.display = on ? display : 'none'; }
/* ---------- 2. VOTE SENDING ---------- */
function sendVote(vote) {
if (DEMO) {
var votes = store.get('hv-demo-votes') || {}, key = normalizeEmail(vote.email) + '|' + todayET();
if (votingState() !== 'open') return Promise.resolve({ ok: false, reason: 'closed' });
if (votes[key]) return Promise.resolve({ ok: false, reason: 'already' });
votes[key] = vote.merchant; store.set('hv-demo-votes', votes);
return new Promise(function (r) { setTimeout(function () { r({ ok: true }); }, 500); });
}
return fetch(API_URL, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(vote) })
.then(function (r) { return r.json(); })
.catch(function () { return { ok: false, reason: 'error' }; });
}
// convenience only: this browser remembers it voted today (the real check is the backend)
function votedToday() { var v = store.get('hv-voted'); return v && v.day === todayET() ? v : null; }
/* ---------- 3. MERCHANT CARDS + SEARCH ---------- */
function cards() { return $$('[data-hv-card]'); }
function cardInfo(card) {
var img = $('.hv_card-img', card), src = img ? img.getAttribute('src') || '' : '';
var hasPhoto = src && !img.classList.contains('w-dyn-bind-empty') && !/placeholder/i.test(src);
return {
slug: ($('.hv_card-slug', card) || {}).textContent ? $('.hv_card-slug', card).textContent.trim() : '',
name: ($('.hv_card-name', card) || {}).textContent ? $('.hv_card-name', card).textContent.trim() : '',
photo: hasPhoto ? (img.currentSrc || src) : PLACEHOLDER
};
}
function prepareCards() {
var list = cards();
list.forEach(function (card) {
var info = cardInfo(card), img = $('.hv_card-img', card);
if (img && info.photo === PLACEHOLDER) img.classList.add('is-empty'); // show the gingerbread placeholder behind
if (img) img.alt = info.name + ' decorated space';
var zoom = $('[data-hv-zoom]', card); if (zoom) zoom.setAttribute('aria-label', 'See ' + info.name + ' larger');
});
// A–Z by name (numbers in natural order) so visitors find merchants easily
var items = list.map(function (c) { return c.closest('.w-dyn-item') || c; });
var parent = items[0] && items[0].parentNode;
if (parent) items.slice().sort(function (a, b) {
return cardInfo($('[data-hv-card]', a) || a).name.localeCompare(cardInfo($('[data-hv-card]', b) || b).name, 'en', { numeric: true, sensitivity: 'base' });
}).forEach(function (it) { parent.appendChild(it); });
}
document.addEventListener('input', function (e) {
if (!e.target.matches || !e.target.matches('[data-hv-search]')) return;
var q = e.target.value.trim().toLowerCase(), shown = 0;
cards().forEach(function (c) {
var hit = !q || cardInfo(c).name.toLowerCase().indexOf(q) > -1;
var item = c.closest('.w-dyn-item') || c;
item.hidden = !hit; if (hit) shown++;
});
showAs($('[data-hv-empty]'), shown === 0, 'block');
});
/* ---------- 4. VOTE WINDOW, PHOTO WINDOW, QR LINK ---------- */
var current = null;
function dialog() { return $('[data-hv-dialog]'); }
function lightbox() { return $('[data-hv-lightbox]'); }
function openOverlay(o) { if (!o) return; o.classList.add('is-open'); document.documentElement.classList.add('hv-lock'); }
function closeOverlay(o) {
if (!o) return; o.classList.remove('is-open');
if (!$('.hv_overlay.is-open')) document.documentElement.classList.remove('hv-lock');
}
function showView(name) {
$$('[data-view]', dialog()).forEach(function (v) { v.style.display = v.getAttribute('data-view') === name ? 'block' : 'none'; });
}
function openVote(info) {
current = info;
var d = dialog();
$('[data-hv-dialog-name]', d).textContent = info.name;
var p = $('.hv_dialog-photo', d); if (p) { p.src = info.photo; p.removeAttribute('srcset'); p.alt = info.name; }
show($('[data-hv-error]', d), false);
$$('.is-invalid', d).forEach(function (el) { el.classList.remove('is-invalid'); });
showView('form');
closeOverlay(lightbox());
openOverlay(d);
renderTurnstile();
}
function openPhoto(info) {
current = info;
var l = lightbox();
var img = $('.hv_lightbox-img', l); if (img) { img.src = info.photo; img.removeAttribute('srcset'); img.alt = info.name + ' decorated space'; }
$('[data-hv-lightbox-name]', l).textContent = info.name;
show($('[data-hv-lightbox-vote]', l), votingState() === 'open');
openOverlay(l);
}
// one listener for every click (works even if the Collection List renders late)
document.addEventListener('click', function (e) {
var t = e.target;
var vote = t.closest('[data-hv-vote]'), zoom = t.closest('[data-hv-zoom]');
if (vote) { e.preventDefault(); if (!vote.hasAttribute('data-disabled')) openVote(cardInfo(vote.closest('[data-hv-card]'))); return; }
if (zoom) { e.preventDefault(); openPhoto(cardInfo(zoom.closest('[data-hv-card]'))); return; }
if (t.closest('[data-hv-lightbox-vote]')) { e.preventDefault(); openVote(current); return; }
if (t.closest('[data-hv-close]')) { e.preventDefault(); closeOverlay(t.closest('.hv_overlay')); return; }
if (t.classList && t.classList.contains('hv_overlay')) closeOverlay(t); // click on the dark backdrop
});
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape') $$('.hv_overlay.is-open').forEach(closeOverlay);
var zoom = e.target.closest && e.target.closest('[data-hv-zoom]');
if (zoom && (e.key === 'Enter' || e.key === ' ')) { e.preventDefault(); openPhoto(cardInfo(zoom.closest('[data-hv-card]'))); }
});
// Turnstile (bot check), rendered into the box that carries data-sitekey
var tsWidget = null;
window.hvTurnstileReady = function () { renderTurnstile(); };
function renderTurnstile() {
var box = $('.hv_turnstile');
if (!box || !window.turnstile || tsWidget !== null) return;
tsWidget = window.turnstile.render(box, { sitekey: box.getAttribute('data-sitekey'), appearance: box.getAttribute('data-appearance') || 'interaction-only', theme: 'light' });
}
function turnstileToken() { try { return window.turnstile && tsWidget !== null ? window.turnstile.getResponse(tsWidget) || '' : ''; } catch (e) { return ''; } }
function resetTurnstile() { try { if (window.turnstile && tsWidget !== null) window.turnstile.reset(tsWidget); } catch (e) {} }
// keep the Vote button usable (Webflow can disable it); only our own "Sending…" state disables it
var sending = false;
function guardSubmit() {
var b = $('[data-hv-form] [type="submit"]');
if (!b || b.hvGuarded) return;
b.hvGuarded = true; b.disabled = false;
new MutationObserver(function () { if (b.disabled && !sending) b.disabled = false; }).observe(b, { attributes: true, attributeFilter: ['disabled'] });
}
// our submit runs first and stops Webflow's own form handler (votes go to the backend, not to Webflow forms)
document.addEventListener('submit', function (e) {
var form = e.target.closest && e.target.closest('[data-hv-form]');
if (!form) return;
e.preventDefault(); e.stopImmediatePropagation();
var get = function (n) { var el = form.querySelector('[name="' + n + '"]'); return el ? el : null; };
var bad = [];
['firstName', 'lastName', 'email'].forEach(function (n) {
var el = get(n), val = el ? el.value.trim() : '';
var ok = val && (n !== 'email' || /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(val));
if (el) el.classList.toggle('is-invalid', !ok);
if (!ok && el) bad.push(el);
});
var err = $('[data-hv-error]', form), btn = $('[data-hv-submit]', form) || form.querySelector('[type="submit"]');
if (bad.length) { show(err, true); err.style.display = 'block'; bad[0].focus(); return; }
show(err, false);
var label = btn ? btn.value : 'Vote';
sending = true;
if (btn) { btn.disabled = true; btn.value = 'Sending…'; }
sendVote({
merchant: current.slug, merchantName: current.name,
firstName: get('firstName').value.trim(), lastName: get('lastName').value.trim(),
email: get('email').value.trim(), optIn: !!(get('optIn') && get('optIn').checked),
turnstileToken: turnstileToken()
}).then(function (res) {
sending = false;
if (btn) { btn.disabled = false; btn.value = label; }
resetTurnstile();
if (res.ok) {
store.set('hv-voted', { day: todayET(), name: current.name });
var n = $('.hv_result-name', dialog()); if (n) n.textContent = current.name;
showView('success'); form.reset(); render();
} else if (res.reason === 'already') { showView('already'); }
else if (res.reason === 'closed') { closeOverlay(dialog()); render(); }
else { err.style.display = 'block'; err.textContent = res.reason === 'bot' ? 'We could not confirm you are not a robot. Please try again.' : 'Something went wrong. Please try again.'; }
});
}, true);
// QR code at each merchant: ?m=merch-12 opens the vote form for that merchant
function openFromLink() {
var m = new URLSearchParams(location.search).get('m');
if (!m) return;
var card = cards().filter(function (c) { return cardInfo(c).slug === m; })[0];
if (!card) return;
card.scrollIntoView({ block: 'center' });
card.classList.add('is-highlight');
if (votingState() === 'open') setTimeout(function () { openVote(cardInfo(card)); }, 350);
}
/* ---------- 5. PAGE STATES ---------- */
function render() {
var state = votingState(), voted = votedToday();
document.documentElement.setAttribute('data-voting', state);
var status = $('[data-hv-status]');
showAs(status, state !== 'open', 'block');
if (status && state === 'before') status.textContent = 'Voting opens November 28';
if (status && state === 'closed') status.textContent = 'Voting is closed. Thank you for voting!';
showAs($('[data-hv-cta-wrap]'), state === 'open', 'block');
var banner = $('[data-hv-voted-banner]');
showAs(banner, state === 'open' && !!voted, 'block');
if (banner && voted) banner.textContent = 'You voted for ' + voted.name + ' today. Come back tomorrow to vote again!';
$$('[data-hv-vote]').forEach(function (b) {
b.style.display = state === 'closed' ? 'none' : '';
if (state === 'before') { b.setAttribute('data-disabled', ''); b.textContent = 'Opens Nov 28'; b.style.opacity = '.55'; }
else { b.removeAttribute('data-disabled'); b.textContent = 'Vote'; b.style.opacity = ''; }
});
}
/* ---------- 6. GINGERBREAD MAN (same cookie as the Home page) ---------- */
var LIMBS = {
arm: '
TEST MODE · votes stay in this browser · day:
' + ['auto', 'before', 'open', 'closed'].map(function (s) { return ''; }).join('') + ''; document.body.appendChild(p); function sync() { $('[data-d]', p).textContent = todayET(); $$('[data-s]', p).forEach(function (b) { b.classList.toggle('is-on', b.getAttribute('data-s') === demo.state); }); } p.addEventListener('click', function (e) { var b = e.target.closest('button'); if (!b) return; if (b.dataset.s) demo.state = b.dataset.s; if (b.dataset.a === 'next') demo.dayOffset++; if (b.dataset.a === 'reset') { demo.dayOffset = 0; store.del('hv-demo-votes'); store.del('hv-voted'); } store.set('hv-demo', demo); sync(); render(); }); sync(); } /* ---------- start ---------- */ function start() { // Collection List items can appear a moment later (Webflow preview): wait for them if (!cards().length && (start.tries = (start.tries || 0) + 1) < 40) return setTimeout(start, 150); prepareCards(); guardSubmit(); mountDemo(); render(); mountWalker(); openFromLink(); } // load Turnstile once (reuse it if the page already has it) if (window.turnstile) setTimeout(renderTurnstile, 0); else if (!document.querySelector('script[src*="challenges.cloudflare.com/turnstile"]')) { var ts = document.createElement('script'); ts.src = 'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit&onload=hvTurnstileReady'; ts.async = true; ts.defer = true; document.head.appendChild(ts); } else { var wait = setInterval(function () { if (window.turnstile) { clearInterval(wait); renderTurnstile(); } }, 200); } if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', start); else start(); })();