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=ratingGroup.querySelectorAll('.ap-form__star');var hiddenInput=ratingGroup.querySelector('input[type="hidden"]');stars.forEach(function(star){star.addEventListener('click',function(){var val=parseInt(star.getAttribute('data-value'));if(hiddenInput)hiddenInput.value=val;stars.forEach(function(s){var sv=parseInt(s.getAttribute('data-value'));s.classList.toggle('is-active',sv<=val);});});star.addEventListener('mouseenter',function(){var val=parseInt(star.getAttribute('data-value'));stars.forEach(function(s){var sv=parseInt(s.getAttribute('data-value'));s.classList.toggle('is-hover',sv<=val);});});star.addEventListener('mouseleave',function(){stars.forEach(function(s){s.classList.remove('is-hover');});});});});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;document.querySelectorAll('[data-lightbox] a').forEach(function(link){link.addEventListener('click',function(e){e.preventDefault();openLightbox(link.href,link.getAttribute('data-caption')||'');});});function openLightbox(src,caption){if(lbOverlay)closeLightbox();lbOverlay=document.createElement('div');lbOverlay.className='ap-lightbox';lbOverlay.innerHTML='<div class="ap-lightbox__backdrop"></div>'+'<div class="ap-lightbox__content">'+'<img src="'+src+'" alt="">'+
(caption?'<p class="ap-lightbox__caption">'+caption+'</p>':'')+'</div>'+'<button class="ap-lightbox__close" aria-label="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);document.addEventListener('keydown',lbKeyHandler);requestAnimationFrame(function(){lbOverlay.classList.add('is-open');});}
function closeLightbox(){if(!lbOverlay)return;document.body.style.overflow='';document.removeEventListener('keydown',lbKeyHandler);lbOverlay.remove();lbOverlay=null;}
function lbKeyHandler(e){if(e.key==='Escape')closeLightbox();}
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:'';if(src)openVideoLightbox(src,capText);});});function openVideoLightbox(iframeSrc,caption){if(lbOverlay)closeLightbox();lbOverlay=document.createElement('div');lbOverlay.className='ap-lightbox';lbOverlay.innerHTML='<div class="ap-lightbox__backdrop"></div>'+'<div class="ap-lightbox__content">'+'<iframe src="'+iframeSrc+'" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>'+
(caption?'<p class="ap-lightbox__caption">'+caption+'</p>':'')+'</div>'+'<button class="ap-lightbox__close" aria-label="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);document.addEventListener('keydown',lbKeyHandler);requestAnimationFrame(function(){lbOverlay.classList.add('is-open');});}});