/* ============================================================
public/js/buildings/bazaar.js
Bazaar – eigenes Parchment-Popup (wie Gaststätte)
============================================================ */
let baz_initialized = false;
/* ── CSS einmalig laden ── */
function loadCSS() {
if (!document.querySelector('link[href="/css/bazaar.css"]')) {
const link = document.createElement("link");
link.rel = "stylesheet";
link.href = "/css/bazaar.css";
document.head.appendChild(link);
}
}
/* ── Popup-Element erzeugen falls noch nicht vorhanden ── */
function ensurePopup() {
if (document.getElementById("bazaar-popup")) return;
const popup = document.createElement("div");
popup.id = "bazaar-popup";
popup.className = "qm-popup";
popup.innerHTML = `
`;
document.body.appendChild(popup);
/* Schließen-Button */
popup.querySelector("#bazaar-close").addEventListener("click", closeBazaar);
/* Tab-Klick */
popup.querySelectorAll(".mp-tab").forEach((btn) => {
btn.addEventListener("click", () => {
popup.querySelectorAll(".mp-tab").forEach((t) => t.classList.remove("mp-tab-active"));
popup.querySelectorAll(".mp-panel").forEach((p) => p.classList.remove("active"));
btn.classList.add("mp-tab-active");
document.getElementById(btn.dataset.tab)?.classList.add("active");
});
});
/* Drag-fähig machen */
const header = popup.querySelector(".qm-popup-header");
let isDragging = false, startX, startY, startLeft, startTop;
header.style.cursor = "grab";
header.addEventListener("mousedown", (e) => {
if (e.target.classList.contains("qm-popup-close")) return;
isDragging = true;
header.style.cursor = "grabbing";
const rect = popup.getBoundingClientRect();
startX = e.clientX; startY = e.clientY;
startLeft = rect.left; startTop = rect.top;
popup.style.transform = "none";
popup.style.left = startLeft + "px";
popup.style.top = startTop + "px";
e.preventDefault();
});
document.addEventListener("mousemove", (e) => {
if (!isDragging) return;
popup.style.left = (startLeft + (e.clientX - startX)) + "px";
popup.style.top = (startTop + (e.clientY - startY)) + "px";
});
document.addEventListener("mouseup", () => {
if (!isDragging) return;
isDragging = false;
header.style.cursor = "grab";
});
}
function closeBazaar() {
document.getElementById("bazaar-popup")?.classList.remove("active");
document.getElementById("qm-overlay")?.classList.remove("active");
}
/* ── Öffentliche Funktion ── */
export function loadBazaar() {
loadCSS();
ensurePopup();
const popup = document.getElementById("bazaar-popup");
const overlay = document.getElementById("qm-overlay");
popup.style.left = "50%";
popup.style.top = "50%";
popup.style.transform = "translate(-50%, -50%) scale(1)";
popup.classList.add("active");
overlay?.classList.add("active");
}