/* ============================================================
   FI-NE & LACUEOR — B2B Partner Portal
   Scripts — i18n, banner selector, smooth scroll, topbar shadow
   ============================================================ */

(function () {
  "use strict";

  /* ── Translations ─────────────────────────────────────── */
  var translations = {
    en: {
      "signup.label": "Contact Us",
      "signup.title": "Become a Partner",
      "signup.body":
        "Interested in carrying [fi-ne] or LACUEOR? Send us your details and we will get back to you within 48 hours.",
      "signup.name": "Name *",
      "signup.email": "Email *",
      "signup.phone": "Phone",
      "signup.website": "Website",
      "signup.social": "Social Media",
      "signup.message": "Describe your business case *",
      "signup.submit": "Send Request",
      "footer.copyright": "\u00a9 2025 FINOR GmbH. All rights reserved.",
      "footer.legal": "Legal Notice",
      "footer.privacy": "Privacy Policy",
      "footer.tagline": "Made in Berlin, with love.",
    },
    de: {
      "signup.label": "Kontakt",
      "signup.title": "Partner werden",
      "signup.body":
        "Interesse an [fi-ne] oder LACUEOR? Senden Sie uns Ihre Daten und wir melden uns innerhalb von 48 Stunden zur\u00fcck.",
      "signup.name": "Name *",
      "signup.email": "E-Mail *",
      "signup.phone": "Telefon",
      "signup.website": "Webseite",
      "signup.social": "Social Media",
      "signup.message": "Beschreiben Sie Ihr Gesch\u00e4ftsfeld *",
      "signup.submit": "Anfrage senden",
      "footer.copyright": "\u00a9 2025 FINOR GmbH. Alle Rechte vorbehalten.",
      "footer.legal": "Impressum",
      "footer.privacy": "Datenschutz",
      "footer.tagline": "Hergestellt in Berlin, mit Liebe.",
    },
  };

  function applyLang(lang) {
    var t = translations[lang] || translations.en;
    document.querySelectorAll("[data-i18n]").forEach(function (el) {
      var key = el.getAttribute("data-i18n");
      if (t[key]) el.textContent = t[key];
    });
    document.getElementById("lang-select").value = lang;
  }

  /* Auto-detect from browser, default to English */
  var detectedLang =
    navigator.language && navigator.language.slice(0, 2) === "de" ? "de" : "en";
  applyLang(detectedLang);

  document
    .getElementById("lang-select")
    .addEventListener("change", function () {
      applyLang(this.value);
    });

  /* ── Banner image selector ──────────────────────────────── */
  var bannerPicker = document.getElementById("banner-picker");
  var brandsBanner = document.getElementById("brands-banner");
  if (bannerPicker && brandsBanner) {
    bannerPicker.addEventListener("change", function () {
      brandsBanner.src = "asset/banner/" + this.value + ".webp";
    });
  }

  /* ── Smooth anchor scroll offset for fixed topbar ────────── */
  document.querySelectorAll('a[href^="#"]').forEach(function (anchor) {
    anchor.addEventListener("click", function (e) {
      var target = document.querySelector(this.getAttribute("href"));
      if (!target) return;
      e.preventDefault();
      var offset = 80;
      var top =
        target.getBoundingClientRect().top + window.pageYOffset - offset;
      window.scrollTo({ top: top, behavior: "smooth" });
    });
  });

  /* ── Topbar shadow on scroll ─────────────────────────────── */
  var topbar = document.querySelector(".topbar");
  window.addEventListener(
    "scroll",
    function () {
      if (window.scrollY > 20) {
        topbar.style.boxShadow = "0 2px 24px rgba(0,0,0,0.08)";
      } else {
        topbar.style.boxShadow = "none";
      }
    },
    { passive: true },
  );
})();
