/* TrainChain — lightweight front-end (no dependencies). */ (function () { "use strict"; // Close mobile nav when a link is tapped or on resize. document.querySelectorAll(".nav a").forEach(function (a) { a.addEventListener("click", function () { document.body.classList.remove("nav-open"); }); }); window.addEventListener("resize", function () { if (window.innerWidth > 720) document.body.classList.remove("nav-open"); }); // Station autocomplete for any . document.querySelectorAll("input[data-station]").forEach(function (input) { var box = document.createElement("div"); box.className = "ac-box"; box.style.cssText = "position:absolute;z-index:60;background:#fff;border:1px solid var(--line);" + "border-radius:12px;box-shadow:var(--shadow);margin-top:4px;max-height:240px;" + "overflow:auto;display:none;min-width:220px;max-width:calc(100vw - 36px)"; input.parentNode.style.position = "relative"; input.parentNode.appendChild(box); var timer = null; input.addEventListener("input", function () { var q = input.value.trim(); clearTimeout(timer); if (q.length < 2) { box.style.display = "none"; return; } timer = setTimeout(function () { fetch("/stations.php?q=" + encodeURIComponent(q)) .then(function (r) { return r.json(); }) .then(function (rows) { box.innerHTML = ""; if (!rows.length) { box.style.display = "none"; return; } rows.forEach(function (s) { var item = document.createElement("button"); item.type = "button"; item.textContent = s.name + " (" + s.code + ")"; item.style.cssText = "display:block;width:100%;text-align:left;border:0;background:none;" + "padding:11px 14px;cursor:pointer;font:inherit;color:var(--ink)"; item.addEventListener("mouseenter", function () { item.style.background = "var(--brand-50)"; }); item.addEventListener("mouseleave", function () { item.style.background = "none"; }); item.addEventListener("click", function () { input.value = s.name + " (" + s.code + ")"; input.dataset.code = s.code; box.style.display = "none"; }); box.appendChild(item); }); box.style.display = "block"; }) .catch(function () { box.style.display = "none"; }); }, 180); }); document.addEventListener("click", function (ev) { if (ev.target !== input) box.style.display = "none"; }); }); // Swap the From/To station fields (value + autocomplete code). window.rrSwap = function (btn) { var form = btn.closest("form"); if (!form) return; var a = form.querySelector('input[name="from"]'), b = form.querySelector('input[name="to"]'); if (!a || !b) return; var v = a.value; a.value = b.value; b.value = v; var c = a.dataset.code || ""; a.dataset.code = b.dataset.code || ""; b.dataset.code = c; }; })();