
(function(){
  function letterize(loader){
    if(!loader) return;
    var logo=loader.querySelector('.loader-logo');
    if(logo && !logo.dataset.letterized){
      var text=(logo.textContent||'NOTORIOUS').trim().toUpperCase();
      logo.innerHTML='';
      Array.prototype.forEach.call(text,function(ch,i){
        var s=document.createElement('span');
        s.textContent=ch===' ' ? '\u00a0' : ch;
        s.style.animationDelay=(i*0.14)+'s';
        logo.appendChild(s);
      });
      logo.dataset.letterized='1';
    }
  }
  function buildLoader(){
    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);
    }
    letterize(loader);
    return loader;
  }
  function hideLoader(delay){
    var loader=buildLoader();
    setTimeout(function(){loader.classList.add('is-hiding');}, delay||3000);
  }
  if(document.readyState==='loading') document.addEventListener('DOMContentLoaded',function(){buildLoader(); hideLoader(3000); initSlider();});
  else {buildLoader(); hideLoader(3000); initSlider();}

  function initSlider(){
    var safe=['promo-grid-premium-02.png','promo-grid-premium-03.png','promo-banners-horizontal-01.png','promo-banners-horizontal-02.png','promo-social-grid-02.png'];
    document.querySelectorAll('.home-hero-slider').forEach(function(slider){
      slider.innerHTML='';
      safe.forEach(function(file,i){
        var d=document.createElement('div'); d.className='slide'+(i===0?' active':'');
        d.style.backgroundImage="url('../assets/images/"+file+"')";
        // fix for root pages
        if(!location.pathname.includes('/el/') && !location.pathname.includes('/eng/')) d.style.backgroundImage="url('assets/images/"+file+"')";
        slider.appendChild(d);
      });
      if(slider.dataset.customerSlider==='1') return;
      slider.dataset.customerSlider='1';
      var slides=[].slice.call(slider.querySelectorAll('.slide'));
      var idx=0, busy=false;
      setInterval(function(){
        if(busy||slides.length<2) return; busy=true;
        slides[idx].classList.remove('active');
        idx=(idx+1)%slides.length;
        slides[idx].classList.add('active');
        setTimeout(function(){busy=false;},1300);
      },6200);
    });
  }
  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 && !href.endsWith('.html') && !href.startsWith('../')) return;
    e.preventDefault();
    var loader=buildLoader(); loader.classList.remove('is-hiding');
    setTimeout(function(){location.href=url.href;},1350);
  },true);
})();


// Footer current link fallback
(function(){
  function markFooter(){
    var current=(location.pathname.split('/').pop()||'index.html');
    document.querySelectorAll('.footer a[href]').forEach(function(a){
      var href=(a.getAttribute('href')||'').split('#')[0].split('?')[0];
      if(href===current){ a.classList.add('current'); a.setAttribute('aria-current','page'); }
    });
  }
  if(document.readyState==='loading') document.addEventListener('DOMContentLoaded',markFooter); else markFooter();
})();
