(function (){
'use strict';
var nav=document.querySelector('.rwh-nav');
var mega=nav&&nav.querySelector('.rwh-mega');
var ink=nav&&nav.querySelector('.rwh-ink');
var row=nav&&nav.querySelector('.rwh-nav__in');
var scrim=document.querySelector('[data-rwh-scrim]');
var current=null, tOpen, tClose;
function moveInk(el){
if(!ink){ return; }
if(!el){ ink.style.opacity=0; return; }
var r=el.getBoundingClientRect(), p=row.getBoundingClientRect();
ink.style.left=(r.left - p.left + 10) + 'px';
ink.style.width=Math.max(0, r.width - 20) + 'px';
ink.style.opacity=1;
}
function panelFor(key){ return mega.querySelector('.rwh-panel[data-rwh-panel="' + key + '"]'); }
function fit(){ var on=mega.querySelector('.rwh-panel:not([hidden])'); mega.style.height=on ? on.offsetHeight + 'px':0; }
function open(btn){
var key=btn.getAttribute('data-rwh-panel');
nav.querySelectorAll('.rwh-ni[data-rwh-panel]').forEach(function (b){ b.setAttribute('aria-expanded', String(b===btn)); });
mega.querySelectorAll('.rwh-panel').forEach(function (p){ p.hidden=p.getAttribute('data-rwh-panel')!==key; });
fit();
if(scrim){ scrim.classList.add('is-on'); }
current=btn;
moveInk(btn);
}
function close(keepInk){
if(!nav){ return; }
nav.querySelectorAll('.rwh-ni[data-rwh-panel]').forEach(function (b){ b.setAttribute('aria-expanded', 'false'); });
mega.style.height=0;
if(scrim){ scrim.classList.remove('is-on'); }
current=null;
if(!keepInk){ moveInk(null); }
setTimeout(function (){ if(!current){ mega.querySelectorAll('.rwh-panel').forEach(function (p){ p.hidden=true; });}}, 360);
}
if(nav){
nav.querySelectorAll('.rwh-ni').forEach(function (el){
el.addEventListener('mouseenter', function (){
clearTimeout(tClose); clearTimeout(tOpen);
if(el.hasAttribute('data-rwh-panel')){ tOpen=setTimeout(function (){ open(el); }, current ? 0:110); }else{ moveInk(el); tOpen=setTimeout(function (){ close(true); }, 150); }});
if(el.hasAttribute('data-rwh-panel')){
el.addEventListener('click', function (){ if(current===el){ close(); }else{ open(el); }});
el.addEventListener('keydown', function (e){
if(e.key==='ArrowDown'){ e.preventDefault(); open(el); var f=panelFor(el.getAttribute('data-rwh-panel')).querySelector('a,button'); if(f){ f.focus(); }}
});
}});
nav.addEventListener('mouseleave', function (){ clearTimeout(tOpen); tClose=setTimeout(function (){ close(); }, 220); });
nav.addEventListener('mouseenter', function (){ clearTimeout(tClose); });
nav.querySelectorAll('.rwh-rail button').forEach(function (b){
function go(){
var k=b.getAttribute('data-rwh-sec'), panel=b.closest('.rwh-panel');
panel.querySelectorAll('.rwh-rail button').forEach(function (x){ x.classList.toggle('is-on', x===b); });
panel.querySelectorAll('.rwh-subs').forEach(function (x){ x.classList.toggle('is-on', x.getAttribute('data-rwh-sec')===k); });
fit();
}
b.addEventListener('mouseenter', go); b.addEventListener('focus', go); b.addEventListener('click', go);
});
window.addEventListener('resize', function (){ if(current){ open(current); }});
}
if(scrim){ scrim.addEventListener('click', function (){ close(); closeDrawer(); });}
document.querySelectorAll('.rwh-dd > button').forEach(function (b){
b.addEventListener('click', function (){
var dd=b.parentNode, on = !dd.classList.contains('is-open');
document.querySelectorAll('.rwh-dd.is-open').forEach(function (x){ x.classList.remove('is-open'); x.firstElementChild.setAttribute('aria-expanded', 'false'); });
dd.classList.toggle('is-open', on); b.setAttribute('aria-expanded', String(on));
});
});
document.addEventListener('click', function (e){
if(!e.target.closest('.rwh-dd')){ document.querySelectorAll('.rwh-dd.is-open').forEach(function (x){ x.classList.remove('is-open'); x.firstElementChild.setAttribute('aria-expanded', 'false'); });}});
var band=Array.prototype.slice.call(document.querySelectorAll('.rwh-band span'));
var still=window.matchMedia('(prefers-reduced-motion: reduce)').matches;
band.forEach(function (sp){ if(band.length > 1){ var t=document.createElement('i'); t.className='rwh-band__t'; t.style.fontStyle='normal'; while (sp.firstChild){ t.appendChild(sp.firstChild); } sp.appendChild(t); }});
function measure(sp){
var t=sp.firstChild, over=t.scrollWidth - (sp.clientWidth - 24);
sp.classList.toggle('is-long', over > 4&&!still);
if(over > 4){ sp.style.setProperty('--rwh-x', (-over) + 'px'); sp.style.setProperty('--rwh-dur', Math.max(6, over / 28 + 3) + 's'); }
return over > 4&&!still ? Math.max(6, over / 28 + 3) * 1000 + 800:4200;
}
if(band.length > 1){
var bi=0;
var step=function (){
var wait=measure(band[bi]);
var t=band[bi].firstChild; t.style.animation='none'; void t.offsetWidth; t.style.animation='';
if(band.length < 2&&wait===4200){ return; }
setTimeout(function (){
if(band.length < 2){ step(); return; }
var a=band[bi]; a.classList.remove('is-on'); a.classList.add('is-out');
bi=(bi + 1) % band.length;
var b=band[bi]; b.classList.remove('is-out'); void b.offsetWidth; b.classList.add('is-on');
setTimeout(function (){ a.classList.remove('is-out'); }, 520);
step();
}, wait);
};
if(!still||band.length > 1){ step(); }}
var drawer=document.getElementById('rwh-drawer');
var burger=document.querySelector('[data-rwh-open]');
var stack=['root'];
function lvl(id){ return drawer.querySelector('[data-rwh-lvl="' + id + '"]'); }
function show(id, back){
var cur=lvl(stack[stack.length - 1]), nxt=lvl(id);
if(!nxt){ return; }
if(back){ stack.pop(); cur.classList.add('is-off'); nxt.classList.remove('is-back'); }else{ stack.push(id); nxt.style.zIndex=stack.length; cur.classList.add('is-back'); nxt.classList.remove('is-off'); }}
function openDrawer(){
if(!drawer){ return; }
drawer.classList.add('is-on'); drawer.setAttribute('aria-hidden', 'false');
if(burger){ burger.setAttribute('aria-expanded', 'true'); }
if(scrim){ scrim.classList.add('is-on'); }
document.body.classList.add('rwh-locked');
}
function closeDrawer(){
if(!drawer||!drawer.classList.contains('is-on')){ return; }
drawer.classList.remove('is-on'); drawer.setAttribute('aria-hidden', 'true');
if(burger){ burger.setAttribute('aria-expanded', 'false'); burger.focus(); }
if(scrim&&!current){ scrim.classList.remove('is-on'); }
document.body.classList.remove('rwh-locked');
}
if(drawer){
if(burger){ burger.addEventListener('click', openDrawer); }
drawer.querySelector('[data-rwh-close]').addEventListener('click', closeDrawer);
drawer.addEventListener('click', function (e){
var g=e.target.closest('[data-rwh-go]'), b=e.target.closest('[data-rwh-back]');
if(g){ show(g.getAttribute('data-rwh-go')); }else if(b){ show(b.getAttribute('data-rwh-back'), true); }});
}
var root=document.documentElement, ticking=false;
window.addEventListener('scroll', function (){
if(ticking){ return; }
ticking=true;
requestAnimationFrame(function (){ root.classList.toggle('rwh-scrolled', window.scrollY > 120); ticking=false; });
}, { passive: true });
var so=document.getElementById('rwh-so');
var mobile=window.matchMedia('(max-width: 1023px)');
if(so){
var soQ=so.querySelector('[data-rwh-so-q]'), soClear=so.querySelector('[data-rwh-so-clear]');
var live=so.querySelector('[data-rwh-so-live]'), deflt=so.querySelector('[data-rwh-so-default]');
var catsBox=so.querySelector('[data-rwh-so-cats]'), prods=so.querySelector('[data-rwh-so-prods]');
var ptitle=so.querySelector('[data-rwh-so-ptitle]'), msg=so.querySelector('[data-rwh-so-msg]'), all=so.querySelector('[data-rwh-so-all]');
var api=so.getAttribute('data-api'), searchUrl=so.getAttribute('data-search');
var cats=[]; try { cats=JSON.parse(so.getAttribute('data-cats')||'[]'); } catch (e){ cats=[]; }
var timer, ctrl, lastFocus, scrollY0=0;
var decode=function (html){ var t=document.createElement('textarea'); t.innerHTML=html; return t.value; };
var norm=function (t){ return t.toLowerCase().normalize('NFD').replace(/[̀-ͯ]/g, ''); };
function el(tag, cls, text){ var n=document.createElement(tag); if(cls){ n.className=cls; } if(text!=null){ n.textContent=text; } return n; }
function highlight(name, q){
var b=el('b'), i=norm(name).indexOf(norm(q));
if(q.length < 2||i < 0){ b.textContent=name; return b; }
b.appendChild(document.createTextNode(name.slice(0, i)));
b.appendChild(el('mark', null, name.slice(i, i + q.length)));
b.appendChild(document.createTextNode(name.slice(i + q.length)));
return b;
}
function price(p){
if(!p||!p.price){ return ''; }
var v=Number(p.price) / Math.pow(10, p.currency_minor_unit||2);
return '€ ' + v.toFixed(2).replace('.', ',');
}
function allLink(q){ return searchUrl + (searchUrl.indexOf('?') < 0 ? '?':'&') + 's=' + encodeURIComponent(q); }
function render(q, items){
prods.textContent='';
items.forEach(function (it){
var a=el('a', 'rwh-so__row'); a.href=it.permalink;
var img=el('img'); img.alt=''; img.decoding='async';
var im=it.images&&it.images[0]; if(im){ img.src=im.thumbnail||im.src; }
var t=el('span'); t.appendChild(highlight(decode(it.name), q)); t.appendChild(document.createTextNode(price(it.prices)));
a.appendChild(img); a.appendChild(t); prods.appendChild(a);
});
ptitle.hidden = !items.length;
msg.textContent=items.length||catsBox.childNodes.length ? '':'Geen producten gevonden voor “' + q + '”. Probeer een ander woord, zoals jas, broek of helm.';
all.textContent=items.length ? 'Bekijk alle resultaten voor “' + q + '”':'';
all.href=allLink(q);
deflt.hidden = !!(items.length||catsBox.childNodes.length);
}
function renderCats(q){
catsBox.textContent='';
var nq=norm(q), hits=cats.filter(function (c){ return norm(c.n).indexOf(nq) > -1; }).sort(function (a, b){ return b.c - a.c; }).slice(0, 4);
if(!hits.length){ return; }
catsBox.appendChild(el('h6', null, 'Categorieën'));
hits.forEach(function (c){
var a=el('a', 'rwh-so__row rwh-so__row--cat'); a.href=c.u;
a.appendChild(highlight(c.n, q)); a.appendChild(el('span', null, c.c + ' producten'));
catsBox.appendChild(a);
});
}
function search(){
var q=soQ.value.trim();
soClear.hidden = !q;
if(q.length < 2){ live.hidden=true; deflt.hidden=false; return; }
deflt.hidden=true; live.hidden=false;
renderCats(q);
ptitle.hidden=false; msg.textContent=''; all.textContent='';
prods.innerHTML='<div class="rwh-so__skel"></div><div class="rwh-so__skel"></div><div class="rwh-so__skel"></div>';
if(ctrl){ ctrl.abort(); }
ctrl=window.AbortController ? new AbortController():null;
fetch(api + '?search=' + encodeURIComponent(q) + '&per_page=6', { signal: ctrl ? ctrl.signal:undefined, credentials: 'same-origin' })
.then(function (r){ if(!r.ok){ throw new Error(r.status); } return r.json(); })
.then(function (items){ if(soQ.value.trim()===q){ render(q, Array.isArray(items) ? items:[]); }})
.catch(function (e){
if(e.name==='AbortError'){ return; }
prods.textContent=''; ptitle.hidden=true;
msg.textContent='Zoeken lukt nu even niet. Druk op Zoeken op je toetsenbord om alle resultaten te zien.';
});
}
function openSo(prefill){
lastFocus=document.activeElement;
so.hidden=false;
scrollY0=window.scrollY;
document.body.classList.add('rwh-locked');
if(typeof prefill==='string'){ soQ.value=prefill; }
soQ.focus();
search();
}
function closeSo(){
if(so.hidden){ return; }
so.hidden=true;
document.body.classList.remove('rwh-locked');
window.scrollTo(0, scrollY0);
if(lastFocus&&lastFocus.focus){ lastFocus.focus({ preventScroll: true });}}
soQ.addEventListener('input', function (){ clearTimeout(timer); timer=setTimeout(search, 220); });
soClear.addEventListener('click', function (){ soQ.value=''; soQ.focus(); search(); });
so.querySelector('[data-rwh-so-close]').addEventListener('click', closeSo);
so.querySelector('form').addEventListener('submit', function (e){ if(!soQ.value.trim()){ e.preventDefault(); }});
document.addEventListener('keydown', function (e){ if(e.key==='Escape'){ closeSo(); }});
document.querySelectorAll('[data-rwh-so-open]').forEach(function (b){ b.addEventListener('click', function (){ openSo(); });});
var q=document.querySelector('[data-rwh-q]');
if(q){
q.addEventListener('focus', function (){
if(!mobile.matches){ return; }
var v=q.value; q.blur(); openSo(v);
});
}}
document.addEventListener('keydown', function (e){
if(e.key!=='Escape'){ return; }
var c=current; close(); if(c){ c.focus(); }
closeDrawer();
});
document.documentElement.classList.add('rwh-js-ready');
})();