// JavaScript Document
(function(){
  // Sticky nav shadow
  var nav=document.getElementById('nav');
  window.addEventListener('scroll',function(){nav.classList.toggle('scrolled',window.scrollY>10)});

  // Mobile menu
  var burger=document.getElementById('burger'),menu=document.getElementById('menu');
  burger.addEventListener('click',function(){menu.classList.toggle('mobile-open');burger.classList.toggle('open')});
  menu.querySelectorAll('a').forEach(function(a){a.addEventListener('click',function(){menu.classList.remove('mobile-open');burger.classList.remove('open')})});

  // Reveal on scroll
  var io=new IntersectionObserver(function(es){es.forEach(function(e){if(e.isIntersecting){e.target.classList.add('in');io.unobserve(e.target)}})},{threshold:.12});
  document.querySelectorAll('.reveal').forEach(function(el){io.observe(el)});

  // Testimonials carousel
  var track=document.getElementById('ttrack'),slides=track.children.length,idx=0,timer;
  var dotsWrap=document.getElementById('tdots');
  for(var i=0;i<slides;i++){var b=document.createElement('button');b.className='tdot'+(i===0?' active':'');b.dataset.i=i;b.setAttribute('aria-label','Go to testimonial '+(i+1));dotsWrap.appendChild(b)}
  var dots=dotsWrap.children;
  function go(n){idx=(n+slides)%slides;track.style.transform='translateX(-'+(idx*100)+'%)';for(var j=0;j<dots.length;j++)dots[j].classList.toggle('active',j===idx)}
  function auto(){timer=setInterval(function(){go(idx+1)},5500)}
  function reset(){clearInterval(timer);auto()}
  document.getElementById('tnext').addEventListener('click',function(){go(idx+1);reset()});
  document.getElementById('tprev').addEventListener('click',function(){go(idx-1);reset()});
  dotsWrap.addEventListener('click',function(e){if(e.target.dataset.i){go(+e.target.dataset.i);reset()}});
  var car=document.querySelector('.tcar');
  car.addEventListener('mouseenter',function(){clearInterval(timer)});
  car.addEventListener('mouseleave',auto);
  auto();

  // FAQ tabs
  var tabs=document.querySelectorAll('.faq-tab'),panels=document.querySelectorAll('.faq-panel');
  tabs.forEach(function(t){t.addEventListener('click',function(){
    var i=+t.dataset.i;
    tabs.forEach(function(x){x.classList.remove('active')});
    panels.forEach(function(x){x.classList.remove('active')});
    t.classList.add('active');panels[i].classList.add('active');
  })});

  // Lightbox
  var lb=document.getElementById('lightbox'),lbImg=document.getElementById('lbImg'),lbCap=document.getElementById('lbCap');
  document.getElementById('photoGrid').addEventListener('click',function(e){
    var item=e.target.closest('.grid-item');if(!item||item.classList.contains('failed'))return;
    var img=item.querySelector('img');if(!img)return;
    lbImg.src=img.src;lbImg.alt=img.alt;lbCap.textContent=item.dataset.cap||img.alt;lb.classList.add('open');
  });
  function closeLb(){lb.classList.remove('open')}
  document.getElementById('lbClose').addEventListener('click',closeLb);
  lb.addEventListener('click',function(e){if(e.target===lb)closeLb()});
  document.addEventListener('keydown',function(e){if(e.key==='Escape')closeLb()});
})();

const obs=new IntersectionObserver((entries)=>{
  entries.forEach(e=>{if(e.isIntersecting){e.target.classList.add('visible');obs.unobserve(e.target);}});
},{threshold:.1});
document.querySelectorAll('.reveal').forEach(el=>obs.observe(el));

function toggleAcc(q){
  const item=q.parentElement;
  const open=item.classList.contains('open');
  document.querySelectorAll('.acc-item').forEach(i=>i.classList.remove('open'));
  if(!open)item.classList.add('open');
}

function openModal(type){
  document.getElementById(type==='apply'?'applyModal':'brochureModal').classList.add('open');
  document.body.style.overflow='hidden';
}
function closeModal(type){
  document.getElementById(type==='apply'?'applyModal':'brochureModal').classList.remove('open');
  document.body.style.overflow='';
}
document.querySelectorAll('.modal-overlay').forEach(o=>{
  o.addEventListener('click',function(e){if(e.target===this){this.classList.remove('open');document.body.style.overflow='';}});
});
document.addEventListener('keydown',e=>{
  if(e.key==='Escape'){document.querySelectorAll('.modal-overlay').forEach(o=>o.classList.remove('open'));document.body.style.overflow='';}
});
function handleApply(e){
  e.preventDefault();
  const btn=e.target.querySelector('button[type=submit]');
  btn.textContent='✓ Submitted! We\'ll call you within 24 hours.';
  btn.style.background='#1A6B3C';btn.style.color='#fff';
  setTimeout(()=>closeModal('apply'),2400);
}
function handleBrochure(e){
  e.preventDefault();
  const btn=e.target.querySelector('button[type=submit]');
  btn.textContent='✓ Brochure sent to your email!';
  btn.style.background='#1A6B3C';btn.style.color='#fff';
  setTimeout(()=>closeModal('brochure'),2400);
}
function openWA(){
  window.open('https://wa.me/919830000000?text=Hi%2C%20I%27m%20interested%20in%20the%20International%20Teaching%20Diploma%20(ITD)','_blank');
}

