diff --git a/OpenBench/static/board.css b/OpenBench/static/board.css
new file mode 100644
index 0000000..52b0dae
--- /dev/null
+++ b/OpenBench/static/board.css
@@ -0,0 +1,500 @@
+/* ===========================================================================
+ OpenBench - board analyser ( event page )
+ Loaded after ux.css. Layout: the board on the left, the moves and the
+ evaluation graph on the right, with both columns the same height.
+ =========================================================================== */
+
+:root {
+ --bd-light: #E3E8F1;
+ --bd-dark: #7A8FB3;
+}
+
+/* ---------- Layout ---------- */
+
+.bd-layout {
+ display: grid;
+ grid-template-columns: minmax(0, 640px) minmax(0, 1fr);
+ gap: 20px;
+ align-items: stretch;
+ margin-bottom: 20px;
+}
+
+.bd-layout .card {
+ margin-bottom: 0;
+}
+
+.bd-card {
+ display: flex;
+ flex-direction: column;
+}
+
+.bd-body {
+ padding: 16px;
+}
+
+/* The right column does not add to the row height, so it always matches the board */
+.ev-right {
+ position: relative;
+ min-width: 0;
+ min-height: 560px;
+}
+
+.ev-right-inner {
+ position: absolute;
+ inset: 0;
+ display: flex;
+ flex-direction: column;
+ gap: 20px;
+}
+
+.ev-moves-card {
+ display: flex;
+ flex: 1 1 0;
+ flex-direction: column;
+ min-height: 0;
+}
+
+.ev-moves-card .ev-movelist {
+ flex: 1 1 0;
+ min-height: 0;
+ max-height: none;
+}
+
+.bd-graph-card {
+ flex: none;
+}
+
+/* ---------- Board ---------- */
+
+.bd-grid {
+ display: grid;
+ grid-template-columns: 28px minmax(0, 1fr);
+ column-gap: 12px;
+ row-gap: 10px;
+}
+
+#bd-top { grid-column: 2; grid-row: 1; }
+.bd-eval { grid-column: 1; grid-row: 2; }
+.bd-board { grid-column: 2; grid-row: 2; }
+#bd-bottom { grid-column: 2; grid-row: 3; }
+
+.bd-board {
+ aspect-ratio: 1;
+ border-radius: var(--radius);
+ box-shadow: 0 0 0 1px var(--line-strong), 0 8px 22px rgba(0, 0, 0, 0.35);
+ overflow: hidden;
+}
+
+.bd-board svg {
+ display: block;
+ width: 100%;
+ height: 100%;
+}
+
+.bd-nojs {
+ margin: 0;
+ padding: 24px;
+ color: var(--muted);
+}
+
+.bd-light { fill: var(--bd-light); }
+.bd-dark { fill: var(--bd-dark); }
+
+.bd-hl-from { fill: rgba(250, 204, 21, 0.38); }
+.bd-hl-to { fill: rgba(250, 204, 21, 0.55); }
+.bd-hl-bad { fill: rgba(248, 113, 113, 0.5); }
+
+.bd-check-in { stop-color: #F87171; stop-opacity: 0.95; }
+.bd-check-out { stop-color: #F87171; stop-opacity: 0; }
+
+.bd-coord {
+ font: 600 17px var(--font-sans);
+ pointer-events: none;
+ user-select: none;
+}
+
+.bd-coord.on-dark { fill: var(--bd-light); }
+.bd-coord.on-light { fill: var(--bd-dark); }
+
+.bd-arrow { opacity: 0.92; }
+.bd-arrow line { stroke: #F87171; }
+.bd-arrow polygon { fill: #F87171; }
+
+/* ---------- Evaluation bar ---------- */
+
+.bd-eval {
+ position: relative;
+ align-self: stretch;
+ background-color: #161B26;
+ border-radius: var(--radius);
+ box-shadow: 0 0 0 1px var(--line-strong);
+ overflow: hidden;
+}
+
+.bd-eval::after {
+ content: '';
+ position: absolute;
+ top: 50%;
+ left: 0;
+ right: 0;
+ height: 1px;
+ background-color: rgba(120, 130, 150, 0.55);
+}
+
+.bd-eval-fill {
+ position: absolute;
+ left: 0;
+ right: 0;
+ bottom: 0;
+ height: 50%;
+ background-color: #EDF0F6;
+ transition: height 0.25s ease;
+}
+
+.bd-eval.is-flipped .bd-eval-fill {
+ top: 0;
+ bottom: auto;
+}
+
+.bd-eval-label {
+ position: absolute;
+ left: 0;
+ right: 0;
+ z-index: 1;
+ font: 600 10.5px var(--font-mono);
+ letter-spacing: -0.03em;
+ text-align: center;
+}
+
+.bd-eval-label.on-white { bottom: 6px; color: #161B26; }
+.bd-eval-label.on-black { top: 6px; color: #E6EAF2; }
+
+.bd-eval.is-flipped .bd-eval-label.on-white { top: 6px; bottom: auto; }
+.bd-eval.is-flipped .bd-eval-label.on-black { bottom: 6px; top: auto; }
+
+/* ---------- Players, captured pieces ---------- */
+
+.bd-player {
+ display: flex;
+ align-items: center;
+ gap: 10px;
+ min-width: 0;
+ min-height: 34px;
+}
+
+.bd-player .ev-piece {
+ flex: none;
+ width: 12px;
+ height: 12px;
+}
+
+.bd-player.is-white .ev-piece { background-color: #F1F4FA; }
+.bd-player.is-black .ev-piece { background-color: #0B0E14; }
+
+.bd-player.is-turn .ev-piece {
+ box-shadow: 0 0 0 2px var(--color2), 0 0 0 4px var(--accent);
+}
+
+.bd-name {
+ flex: 0 1 auto;
+ min-width: 0;
+ overflow: hidden;
+ color: var(--color-font1);
+ font-size: 14px;
+ font-weight: 600;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+}
+
+.bd-caps {
+ display: flex;
+ flex: 1 1 auto;
+ align-items: center;
+ min-width: 0;
+ padding-left: 6px;
+ overflow: hidden;
+}
+
+.bd-cap {
+ flex: none;
+ width: 20px;
+ height: 20px;
+}
+
+.bd-cap + .bd-cap {
+ margin-left: -9px;
+}
+
+.bd-mat {
+ flex: none;
+ color: var(--muted);
+ font: 600 12px var(--font-mono);
+}
+
+/* ---------- Status, controls, FEN ---------- */
+
+.bd-status {
+ min-height: 20px;
+ margin: 14px 0 0;
+ color: var(--color-font2);
+ font-size: 13px;
+ font-weight: 500;
+ text-align: center;
+}
+
+.bd-status.is-bad { color: var(--bad); }
+.bd-status.is-warn { color: var(--warn); }
+
+.bd-controls {
+ display: grid;
+ grid-template-columns: repeat(5, minmax(0, 1fr)) 52px;
+ gap: 8px;
+ margin-top: 12px;
+}
+
+.bd-card .bd-btn {
+ display: inline-flex;
+ align-items: center;
+ justify-content: center;
+ height: 40px;
+ padding: 0;
+ background-color: var(--color3);
+ border: 1px solid var(--line-strong);
+ border-radius: var(--radius);
+ color: var(--color-font1);
+ font-size: 14px;
+ cursor: pointer;
+ transition: background-color 0.12s ease, border-color 0.12s ease;
+}
+
+.bd-card .bd-btn:hover:not(:disabled) {
+ background-color: var(--surface-hover);
+ border-color: var(--accent);
+}
+
+.bd-card .bd-btn:focus-visible {
+ outline: 2px solid var(--accent);
+ outline-offset: 2px;
+}
+
+.bd-card .bd-btn:disabled {
+ opacity: 0.4;
+ cursor: not-allowed;
+}
+
+.bd-card .bd-btn-main {
+ background-color: var(--accent);
+ border-color: var(--accent);
+ color: #FFFFFF;
+}
+
+.bd-card .bd-btn-main:hover:not(:disabled) {
+ background-color: var(--accent-hover);
+ border-color: var(--accent-hover);
+}
+
+.bd-card .bd-btn-main.is-playing {
+ background-color: var(--accent-soft);
+ border-color: var(--accent);
+ color: var(--color-font1);
+}
+
+.bd-card .bd-btn-flip {
+ margin-left: 4px;
+}
+
+.bd-fen {
+ display: flex;
+ align-items: center;
+ gap: 10px;
+ margin-top: 12px;
+ padding: 5px 5px 5px 12px;
+ background-color: var(--color1);
+ border: 1px solid var(--line);
+ border-radius: var(--radius);
+}
+
+.bd-fen-label {
+ flex: none;
+ color: var(--muted);
+ font-size: 11px;
+ font-weight: 600;
+ letter-spacing: 0.1em;
+}
+
+.bd-fen code {
+ flex: 1 1 auto;
+ min-width: 0;
+ padding: 0;
+ overflow: hidden;
+ background: none;
+ color: var(--color-font2);
+ font-family: var(--font-mono);
+ font-size: 12px;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+}
+
+.bd-card .bd-btn-icon {
+ flex: none;
+ width: 32px;
+ height: 32px;
+}
+
+/* ---------- Moves card ---------- */
+
+.ev-moves-card .ev-sel {
+ display: flex;
+ flex-wrap: wrap;
+ align-items: center;
+ justify-content: space-between;
+ gap: 10px 16px;
+ padding: 14px 16px;
+ border-bottom: 1px solid var(--line);
+}
+
+.ev-moves-card .ev-sel-main {
+ flex: 1 1 auto;
+ min-width: 0;
+}
+
+.ev-moves-card .ev-sel-san {
+ font-size: 24px;
+}
+
+.ev-sel-counter {
+ margin-left: auto;
+ color: var(--muted);
+ font-size: 12px;
+}
+
+.ev-moves-card .ev-sel-stats {
+ grid-template-columns: repeat(3, minmax(64px, auto));
+ margin-top: 0;
+}
+
+.ev-moves-card .ev-sel-note {
+ flex-basis: 100%;
+ margin: 0;
+}
+
+.ev-moves-foot {
+ display: flex;
+ flex-wrap: wrap;
+ gap: 6px 18px;
+ padding: 10px 16px;
+ border-top: 1px solid var(--line);
+ color: var(--muted);
+ font-size: 12px;
+}
+
+.ev-moves-foot kbd {
+ display: inline-block;
+ min-width: 20px;
+ padding: 1px 6px;
+ background-color: var(--color3);
+ border: 1px solid var(--line-strong);
+ border-bottom-width: 2px;
+ border-radius: var(--radius);
+ color: var(--color-font2);
+ font: 11px var(--font-mono);
+ text-align: center;
+}
+
+/* ---------- Evaluation graph ---------- */
+
+.bd-graph-wrap {
+ padding: 14px 16px 16px;
+}
+
+.bd-graph {
+ height: 132px;
+ background-color: #363F54;
+ border: 1px solid var(--line-strong);
+ border-radius: var(--radius);
+ cursor: crosshair;
+ overflow: hidden;
+ touch-action: none;
+}
+
+.bd-graph svg {
+ display: block;
+ width: 100%;
+ height: 100%;
+}
+
+.bd-area-white { fill: rgba(237, 240, 246, 0.88); }
+.bd-area-black { fill: rgba(11, 14, 20, 0.92); }
+
+.bd-mid {
+ stroke: rgba(255, 255, 255, 0.28);
+ stroke-width: 1;
+ vector-effect: non-scaling-stroke;
+}
+
+.bd-curve {
+ fill: none;
+ stroke: var(--accent-hover);
+ stroke-width: 2;
+ stroke-linejoin: round;
+ vector-effect: non-scaling-stroke;
+}
+
+.bd-cursor {
+ stroke: var(--accent);
+ stroke-width: 2;
+ vector-effect: non-scaling-stroke;
+}
+
+/* ---------- Responsive ---------- */
+
+@media (max-width: 1180px) {
+
+ .bd-layout {
+ grid-template-columns: minmax(0, 1fr);
+ }
+
+ .bd-card {
+ width: 100%;
+ max-width: 640px;
+ justify-self: center;
+ }
+
+ .ev-right {
+ min-height: 0;
+ }
+
+ .ev-right-inner {
+ position: static;
+ }
+
+ .ev-moves-card {
+ flex: none;
+ }
+
+ .ev-moves-card .ev-movelist {
+ flex: none;
+ max-height: 420px;
+ }
+}
+
+@media (max-width: 767px) {
+
+ .bd-body {
+ padding: 12px;
+ }
+
+ .bd-grid {
+ grid-template-columns: 22px minmax(0, 1fr);
+ column-gap: 8px;
+ }
+
+ .bd-eval-label {
+ font-size: 9px;
+ }
+
+ .bd-controls {
+ grid-template-columns: repeat(5, minmax(0, 1fr)) 44px;
+ gap: 6px;
+ }
+}
diff --git a/OpenBench/static/board.js b/OpenBench/static/board.js
new file mode 100644
index 0000000..b4bf218
--- /dev/null
+++ b/OpenBench/static/board.js
@@ -0,0 +1,502 @@
+/* OpenBench board analyser.
+
+ Replays the PGN of an event position by position ( see chess.js ), and draws
+ - the board, with the last move, checks, and any illegal move that was tried
+ - an evaluation bar and an evaluation graph, from the engine comments
+ - captured pieces and material for both sides
+
+ event_viewer.js owns the selected move and tells us about it with the
+ "ob:ply" event. We only draw, and ask window.OpenBenchViewer to move.
+
+ Piece artwork: "Cburnett" set by Colin M.L. Burnett, GPL-2.0-or-later.
+ https://en.wikipedia.org/wiki/User:Cburnett/GFDL_images/Chess */
+
+(function () {
+ 'use strict';
+
+ var Chess = window.OBChess;
+ var host = document.getElementById('bd-board');
+
+ if (!Chess || !host) return;
+
+
+ var NS = 'http://www.w3.org/2000/svg';
+
+ function $(id) { return document.getElementById(id); }
+
+ function readJSON(id, fallback) {
+ var element = $(id);
+ if (!element) return fallback;
+ try { return JSON.parse(element.textContent); } catch (error) { return fallback; }
+ }
+
+ var game = readJSON('ev-game', null);
+ var moves = readJSON('ev-moves', []);
+
+ if (!game || !moves.length) return;
+
+ var total = moves.length;
+ var replayed = Chess.replay(game.startFen, moves.map(function (move) { return move.san; }));
+ var positions = replayed.positions; // positions[i] is the board after i moves
+ var problem = replayed.error;
+ var attempt = Chess.findUciMove(game.note);
+
+ var current = total;
+ var flipped = false;
+ var timer = null;
+
+ try { flipped = window.localStorage.getItem('ob:board:flipped') === '1'; } catch (error) { /* ignore */ }
+
+ /* ---------- Evaluations, from the White point of view ---------- */
+
+ var evals = [{ value: null, label: '\u2013' }];
+
+ moves.forEach(function (move) {
+
+ var raw = move.eval;
+
+ if (!raw) return evals.push({ value: null, label: '\u2013' });
+ if (raw === 'book') return evals.push({ value: null, label: 'book' });
+
+ var body = raw.replace(/^[+-]/, '');
+ var mate = body.charAt(0) === 'M';
+ var magnitude = mate ? 100 : parseFloat(body);
+
+ if (isNaN(magnitude)) return evals.push({ value: null, label: '\u2013' });
+
+ // The comment is from the point of view of the side that moved
+ var value = (raw.charAt(0) === '-' ? -magnitude : magnitude) * (move.color === 'b' ? -1 : 1);
+
+ var label = mate ? (value < 0 ? '-' : '') + body : (value > 0 ? '+' : '') + value.toFixed(1);
+
+ evals.push({ value: value, label: label });
+ });
+
+ var hasEvals = evals.some(function (entry) { return entry.value !== null; });
+
+ // Squashes pawns into -1 .. +1, so that a mate does not flatten everything else
+ function squash(pawns) {
+ return 2 / (1 + Math.exp(-pawns / 2.2)) - 1;
+ }
+
+ /* ---------- Piece artwork, shared by every board and tray ---------- */
+
+ var PIECES = {
+ wK: '',
+ wQ: '',
+ wR: '',
+ wB: '',
+ wN: '',
+ wP: '',
+ bK: '',
+ bQ: '',
+ bR: '',
+ bB: '',
+ bN: '',
+ bP: ''
+ };
+
+ function buildSprite() {
+
+ if ($('bd-sprite')) return;
+
+ var defs = '';
+ Object.keys(PIECES).forEach(function (key) {
+ defs += '' + PIECES[key] + '';
+ });
+
+ var sprite = document.createElementNS(NS, 'svg');
+ sprite.setAttribute('id', 'bd-sprite');
+ sprite.setAttribute('width', '0');
+ sprite.setAttribute('height', '0');
+ sprite.setAttribute('aria-hidden', 'true');
+ sprite.style.position = 'absolute';
+ sprite.innerHTML = '' + defs + '';
+
+ document.body.appendChild(sprite);
+ }
+
+ function pieceKey(piece) {
+ return (piece === piece.toUpperCase() ? 'w' : 'b') + piece.toUpperCase();
+ }
+
+ /* ---------- The board ---------- */
+
+ // Top left corner of a square, in the 800 x 800 space of the board
+ function corner(square) {
+
+ var file = square & 7, rank = square >> 3;
+
+ return flipped ? { x: (7 - file) * 100, y: rank * 100 } : { x: file * 100, y: (7 - rank) * 100 };
+ }
+
+ function centre(square) {
+ var at = corner(square);
+ return { x: at.x + 50, y: at.y + 50 };
+ }
+
+ function arrow(from, to, className) {
+
+ var a = centre(from), b = centre(to);
+ var dx = b.x - a.x, dy = b.y - a.y, length = Math.sqrt(dx * dx + dy * dy);
+
+ if (!length) return '';
+
+ var ux = dx / length, uy = dy / length;
+ var tip = { x: b.x - ux * 8, y: b.y - uy * 8 };
+ var base = { x: tip.x - ux * 42, y: tip.y - uy * 42 };
+ var from_ = { x: a.x + ux * 22, y: a.y + uy * 22 };
+
+ var points = [
+ tip.x + ',' + tip.y,
+ (base.x - uy * 25) + ',' + (base.y + ux * 25),
+ (base.x + uy * 25) + ',' + (base.y - ux * 25)
+ ].join(' ');
+
+ return '' +
+ '' +
+ '';
+ }
+
+ function square(at, className) {
+ return '';
+ }
+
+ function drawBoard(position, index) {
+
+ var out = '';
+ }
+
+ /* ---------- Player strips, captured pieces, material ---------- */
+
+ var ORDER = { q: 0, r: 1, b: 2, n: 3, p: 4 };
+
+ function captured(list) {
+
+ return list.split('').sort(function (a, b) {
+ return ORDER[a.toLowerCase()] - ORDER[b.toLowerCase()];
+ }).map(function (piece) {
+ return '';
+ }).join('');
+ }
+
+ function fillStrip(strip, color, position) {
+
+ if (!strip) return;
+
+ var other = color === 'w' ? 'b' : 'w';
+ var material = Chess.material(position.board);
+ var lead = material[color] - material[other];
+
+ strip.className = 'bd-player is-' + (color === 'w' ? 'white' : 'black') + (position.turn === color ? ' is-turn' : '');
+ strip.querySelector('.bd-name').textContent = color === 'w' ? game.white : game.black;
+ strip.querySelector('.bd-caps').innerHTML = captured(position.caps[color]);
+ strip.querySelector('.bd-mat').textContent = lead > 0 ? '+' + lead : '';
+ }
+
+ function describe(position, index) {
+
+ if (problem && index > positions.length - 1) {
+ return { tone: 'warn', text: 'Move ' + (problem.ply + 1) + ' (' + problem.san + ') cannot be played on the board' };
+ }
+
+ if (position.status === 'checkmate') return { tone: 'bad', text: 'Checkmate' };
+ if (position.status === 'stalemate') return { tone: 'warn', text: 'Stalemate' };
+
+ if (attempt && index === total) {
+ return { tone: 'bad', text: (position.turn === 'w' ? 'White' : 'Black') + ' tried ' + attempt.text + ', which is illegal' };
+ }
+
+ return { tone: position.check >= 0 ? 'warn' : '', text: (position.check >= 0 ? 'Check. ' : '') + (position.turn === 'w' ? 'White' : 'Black') + ' to move' };
+ }
+
+ /* ---------- Evaluation bar ---------- */
+
+ function drawEval(index) {
+
+ var bar = $('bd-eval');
+ if (!bar) return;
+
+ var info = evals[Math.min(index, evals.length - 1)];
+ var share = info.value === null ? 50 : 50 + 50 * squash(info.value);
+ var label = bar.querySelector('.bd-eval-label');
+
+ bar.classList.toggle('is-flipped', flipped);
+ bar.querySelector('.bd-eval-fill').style.height = share + '%';
+
+ label.textContent = info.label;
+ label.className = 'bd-eval-label ' + (info.value !== null && info.value < 0 ? 'on-black' : 'on-white');
+ bar.setAttribute('title', 'Evaluation: ' + info.label);
+ }
+
+ /* ---------- Evaluation graph ---------- */
+
+ var graph = { width: 600, height: 150, pad: 8, cursor: null };
+
+ function graphX(index) { return total ? (index / total) * graph.width : 0; }
+
+ function graphY(value) {
+ var middle = graph.height / 2;
+ return middle - squash(value) * (middle - graph.pad);
+ }
+
+ function moveLabel(index) {
+
+ if (index === 0) return 'Start position';
+
+ var move = moves[index - 1];
+ return move.num + (move.color === 'w' ? '.' : '\u2026') + ' ' + move.san;
+ }
+
+ function buildGraph() {
+
+ var host = $('bd-graph');
+ var card = $('bd-graph-card');
+
+ if (!host) return;
+
+ if (!hasEvals) {
+ if (card) card.hidden = true;
+ return;
+ }
+
+ var middle = graph.height / 2, previous = 0, points = [], i;
+
+ for (i = 0; i <= total; i++) {
+ if (evals[i].value !== null) previous = evals[i].value;
+ points.push([graphX(i), graphY(previous)]);
+ }
+
+ var line = points.map(function (point, n) { return (n ? 'L' : 'M') + point[0].toFixed(1) + ' ' + point[1].toFixed(1); }).join(' ');
+ var area = 'M0 ' + middle + ' ' + line.replace(/^M/, 'L') + ' L' + graph.width + ' ' + middle + ' Z';
+
+ host.innerHTML =
+ '';
+
+ graph.cursor = $('bd-cursor');
+
+ var scrubbing = false;
+
+ function indexAt(event) {
+ var box = host.getBoundingClientRect();
+ var ratio = box.width ? (event.clientX - box.left) / box.width : 0;
+ return Math.max(0, Math.min(total, Math.round(ratio * total)));
+ }
+
+ host.addEventListener('pointerdown', function (event) {
+ scrubbing = true;
+ if (host.setPointerCapture) host.setPointerCapture(event.pointerId);
+ go(indexAt(event));
+ });
+
+ host.addEventListener('pointermove', function (event) {
+ var index = indexAt(event);
+ graphLabel(index);
+ if (scrubbing) go(index);
+ });
+
+ host.addEventListener('pointerup', function () { scrubbing = false; });
+ host.addEventListener('pointercancel', function () { scrubbing = false; });
+ host.addEventListener('pointerleave', function () { if (!scrubbing) graphLabel(current); });
+ }
+
+ function graphLabel(index) {
+ var label = $('bd-graph-label');
+ if (label) label.textContent = moveLabel(index) + (evals[index].label !== '\u2013' ? ' \u00B7 ' + evals[index].label : '');
+ }
+
+ function drawGraphCursor(index) {
+
+ if (graph.cursor) {
+ var x = graphX(index).toFixed(1);
+ graph.cursor.setAttribute('x1', x);
+ graph.cursor.setAttribute('x2', x);
+ }
+
+ graphLabel(index);
+ }
+
+ /* ---------- Putting it together ---------- */
+
+ function go(index) {
+
+ index = Math.max(0, Math.min(total, index));
+
+ if (window.OpenBenchViewer) window.OpenBenchViewer.select(index);
+ else { current = index; render(); }
+ }
+
+ function render() {
+
+ var position = positions[Math.min(current, positions.length - 1)];
+ var status = describe(position, current);
+
+ host.innerHTML = drawBoard(position, current);
+
+ // The side at the bottom of the board is the one that faces us
+ fillStrip($('bd-bottom'), flipped ? 'b' : 'w', position);
+ fillStrip($('bd-top'), flipped ? 'w' : 'b', position);
+
+ drawEval(current);
+ drawGraphCursor(current);
+
+ var note = $('bd-status');
+ if (note) {
+ note.textContent = status.text;
+ note.className = 'bd-status' + (status.tone ? ' is-' + status.tone : '');
+ }
+
+ var fen = $('bd-fen');
+ if (fen) { fen.textContent = position.fen; fen.setAttribute('title', position.fen); }
+
+ [['bd-first', 0], ['bd-prev', 0], ['bd-next', total], ['bd-last', total]].forEach(function (pair) {
+ if ($(pair[0])) $(pair[0]).disabled = current === pair[1];
+ });
+ }
+
+ function stop() {
+
+ if (timer) window.clearInterval(timer);
+ timer = null;
+
+ var play = $('bd-play');
+ if (play) {
+ play.classList.remove('is-playing');
+ play.setAttribute('aria-label', 'Play');
+ play.innerHTML = '';
+ }
+ }
+
+ function play() {
+
+ if (timer) return stop();
+
+ if (current >= total) go(0);
+
+ timer = window.setInterval(function () {
+ if (current >= total) return stop();
+ go(current + 1);
+ }, 900);
+
+ var button = $('bd-play');
+ if (button) {
+ button.classList.add('is-playing');
+ button.setAttribute('aria-label', 'Pause');
+ button.innerHTML = '';
+ }
+ }
+
+ function flip() {
+
+ flipped = !flipped;
+ try { window.localStorage.setItem('ob:board:flipped', flipped ? '1' : '0'); } catch (error) { /* ignore */ }
+ render();
+ }
+
+ function bind(id, handler) {
+ if ($(id)) $(id).addEventListener('click', handler);
+ }
+
+ bind('bd-first', function () { stop(); go(0); });
+ bind('bd-prev', function () { stop(); go(current - 1); });
+ bind('bd-next', function () { stop(); go(current + 1); });
+ bind('bd-last', function () { stop(); go(total); });
+ bind('bd-play', play);
+ bind('bd-flip', flip);
+
+ bind('bd-copy', function () {
+ var position = positions[Math.min(current, positions.length - 1)];
+ if (window.OpenBenchViewer) window.OpenBenchViewer.copy(position.fen, 'FEN copied');
+ });
+
+ document.addEventListener('keydown', function (event) {
+
+ if (event.altKey || event.ctrlKey || event.metaKey) return;
+ if (/^(INPUT|TEXTAREA|SELECT|BUTTON|A)$/.test((event.target && event.target.tagName) || '')) return;
+
+ if (event.key === 'f' || event.key === 'F') { flip(); event.preventDefault(); }
+ if (event.key === ' ') { play(); event.preventDefault(); }
+
+ // Moving by hand ends a running playback
+ if (/^(ArrowLeft|ArrowRight|Home|End)$/.test(event.key)) stop();
+ });
+
+ document.addEventListener('ob:ply', function (event) {
+ current = event.detail.index;
+ render();
+ });
+
+ window.OpenBenchBoard = {
+ flip : flip,
+ play : play,
+ positions : positions,
+ evals : evals
+ };
+
+ buildSprite();
+ buildGraph();
+ render();
+
+})();