
(function(){
  function letters(el){
    if(!el || el.dataset.lettersReady==='1') return;
    var text=(el.textContent||'NOTORIOUS').trim();
    el.textContent='';
    Array.prototype.forEach.call(text, function(ch,i){
      var span=document.createElement('span');
      span.textContent=ch===' ' ? '\u00A0' : ch;
      span.style.animationDelay=(i*0.14)+'s';
      el.appendChild(span);
    });
    el.dataset.lettersReady='1';
  }
  function ensureLoader(){
    var loader=document.querySelector('.site-loader');
    if(!loader){
      loader=document.createElement('div');
      loader.className='site-loader';
      loader.innerHTML='<div><div class="loader-logo">NOTORIOUS</div><div class="loader-line"></div><div class="loader-small">Market & Coffee · 24/7</div></div>';
      document.body.appendChild(loader);
    }
    var logo=loader.querySelector('.loader-logo');
    if(logo){logo.dataset.lettersReady=''; logo.textContent='NOTORIOUS'; letters(logo);}
    return loader;
  }
  function hideLoader(delay){
    var loader=document.querySelector('.site-loader');
    if(!loader) return;
    setTimeout(function(){loader.classList.add('is-hiding'); setTimeout(function(){ if(loader && loader.parentNode) loader.remove(); }, 800);}, delay||3000);
  }
  function initSlider(){
    document.querySelectorAll('.smooth-only-slider').forEach(function(slider){
      if(slider.dataset.smoothReady==='1') return;
      slider.dataset.smoothReady='1';
      var slides=Array.prototype.slice.call(slider.querySelectorAll('.slide'));
      if(!slides.length) return;
      var i=0;
      slides.forEach(function(s,idx){s.classList.toggle('active',idx===0);});
      setInterval(function(){
        slides[i].classList.remove('active');
        i=(i+1)%slides.length;
        slides[i].classList.add('active');
      }, 5200);
    });
  }
  function initLinks(){
    document.addEventListener('click',function(e){
      var a=e.target.closest && e.target.closest('a[href]');
      if(!a) return;
      var href=a.getAttribute('href')||'';
      if(!href || href[0]==='#' || href.startsWith('tel:') || href.startsWith('mailto:') || href.startsWith('sms:') || href.startsWith('https://wa.me') || href.startsWith('javascript:') || a.target==='_blank') return;
      var url; try{url=new URL(href, location.href);}catch(err){return;}
      if(url.origin!==location.origin) return;
      e.preventDefault();
      ensureLoader().classList.remove('is-hiding');
      setTimeout(function(){ location.href=url.href; }, 2100);
    }, true);
  }
  function unlock(){
    document.documentElement.style.overflowY='auto';
    document.body.style.overflowY='auto';
    document.body.style.position='static';
    document.body.classList.remove('menu-open','no-scroll','scroll-lock');
  }
  function init(){
    unlock(); initSlider(); initLinks();
    document.querySelectorAll('.store-info-card').forEach(function(el){ el.remove(); });
    ensureLoader();
    hideLoader(1850);
  }
  if(document.readyState==='loading') document.addEventListener('DOMContentLoaded',init); else init();
  window.addEventListener('pageshow',function(){unlock();});
})();
