
const BCN={phoneDisplay:'(844) 795-3838',phoneHref:'+18447953838'};
const ZIP_RANGES=[['00','06','Connecticut'],['07','08','New Jersey'],['10','14','New York'],['15','19','Pennsylvania'],['20','20','District of Columbia'],['21','21','Maryland'],['22','24','Virginia'],['25','26','West Virginia'],['27','28','North Carolina'],['29','29','South Carolina'],['30','31','Georgia'],['32','34','Florida'],['35','36','Alabama'],['37','38','Tennessee'],['39','39','Mississippi'],['40','42','Kentucky'],['43','45','Ohio'],['46','47','Indiana'],['48','49','Michigan'],['50','52','Iowa'],['53','54','Wisconsin'],['55','56','Minnesota'],['57','57','South Dakota'],['58','58','North Dakota'],['59','59','Montana'],['60','62','Illinois'],['63','65','Missouri'],['66','67','Kansas'],['68','69','Nebraska'],['70','71','Louisiana'],['72','72','Arkansas'],['73','74','Oklahoma'],['75','79','Texas'],['80','81','Colorado'],['82','83','Wyoming'],['84','84','Utah'],['85','86','Arizona'],['87','88','New Mexico'],['89','89','Nevada'],['90','96','California'],['97','97','Oregon'],['98','99','Washington']];
function stateFromZip(z){const n=z.slice(0,2);for(const [a,b,s] of ZIP_RANGES){if(n>=a&&n<=b)return s}return 'your area'}
function handleZip(form){const input=form.querySelector('input[name=zip],input#zip');const z=(input?.value||'').trim();const err=form.querySelector('.zip-error');if(!/^\d{5}$/.test(z)){if(err){err.textContent='Enter a valid 5-digit U.S. ZIP code.';err.classList.remove('hidden')}input?.focus();return false}localStorage.setItem('bcnZip',z);location.href=(form.dataset.results||'results.html')+'?zip='+encodeURIComponent(z);return false}
document.addEventListener('DOMContentLoaded',()=>{document.querySelectorAll('[data-zip-form]').forEach(f=>f.addEventListener('submit',e=>{e.preventDefault();handleZip(f)}));const menu=document.querySelector('.menu'),nav=document.querySelector('.nav');menu?.addEventListener('click',()=>{nav.classList.toggle('open');menu.setAttribute('aria-expanded',nav.classList.contains('open'))});document.querySelectorAll('[data-year]').forEach(x=>x.textContent=new Date().getFullYear());const qp=new URLSearchParams(location.search),z=qp.get('zip')||localStorage.getItem('bcnZip');if(z&&/^\d{5}$/.test(z)){document.querySelectorAll('[data-zip-value]').forEach(x=>x.textContent=z);document.querySelectorAll('[data-state-value]').forEach(x=>x.textContent=stateFromZip(z));document.querySelectorAll('input[data-prefill-zip]').forEach(x=>x.value=z)}initResults(z)});
const PLANS=[{provider:'AT&T Fiber',logo:'att-transparent-v2.png',type:'Fiber',speed:1000,price:55,contract:'No annual contract',cap:'No data cap'},{provider:'Xfinity',logo:'xfinity.png',type:'Cable',speed:1000,price:35,contract:'Terms vary',cap:'Plan-dependent'},{provider:'Frontier Fiber',logo:'frontier.png',type:'Fiber',speed:2000,price:45,contract:'No annual contract',cap:'No data cap'},{provider:'Spectrum',logo:'spectrum.png',type:'Cable',speed:1000,price:50,contract:'No annual contract',cap:'No data cap'},{provider:'Verizon Fios',logo:'verizon.png',type:'Fiber',speed:1000,price:60,contract:'No annual contract',cap:'No data cap'},{provider:'Cox',logo:'cox.png',type:'Cable',speed:1000,price:50,contract:'Terms vary',cap:'Plan-dependent'},{provider:'Hughesnet',logo:'hughesnet.png',type:'Satellite',speed:100,price:50,contract:'Term may apply',cap:'Priority-data allowance'}];
function initResults(z){const box=document.querySelector('#results-list');if(!box)return;const type=document.querySelector('#type-filter'),sort=document.querySelector('#sort-filter');function draw(){let p=[...PLANS];if(type.value!=='All')p=p.filter(x=>x.type===type.value);if(sort.value==='price')p.sort((a,b)=>a.price-b.price);if(sort.value==='speed')p.sort((a,b)=>b.speed-a.speed);box.innerHTML=p.map(x=>`<article class="card result-card"><img class="provider-logo" src="assets/provider-logos/${x.logo}" alt="${x.provider} logo"><div><span class="badge">${x.type}</span><h3>${x.provider}</h3><p><strong>Up to ${x.speed>=1000?x.speed/1000+' Gbps':x.speed+' Mbps'}</strong> · ${x.contract} · ${x.cap}</p><p class="micro">Illustrative nationally advertised plan. Availability and final price require provider confirmation for the service address.</p></div><div><div class="price">$${x.price}<small>/mo.*</small></div><a class="btn" href="tel:${BCN.phoneHref}">Call to verify</a></div></article>`).join('')}type?.addEventListener('change',draw);sort?.addEventListener('change',draw);draw()}

// Accessible mega-menu behavior
window.addEventListener('DOMContentLoaded',()=>{
  const groups=[...document.querySelectorAll('.nav-group')];
  groups.forEach(group=>{
    const btn=group.querySelector('.nav-link');
    btn?.addEventListener('click',e=>{
      e.stopPropagation();
      const opening=!group.classList.contains('open');
      groups.forEach(g=>{g.classList.remove('open');g.querySelector('.nav-link')?.setAttribute('aria-expanded','false')});
      if(opening){group.classList.add('open');btn.setAttribute('aria-expanded','true')}
    });
  });
  document.addEventListener('click',()=>groups.forEach(g=>{g.classList.remove('open');g.querySelector('.nav-link')?.setAttribute('aria-expanded','false')}));
  document.addEventListener('keydown',e=>{if(e.key==='Escape')groups.forEach(g=>{g.classList.remove('open');g.querySelector('.nav-link')?.setAttribute('aria-expanded','false')})});
});

// Illinois provider connection-type filters
document.addEventListener('DOMContentLoaded',()=>{const controls=document.querySelector('.illinois-filters');if(!controls)return;const rows=[...document.querySelectorAll('.illinois-provider-row')];controls.addEventListener('click',e=>{const btn=e.target.closest('button[data-filter]');if(!btn)return;controls.querySelectorAll('button').forEach(b=>b.classList.toggle('active',b===btn));const f=btn.dataset.filter;rows.forEach(row=>{const types=(row.dataset.types||'').split(/\s+/);row.hidden=f!=='all'&&!types.includes(f);});});});
