
document.addEventListener('DOMContentLoaded', function () {
  document.body.classList.add('loaded');
  var loader = document.querySelector('.site-loader');
  if (loader) { setTimeout(function(){ loader.classList.add('is-hiding'); setTimeout(function(){ if(loader.parentNode) loader.remove(); }, 520); }, 3400); }

  var links = {
    whatsapp: "https://wa.me/302111103762?text=Geia%20sas%20NOTORIOUS%2C%20tha%20ithela%20na%20rotiso.",
    maps: "https://www.google.com/maps/search/?api=1&query=%CE%9B%CE%B5%CF%89%CF%86%CF%8C%CF%81%CE%BF%CF%82%20%CE%94%CE%B7%CE%BC%CE%B7%CF%84%CF%81%CE%AF%CE%BF%CF%85%20%CE%93%CE%BF%CF%8D%CE%BD%CE%B1%CF%81%CE%B7%20193%20%CE%86%CE%BD%CF%89%20%CE%93%CE%BB%CF%85%CF%86%CE%AC%CE%B4%CE%B1%20166%2074",
    phone: "tel:+302111103762",
    call: "tel:+302111103762"
  };

  document.querySelectorAll('[data-link]').forEach(function (el) {
    var key = el.getAttribute('data-link');
    if (links[key]) {
      el.setAttribute('href', links[key]);
      if (key === 'whatsapp' || key === 'maps') {
        el.setAttribute('target', '_blank');
        el.setAttribute('rel', 'noopener');
      }
    }
  });

  document.querySelectorAll('.reveal').forEach(function (el) {
    el.classList.add('visible');
  });

  var menuBtn = document.getElementById('menuToggle');
  var menu = document.getElementById('mobileMenu');
  var closeBtn = document.getElementById('closeMenu');

  if (menuBtn && menu) {
    menuBtn.addEventListener('click', function () {
      menu.classList.add('show');
    });
  }
  if (closeBtn && menu) {
    closeBtn.addEventListener('click', function () {
      menu.classList.remove('show');
    });
  }
  if (menu) {
    menu.querySelectorAll('a').forEach(function (a) {
      a.addEventListener('click', function () {
        menu.classList.remove('show');
      });
    });
  }

  var topBtn = document.getElementById('backToTop');
  if (topBtn) {
    window.addEventListener('scroll', function () {
      if (window.scrollY > 500) topBtn.classList.add('show');
      else topBtn.classList.remove('show');
    });
    topBtn.addEventListener('click', function () {
      window.scrollTo({ top: 0, behavior: 'smooth' });
    });
  }

  var banner = document.getElementById('cookieBanner');
  var panel = document.getElementById('cookiePanel');
  var reopen = document.getElementById('cookieReopen');

  if (banner && localStorage.getItem('notoriousCookieConsent')) {
    banner.classList.add('hide');
  }

  document.querySelectorAll('[data-cookie-accept]').forEach(function (btn) {
    btn.addEventListener('click', function () {
      localStorage.setItem('notoriousCookieConsent', 'accepted');
      if (banner) banner.classList.add('hide');
      if (panel) panel.classList.remove('show');
    });
  });

  document.querySelectorAll('[data-cookie-settings]').forEach(function (btn) {
    btn.addEventListener('click', function () {
      if (panel) panel.classList.add('show');
    });
  });

  document.querySelectorAll('[data-cookie-save]').forEach(function (btn) {
    btn.addEventListener('click', function () {
      localStorage.setItem('notoriousCookieConsent', 'custom');
      if (banner) banner.classList.add('hide');
      if (panel) panel.classList.remove('show');
    });
  });

  document.querySelectorAll('[data-cookie-close]').forEach(function (btn) {
    btn.addEventListener('click', function () {
      if (panel) panel.classList.remove('show');
    });
  });

  if (reopen && panel) {
    reopen.addEventListener('click', function () {
      panel.classList.add('show');
    });
  }

  document.querySelectorAll('form.contact-form').forEach(function(form){
    form.addEventListener('submit', function(e){
      if (!form.checkValidity()) {
        e.preventDefault();
        if (typeof form.reportValidity === 'function') form.reportValidity();
      }
    });
  });
});

document.addEventListener('DOMContentLoaded',()=>document.documentElement.classList.add('notoriousStoreSliderReady'));


/* working homepage hero slider */
document.addEventListener('DOMContentLoaded', function(){
  document.querySelectorAll('.home-hero-slider').forEach(function(slider){
    const slides = Array.from(slider.querySelectorAll('.slide'));
    if(!slides.length) return;
    let index = 0;
    slides.forEach((s,i)=>s.classList.toggle('active', i===0));
    if(slides.length > 1){
      setInterval(function(){
        slides[index].classList.remove('active');
        index = (index + 1) % slides.length;
        slides[index].classList.add('active');
      }, 4200);
    }
  });
});


/* robust language switching */
document.addEventListener('DOMContentLoaded', function(){
  document.querySelectorAll('.lang a[data-lang-target]').forEach(function(a){
    a.addEventListener('click', function(e){
      const lang = a.getAttribute('data-lang-target');
      const file = document.body.getAttribute('data-page') || 'index.html';
      if(lang === 'el') a.href = '../el/' + file;
      if(lang === 'eng') a.href = '../eng/' + file;
    });
  });
});


/* show Notorious loader briefly when moving between internal pages */
document.addEventListener('DOMContentLoaded', function(){
  document.querySelectorAll('a[href]').forEach(function(a){
    a.addEventListener('click', function(e){
      var href = a.getAttribute('href') || '';
      if(!href || href.startsWith('#') || href.startsWith('tel:') || href.startsWith('mailto:') || href.startsWith('https://wa.me') || a.target === '_blank') return;
      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 · 24h</div></div>';
        document.body.appendChild(loader);
      }
      loader.classList.remove('is-hiding');
    });
  });
});


// Robust NOTORIOUS sliders
(function(){
  function initSlider(selector, interval){
    document.querySelectorAll(selector).forEach(function(slider){
      var slides=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');
      }, interval || 4200);
    });
  }
  if(document.readyState==='loading') document.addEventListener('DOMContentLoaded',function(){initSlider('.home-hero-slider',3900)});
  else initSlider('.home-hero-slider',3900);
})();


/* FINAL: visible NOTORIOUS loader on every internal page change */
(function(){
  function showLoader(){
    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);
    }
    loader.classList.remove('is-hiding');
    return loader;
  }
  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.charAt(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, window.location.href); }catch(err){ return; }
    if(url.origin!==window.location.origin && !href.startsWith('../') && !href.endsWith('.html')) return;
    e.preventDefault();
    showLoader();
    setTimeout(function(){ window.location.href=url.href; }, 720);
  }, true);
})();
