document.addEventListener('DOMContentLoaded',function(){document.querySelectorAll('[data-tabs]').forEach(function(tabGroup){var buttons=tabGroup.querySelectorAll('.ap-tabs__btn');var panels=tabGroup.querySelectorAll('.ap-tabs__panel');buttons.forEach(function(btn){btn.addEventListener('click',function(){var target=btn.getAttribute('data-tab-target');buttons.forEach(function(b){b.classList.remove('is-active');b.setAttribute('aria-selected','false');});panels.forEach(function(p){p.hidden=true;});btn.classList.add('is-active');btn.setAttribute('aria-selected','true');var panel=tabGroup.querySelector('#'+target);if(panel)panel.hidden=false;});});});document.querySelectorAll('.ap-accordion:not([data-multi="true"])').forEach(function(acc){var details=acc.querySelectorAll('details');details.forEach(function(d){d.addEventListener('toggle',function(){if(d.open){details.forEach(function(other){if(other!==d)other.removeAttribute('open');});}});});});if('IntersectionObserver'in window){var animated=document.querySelectorAll('[data-animation]');if(animated.length>0){var observer=new IntersectionObserver(function(entries){entries.forEach(function(entry){if(entry.isIntersecting){entry.target.style.animationPlayState='running';observer.unobserve(entry.target);}});},{threshold:0.1});animated.forEach(function(el){el.style.animationPlayState='paused';observer.observe(el);});}}
document.querySelectorAll('form[data-form-submit]').forEach(function(form){form.addEventListener('submit',function(e){e.preventDefault();var formId=form.getAttribute('data-form-submit');var submitBtn=form.querySelector('.ap-form__submit');var msgEl=form.closest('.ap-form').querySelector('.ap-form__message');var originalText=submitBtn?submitBtn.textContent:'';var msgSuccess=form.getAttribute('data-msg-success')||'Thank you!';var msgError=form.getAttribute('data-msg-error')||'Something went wrong.';var msgNetwork=form.getAttribute('data-msg-network')||'Network error. Please try again.';if(submitBtn){submitBtn.disabled=true;submitBtn.textContent='...';}
if(msgEl){msgEl.style.display='none';msgEl.className='ap-form__message';}
var formData=new FormData(form);fetch('/api/forms/'+formId+'/submit',{method:'POST',body:formData}).then(function(r){return r.json();}).then(function(data){if(data.success){if(msgEl){msgEl.textContent=data.message||msgSuccess;msgEl.className='ap-form__message ap-form__message--success';msgEl.style.display='block';}
form.reset();form.querySelectorAll('.ap-form__star').forEach(function(s){s.classList.remove('is-active');});if(data.redirect){setTimeout(function(){window.location.href=data.redirect;},1500);}}else{var errMsg=data.errors?data.errors.join('. '):(data.error||msgError);if(msgEl){msgEl.textContent=errMsg;msgEl.className='ap-form__message ap-form__message--error';msgEl.style.display='block';}}}).catch(function(){if(msgEl){msgEl.textContent=msgNetwork;msgEl.className='ap-form__message ap-form__message--error';msgEl.style.display='block';}}).finally(function(){if(submitBtn){submitBtn.disabled=false;submitBtn.textContent=originalText;}});});});document.querySelectorAll('.ap-form__rating').forEach(function(ratingGroup){var stars=Array.prototype.slice.call(ratingGroup.querySelectorAll('.ap-form__star'));var radios=Array.prototype.slice.call(ratingGroup.querySelectorAll('input[type="radio"]'));if(!radios.length)return;function current(){for(var i=0;i<radios.length;i++){if(radios[i].checked)return parseInt(radios[i].value,10);}return 0;}function paint(val,cls){stars.forEach(function(s,i){s.classList.toggle(cls,(i+1)<=val);});}ratingGroup.addEventListener('change',function(){paint(current(),'is-active');});stars.forEach(function(s,i){s.addEventListener('mouseenter',function(){paint(i+1,'is-hover');});s.addEventListener('mouseleave',function(){paint(0,'is-hover');});});paint(current(),'is-active');});document.querySelectorAll('[data-slideshow]').forEach(function(el){var slides=el.querySelectorAll('.ap-slideshow__slide');var dots=el.querySelectorAll('.ap-slideshow__dot');var prevBtn=el.querySelector('.ap-slideshow__arrow--prev');var nextBtn=el.querySelector('.ap-slideshow__arrow--next');var animation=el.getAttribute('data-animation')||'slide';var autoplay=el.getAttribute('data-autoplay')==='true';var interval=parseInt(el.getAttribute('data-interval'))||5;var current=0;var total=slides.length;var timer=null;var isTransitioning=false;if(total<=1)return;function goTo(index,direction){if(isTransitioning||index===current)return;isTransitioning=true;var prev=current;current=(index+total)%total;var dir=direction||(current>prev?'left':'right');if(animation==='slide'){slides[prev].classList.remove('is-active');slides[prev].classList.add(dir==='left'?'is-exiting-left':'is-exiting-right');slides[current].style.transform=dir==='left'?'translateX(60px)':'translateX(-60px)';slides[current].classList.add('is-active');void slides[current].offsetWidth;slides[current].style.transform='';setTimeout(function(){slides[prev].classList.remove('is-exiting-left','is-exiting-right');isTransitioning=false;},620);}else{slides[prev].classList.remove('is-active');slides[current].classList.add('is-active');setTimeout(function(){isTransitioning=false;},620);}
dots.forEach(function(d,i){d.classList.toggle('is-active',i===current);});}
function next(){goTo(current+1,'left');}
function prev(){goTo(current-1,'right');}
function startAutoplay(){stopAutoplay();if(autoplay){timer=setInterval(next,interval*1000);}}
function stopAutoplay(){if(timer){clearInterval(timer);timer=null;}}
if(nextBtn)nextBtn.addEventListener('click',function(){stopAutoplay();next();startAutoplay();});if(prevBtn)prevBtn.addEventListener('click',function(){stopAutoplay();prev();startAutoplay();});dots.forEach(function(dot){dot.addEventListener('click',function(){var idx=parseInt(dot.getAttribute('data-slide-index'));stopAutoplay();goTo(idx,idx>current?'left':'right');startAutoplay();});});var touchStartX=0;var touchEndX=0;el.addEventListener('touchstart',function(e){touchStartX=e.changedTouches[0].screenX;},{passive:true});el.addEventListener('touchend',function(e){touchEndX=e.changedTouches[0].screenX;var diff=touchStartX-touchEndX;if(Math.abs(diff)>50){stopAutoplay();if(diff>0)next();else prev();startAutoplay();}},{passive:true});el.addEventListener('mouseenter',stopAutoplay);el.addEventListener('mouseleave',startAutoplay);el.setAttribute('tabindex','0');el.addEventListener('keydown',function(e){if(e.key==='ArrowLeft'){stopAutoplay();prev();startAutoplay();}
if(e.key==='ArrowRight'){stopAutoplay();next();startAutoplay();}});startAutoplay();});var lbOverlay=null,lbItems=[],lbIndex=0;
var LB_ICON_PREV='<svg viewBox="0 0 24 24" width="26" height="26" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><polyline points="15 18 9 12 15 6"/></svg>';
var LB_ICON_NEXT='<svg viewBox="0 0 24 24" width="26" height="26" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><polyline points="9 18 15 12 9 6"/></svg>';
function lbEsc(s){return String(s).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;');}
document.querySelectorAll('[data-lightbox]').forEach(function(gallery){
var links=Array.prototype.slice.call(gallery.querySelectorAll('a[href]'));
if(!links.length)return;
var labels={prev:gallery.getAttribute('data-lb-prev')||'Previous image',next:gallery.getAttribute('data-lb-next')||'Next image',close:gallery.getAttribute('data-lb-close')||'Close'};
var items=links.map(function(l){return{src:l.href,caption:l.getAttribute('data-caption')||''};});
links.forEach(function(link,i){link.addEventListener('click',function(e){e.preventDefault();openLightbox(items,i,labels);});});
});
function openLightbox(items,index,labels){
if(lbOverlay)closeLightbox();
lbItems=items;
var multi=items.length>1;
lbOverlay=document.createElement('div');
lbOverlay.className='ap-lightbox';
lbOverlay.innerHTML='<div class="ap-lightbox__backdrop"></div>'
+'<div class="ap-lightbox__content"><img src="" alt=""><p class="ap-lightbox__caption" hidden></p></div>'
+(multi?'<button type="button" class="ap-lightbox__arrow ap-lightbox__arrow--prev" aria-label="'+lbEsc(labels.prev)+'">'+LB_ICON_PREV+'</button>'
+'<button type="button" class="ap-lightbox__arrow ap-lightbox__arrow--next" aria-label="'+lbEsc(labels.next)+'">'+LB_ICON_NEXT+'</button>':'')
+'<button type="button" class="ap-lightbox__close" aria-label="'+lbEsc(labels.close)+'">&times;</button>';
document.body.appendChild(lbOverlay);
document.body.style.overflow='hidden';
lbOverlay.querySelector('.ap-lightbox__backdrop').addEventListener('click',closeLightbox);
lbOverlay.querySelector('.ap-lightbox__close').addEventListener('click',closeLightbox);
if(multi){
lbOverlay.querySelector('.ap-lightbox__arrow--prev').addEventListener('click',function(){lbShow(lbIndex-1);});
lbOverlay.querySelector('.ap-lightbox__arrow--next').addEventListener('click',function(){lbShow(lbIndex+1);});
var lbTouchX=0;
lbOverlay.addEventListener('touchstart',function(e){lbTouchX=e.changedTouches[0].screenX;},{passive:true});
lbOverlay.addEventListener('touchend',function(e){var diff=lbTouchX-e.changedTouches[0].screenX;if(Math.abs(diff)>50){if(diff>0)lbShow(lbIndex+1);else lbShow(lbIndex-1);}},{passive:true});
}
document.addEventListener('keydown',lbKeyHandler);
lbShow(index);
requestAnimationFrame(function(){lbOverlay.classList.add('is-open');});
}
function lbShow(i){
if(!lbOverlay||!lbItems.length)return;
var n=lbItems.length;
lbIndex=((i%n)+n)%n;
var item=lbItems[lbIndex];
var img=lbOverlay.querySelector('.ap-lightbox__content img');
var cap=lbOverlay.querySelector('.ap-lightbox__caption');
if(img)img.src=item.src;
if(cap){if(item.caption){cap.textContent=item.caption;cap.hidden=false;}else{cap.textContent='';cap.hidden=true;}}
if(n>1){new Image().src=lbItems[(lbIndex+1)%n].src;new Image().src=lbItems[(lbIndex-1+n)%n].src;}
}
function closeLightbox(){if(!lbOverlay)return;document.body.style.overflow='';document.removeEventListener('keydown',lbKeyHandler);lbOverlay.remove();lbOverlay=null;lbItems=[];lbIndex=0;}
function lbKeyHandler(e){
if(e.key==='Escape'){closeLightbox();return;}
if(lbItems.length>1){if(e.key==='ArrowLeft'){lbShow(lbIndex-1);}else if(e.key==='ArrowRight'){lbShow(lbIndex+1);}}
}
document.querySelectorAll('[data-video-facade]').forEach(function(item){item.addEventListener('click',function(e){e.preventDefault();var src=item.getAttribute('data-video-src');var caption=item.querySelector('figcaption');var capText=caption?caption.textContent:'';var host=item.closest('[data-lb-close]');var closeLabel=host?host.getAttribute('data-lb-close'):'Close';if(src)openVideoLightbox(src,capText,closeLabel);});});
function openVideoLightbox(iframeSrc,caption,closeLabel){if(lbOverlay)closeLightbox();lbItems=[];lbOverlay=document.createElement('div');lbOverlay.className='ap-lightbox';lbOverlay.innerHTML='<div class="ap-lightbox__backdrop"></div>'+'<div class="ap-lightbox__content"><iframe src="'+lbEsc(iframeSrc)+'" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>'+(caption?'<p class="ap-lightbox__caption"></p>':'')+'</div>'+'<button type="button" class="ap-lightbox__close" aria-label="'+lbEsc(closeLabel||'Close')+'">&times;</button>';if(caption){lbOverlay.querySelector('.ap-lightbox__caption').textContent=caption;}
document.body.appendChild(lbOverlay);document.body.style.overflow='hidden';lbOverlay.querySelector('.ap-lightbox__backdrop').addEventListener('click',closeLightbox);lbOverlay.querySelector('.ap-lightbox__close').addEventListener('click',closeLightbox);document.addEventListener('keydown',lbKeyHandler);requestAnimationFrame(function(){lbOverlay.classList.add('is-open');});}});