Add clock support.
[remoteglot] / www / js / remoteglot.js
index 9a2b5fd..01fee37 100644 (file)
 (function() {
 
-var board = [];
+/** @type {window.ChessBoard} @private */
+var board = null;
+
+/**
+ * The most recent analysis data we have from the server
+ * (about the most recent position).
+ *
+ * @type {?Object}
+ * @private */
+var current_analysis_data = null;
+
+/**
+ * If we are displaying previous analysis, this is non-null,
+ * and will override most of current_analysis_data.
+ *
+ * @type {?Object}
+ * @private
+ */
+var displayed_analysis_data = null;
+
+/** @type {Array.<{
+ *      from_col: number,
+ *      from_row: number,
+ *      to_col: number,
+ *      to_row: number,
+ *      line_width: number,
+ *      arrow_size: number,
+ *      fg_color: string
+ * }>}
+ * @private
+ */
 var arrows = [];
-var arrow_targets = [];
+
+/** @type {Array.<Array.<boolean>>} */
 var occupied_by_arrows = [];
+
+var refutation_lines = [];
+
+/** @type {!number} @private */
+var move_num = 1;
+
+/** @type {!string} @private */
+var toplay = 'W';
+
+/** @type {number} @private */
 var ims = 0;
+
+/** @type {boolean} @private */
+var sort_refutation_lines_by_score = true;
+
+/** @type {boolean} @private */
+var truncate_display_history = true;
+
+/** @type {!string|undefined} @private */
 var highlight_from = undefined;
+
+/** @type {!string|undefined} @private */
 var highlight_to = undefined;
-var unique = Math.random();
 
-var request_update = function(board) {
+/** @type {?jQuery} @private */
+var highlighted_move = null;
+
+/** @type {?number} @private */
+var unique = null;
+
+/** @type {boolean} @private */
+var enable_sound = false;
+
+/**
+ * Our best estimate of how many milliseconds we need to add to 
+ * new Date() to get the true UTC time. Calibrated against the
+ * server clock.
+ *
+ * @type {?number}
+ * @private
+ */
+var client_clock_offset_ms = null;
+
+var clock_timer = null;
+
+/** The current position on the board, represented as a FEN string.
+ * @type {?string}
+ * @private
+ */
+var fen = null;
+
+/** @typedef {{
+ *    start_fen: string,
+ *    uci_pv: Array.<string>,
+ *    pretty_pv: Array.<string>,
+ *    line_num: number
+ * }} DisplayLine
+ */
+
+/** @type {Array.<DisplayLine>}
+ * @private
+ */
+var display_lines = [];
+
+/** @type {?DisplayLine} @private */
+var current_display_line = null;
+
+/** @type {boolean} @private */
+var current_display_line_is_history = false;
+
+/** @type {?number} @private */
+var current_display_move = null;
+
+var supports_html5_storage = function() {
+       try {
+               return 'localStorage' in window && window['localStorage'] !== null;
+       } catch (e) {
+               return false;
+       }
+}
+
+// Make the unique token persistent so people refreshing the page won't count twice.
+// Of course, you can never fully protect against people deliberately wanting to spam.
+var get_unique = function() {
+       var use_local_storage = supports_html5_storage();
+       if (use_local_storage && localStorage['unique']) {
+               return localStorage['unique'];
+       }
+       var unique = Math.random();
+       if (use_local_storage) {
+               localStorage['unique'] = unique;
+       }
+       return unique;
+}
+
+var request_update = function() {
        $.ajax({
-               url: "http://analysis.sesse.net/analysis.pl?ims=" + ims + "&unique=" + unique
-               //url: "http://analysis.sesse.net:5000/analysis.pl?ims=" + ims + "&unique=" + unique
+               url: "/analysis.pl?ims=" + ims + "&unique=" + unique
        }).done(function(data, textstatus, xhr) {
+               sync_server_clock(xhr.getResponseHeader('Date'));
                ims = xhr.getResponseHeader('X-Remoteglot-Last-Modified');
                var num_viewers = xhr.getResponseHeader('X-Remoteglot-Num-Viewers');
-               update_board(board, data, num_viewers);
+               possibly_play_sound(current_analysis_data, data);
+               current_analysis_data = data;
+               update_board(current_analysis_data, displayed_analysis_data);
+               update_num_viewers(num_viewers);
+
+               // Next update.
+               setTimeout(function() { request_update(); }, 100);
+       }).fail(function() {
+               // Wait ten seconds, then try again.
+               setTimeout(function() { request_update(); }, 10000);
        });
 }
 
+var possibly_play_sound = function(old_data, new_data) {
+       if (!enable_sound) {
+               return;
+       }
+       if (old_data === null) {
+               return;
+       }
+       var ding = document.getElementById('ding');
+       if (ding && ding.play) {
+               if (old_data['position'] && old_data['position']['fen'] &&
+                   new_data['position'] && new_data['position']['fen'] &&
+                   (old_data['position']['fen'] !== new_data['position']['fen'] ||
+                    old_data['position']['move_num'] !== new_data['position']['move_num'])) {
+                       ding.play();
+               }
+       }
+}
+
+/**
+ * @type {!string} server_date_string
+ */
+var sync_server_clock = function(server_date_string) {
+       var server_time_ms = new Date(server_date_string).getTime();
+       var client_time_ms = new Date().getTime();
+       var estimated_offset_ms = server_time_ms - client_time_ms;
+
+       // In order not to let the noise move us too much back and forth
+       // (the server only has one-second resolution anyway), we only
+       // change an existing skew if we are at least five seconds off.
+       if (client_clock_offset_ms === null ||
+           Math.abs(estimated_offset_ms - client_clock_offset_ms) > 5000) {
+               client_clock_offset_ms = estimated_offset_ms;
+       }
+}
+
 var clear_arrows = function() {
        for (var i = 0; i < arrows.length; ++i) {
-               jsPlumb.detach(arrows[i].connection1);
-               jsPlumb.detach(arrows[i].connection2);
+               if (arrows[i].svg) {
+                       arrows[i].svg.parentElement.removeChild(arrows[i].svg);
+                       delete arrows[i].svg;
+               }
        }
        arrows = [];
 
-       for (var i = 0; i < arrow_targets.length; ++i) {
-               document.body.removeChild(arrow_targets[i]);
-       }
-       arrow_targets = [];
-       
-       occupied_by_arrows = [];        
+       occupied_by_arrows = [];
        for (var y = 0; y < 8; ++y) {
                occupied_by_arrows.push([false, false, false, false, false, false, false, false]);
        }
@@ -44,6 +206,9 @@ var redraw_arrows = function() {
        }
 }
 
+/** @param {!number} x
+ * @return {!number}
+ */
 var sign = function(x) {
        if (x > 0) {
                return 1;
@@ -54,7 +219,11 @@ var sign = function(x) {
        }
 }
 
-// See if drawing this arrow on the board would cause unduly amount of confusion.
+/** See if drawing this arrow on the board would cause unduly amount of confusion.
+ * @param {!string} from The square the arrow is from (e.g. e4).
+ * @param {!string} to The square the arrow is to (e.g. e4).
+ * @return {boolean}
+ */
 var interfering_arrow = function(from, to) {
        var from_col = from.charCodeAt(0) - "a1".charCodeAt(0);
        var from_row = from.charCodeAt(1) - "a1".charCodeAt(1);
@@ -86,16 +255,63 @@ var interfering_arrow = function(from, to) {
        return false;
 }
 
-var add_target = function() {
-       var elem = document.createElement("div");
-       $(elem).addClass("window");
-       elem.id = "target" + arrow_targets.length;
-       document.body.appendChild(elem);        
-       arrow_targets.push(elem);
-       return elem.id;
+/** Find a point along the coordinate system given by the given line,
+ * <t> units forward from the start of the line, <u> units to the right of it.
+ * @param {!number} x1
+ * @param {!number} x2
+ * @param {!number} y1
+ * @param {!number} y2
+ * @param {!number} t
+ * @param {!number} u
+ * @return {!string} The point in "x y" form, suitable for SVG paths.
+ */
+var point_from_start = function(x1, y1, x2, y2, t, u) {
+       var dx = x2 - x1;
+       var dy = y2 - y1;
+
+       var norm = 1.0 / Math.sqrt(dx * dx + dy * dy);
+       dx *= norm;
+       dy *= norm;
+
+       var x = x1 + dx * t + dy * u;
+       var y = y1 + dy * t - dx * u;
+       return x + " " + y;
+}
+
+/** Find a point along the coordinate system given by the given line,
+ * <t> units forward from the end of the line, <u> units to the right of it.
+ * @param {!number} x1
+ * @param {!number} x2
+ * @param {!number} y1
+ * @param {!number} y2
+ * @param {!number} t
+ * @param {!number} u
+ * @return {!string} The point in "x y" form, suitable for SVG paths.
+ */
+var point_from_end = function(x1, y1, x2, y2, t, u) {
+       var dx = x2 - x1;
+       var dy = y2 - y1;
+
+       var norm = 1.0 / Math.sqrt(dx * dx + dy * dy);
+       dx *= norm;
+       dy *= norm;
+
+       var x = x2 + dx * t + dy * u;
+       var y = y2 + dy * t - dx * u;
+       return x + " " + y;
 }
-       
+
 var position_arrow = function(arrow) {
+       if (arrow.svg) {
+               arrow.svg.parentElement.removeChild(arrow.svg);
+               delete arrow.svg;
+       }
+       if (current_display_line !== null && !current_display_line_is_history) {
+               return;
+       }
+
+       var pos = $(".square-a8").position();
+
        var zoom_factor = $("#board").width() / 400.0;
        var line_width = arrow.line_width * zoom_factor;
        var arrow_size = arrow.arrow_size * zoom_factor;
@@ -106,68 +322,65 @@ var position_arrow = function(arrow) {
        var from_x = (arrow.from_col + 0.5)*square_width;
        var to_x = (arrow.to_col + 0.5)*square_width;
 
-       var dx = to_x - from_x;
-       var dy = to_y - from_y;
-       var len = Math.sqrt(dx * dx + dy * dy);
-       dx /= len;
-       dy /= len;
-       var pos = $(".square-a8").position();
-       $("#" + arrow.s1).css({ top: pos.top + from_y + (0.5 * arrow_size) * dy, left: pos.left + from_x + (0.5 * arrow_size) * dx });
-       $("#" + arrow.d1).css({ top: pos.top + to_y - (0.5 * arrow_size) * dy, left: pos.left + to_x - (0.5 * arrow_size) * dx });
-       $("#" + arrow.s1v).css({ top: pos.top + from_y - 0 * dy, left: pos.left + from_x - 0 * dx });
-       $("#" + arrow.d1v).css({ top: pos.top + to_y + 0 * dy, left: pos.left + to_x + 0 * dx });
-
-       if (arrow.connection1) {
-               jsPlumb.detach(arrow.connection1);
-       }
-       if (arrow.connection2) {
-               jsPlumb.detach(arrow.connection2);
-       }
-       arrow.connection1 = jsPlumb.connect({
-               source: arrow.s1,
-               target: arrow.d1,
-               connector:["Straight"],
-               cssClass:"c1",
-               endpoint:"Blank",
-               endpointClass:"c1Endpoint",                                                                                                        
-               anchor:"Continuous",
-               paintStyle:{ 
-                       lineWidth:line_width,
-                       strokeStyle:arrow.fg_color,
-                       outlineWidth:1,
-                       outlineColor:"#666",
-                       opacity:"60%"
-               }
-       });
-       arrow.connection2 = jsPlumb.connect({
-               source: arrow.s1v,
-               target: arrow.d1v,
-               connector:["Straight"],
-               cssClass:"vir",
-               endpoint:"Blank",
-               endpointClass:"c1Endpoint",                                                                                                        
-               anchor:"Continuous",
-               paintStyle:{ 
-                       lineWidth:0,
-                       strokeStyle:arrow.fg_color,
-                       outlineWidth:0,
-                       outlineColor:"#666",
-               },
-               overlays : [
-                       ["Arrow", {
-                               cssClass:"l1arrow",
-                               location:1.0,
-                               width: arrow_size,
-                               length: arrow_size,
-                               paintStyle: { 
-                                       lineWidth:line_width,
-                                       strokeStyle:"#000",
-                               },
-                       }]
-               ]
-       });
+       var SVG_NS = "http://www.w3.org/2000/svg";
+       var XHTML_NS = "http://www.w3.org/1999/xhtml";
+       var svg = document.createElementNS(SVG_NS, "svg");
+       svg.setAttribute("width", /** @type{number} */ ($("#board").width()));
+       svg.setAttribute("height", /** @type{number} */ ($("#board").height()));
+       svg.setAttribute("style", "position: absolute");
+       svg.setAttribute("position", "absolute");
+       svg.setAttribute("version", "1.1");
+       svg.setAttribute("class", "c1");
+       svg.setAttribute("xmlns", XHTML_NS);
+
+       var x1 = from_x;
+       var y1 = from_y;
+       var x2 = to_x;
+       var y2 = to_y;
+
+       // Draw the line.
+       var outline = document.createElementNS(SVG_NS, "path");
+       outline.setAttribute("d", "M " + point_from_start(x1, y1, x2, y2, arrow_size / 2, 0) + " L " + point_from_end(x1, y1, x2, y2, -arrow_size / 2, 0));
+       outline.setAttribute("xmlns", XHTML_NS);
+       outline.setAttribute("stroke", "#666");
+       outline.setAttribute("stroke-width", line_width + 2);
+       outline.setAttribute("fill", "none");
+       svg.appendChild(outline);
+
+       var path = document.createElementNS(SVG_NS, "path");
+       path.setAttribute("d", "M " + point_from_start(x1, y1, x2, y2, arrow_size / 2, 0) + " L " + point_from_end(x1, y1, x2, y2, -arrow_size / 2, 0));
+       path.setAttribute("xmlns", XHTML_NS);
+       path.setAttribute("stroke", arrow.fg_color);
+       path.setAttribute("stroke-width", line_width);
+       path.setAttribute("fill", "none");
+       svg.appendChild(path);
+
+       // Then the arrow head.
+       var head = document.createElementNS(SVG_NS, "path");
+       head.setAttribute("d",
+               "M " +  point_from_end(x1, y1, x2, y2, 0, 0) +
+               " L " + point_from_end(x1, y1, x2, y2, -arrow_size, -arrow_size / 2) +
+               " L " + point_from_end(x1, y1, x2, y2, -arrow_size * .623, 0.0) +
+               " L " + point_from_end(x1, y1, x2, y2, -arrow_size, arrow_size / 2) +
+               " L " + point_from_end(x1, y1, x2, y2, 0, 0));
+       head.setAttribute("xmlns", XHTML_NS);
+       head.setAttribute("stroke", "#000");
+       head.setAttribute("stroke-width", "1");
+       head.setAttribute("fill", arrow.fg_color);
+       svg.appendChild(head);
+
+       $(svg).css({ top: pos.top, left: pos.left });
+       document.body.appendChild(svg);
+       arrow.svg = svg;
 }
 
+/**
+ * @param {!string} from_square
+ * @param {!string} to_square
+ * @param {!string} fg_color
+ * @param {number} line_width
+ * @param {number} arrow_size
+ */
 var create_arrow = function(from_square, to_square, fg_color, line_width, arrow_size) {
        var from_col = from_square.charCodeAt(0) - "a1".charCodeAt(0);
        var from_row = from_square.charCodeAt(1) - "a1".charCodeAt(1);
@@ -176,16 +389,12 @@ var create_arrow = function(from_square, to_square, fg_color, line_width, arrow_
 
        // Create arrow.
        var arrow = {
-               s1: add_target(),
-               d1: add_target(),
-               s1v: add_target(),
-               d1v: add_target(),
                from_col: from_col,
                from_row: from_row,
                to_col: to_col,
                to_row: to_row,
                line_width: line_width,
-               arrow_size: arrow_size, 
+               arrow_size: arrow_size,
                fg_color: fg_color
        };
 
@@ -193,23 +402,45 @@ var create_arrow = function(from_square, to_square, fg_color, line_width, arrow_
        arrows.push(arrow);
 }
 
-// Fake multi-PV using the refutation lines. Find all “relevant” moves,
-// sorted by quality, descending.
+var compare_by_sort_key = function(refutation_lines, a, b) {
+       var ska = refutation_lines[a]['sort_key'];
+       var skb = refutation_lines[b]['sort_key'];
+       if (ska < skb) return -1;
+       if (ska > skb) return 1;
+       return 0;
+};
+
+var compare_by_score = function(refutation_lines, a, b) {
+       var sa = parseInt(refutation_lines[b]['score_sort_key'], 10);
+       var sb = parseInt(refutation_lines[a]['score_sort_key'], 10);
+       return sa - sb;
+}
+
+/**
+ * Fake multi-PV using the refutation lines. Find all “relevant” moves,
+ * sorted by quality, descending.
+ *
+ * @param {!Object} data
+ * @param {number} margin The maximum number of centipawns worse than the
+ *     best move can be and still be included.
+ * @return {Array.<string>} The UCI representation (e.g. e1g1) of all
+ *     moves, in score order.
+ */
 var find_nonstupid_moves = function(data, margin) {
        // First of all, if there are any moves that are more than 0.5 ahead of
        // the primary move, the refutation lines are probably bunk, so just
        // kill them all. 
        var best_score = undefined;
        var pv_score = undefined;
-       for (var move in data.refutation_lines) {
-               var score = parseInt(data.refutation_lines[move].score_sort_key);
-               if (move == data.pv_uci[0]) {
+       for (var move in data['refutation_lines']) {
+               var score = parseInt(data['refutation_lines'][move]['score_sort_key'], 10);
+               if (move == data['pv_uci'][0]) {
                        pv_score = score;
                }
                if (best_score === undefined || score > best_score) {
                        best_score = score;
                }
-               if (!(data.refutation_lines[move].depth >= 8)) {
+               if (!(data['refutation_lines'][move]['depth'] >= 8)) {
                        return [];
                }
        }
@@ -221,133 +452,331 @@ var find_nonstupid_moves = function(data, margin) {
        // Now find all moves that are within “margin” of the best score.
        // The PV move will always be first.
        var moves = [];
-       for (var move in data.refutation_lines) {
-               var score = parseInt(data.refutation_lines[move].score_sort_key);
-               if (move != data.pv_uci[0] && best_score - score <= margin) {
+       for (var move in data['refutation_lines']) {
+               var score = parseInt(data['refutation_lines'][move]['score_sort_key'], 10);
+               if (move != data['pv_uci'][0] && best_score - score <= margin) {
                        moves.push(move);
                }
        }
-       moves = moves.sort(function(a, b) { return parseInt(data.refutation_lines[b].score_sort_key) - parseInt(data.refutation_lines[a].score_sort_key); });
-       moves.unshift(data.pv_uci[0]);
+       moves = moves.sort(function(a, b) { return compare_by_score(data['refutation_lines'], a, b) });
+       moves.unshift(data['pv_uci'][0]);
 
        return moves;
 }
 
+/**
+ * @param {number} x
+ * @return {!string}
+ */
 var thousands = function(x) {
        return String(x).split('').reverse().join('').replace(/(\d{3}\B)/g, '$1,').split('').reverse().join('');
 }
 
-var print_pv = function(pretty_pv, move_num, toplay, limit) {
+/**
+ * @param {!string} fen
+ * @param {Array.<string>} pretty_pv
+ * @param {number} move_num
+ * @param {!string} toplay
+ * @param {number=} opt_limit
+ * @param {boolean=} opt_showlast
+ */
+var add_pv = function(fen, pretty_pv, move_num, toplay, opt_limit, opt_showlast) {
+       display_lines.push({
+               start_fen: fen,
+               pretty_pv: pretty_pv,
+               line_number: display_lines.length
+       });
+       return print_pv(display_lines.length - 1, pretty_pv, move_num, toplay, opt_limit, opt_showlast);
+}
+
+/**
+ * @param {number} line_num
+ * @param {Array.<string>} pretty_pv
+ * @param {number} move_num
+ * @param {!string} toplay
+ * @param {number=} opt_limit
+ * @param {boolean=} opt_showlast
+ */
+var print_pv = function(line_num, pretty_pv, move_num, toplay, opt_limit, opt_showlast) {
        var pv = '';
        var i = 0;
-       if (toplay == 'B') {
-               pv = move_num + '. … ' + pretty_pv[0];
+       if (opt_limit && opt_showlast && pretty_pv.length > opt_limit) {
+               // Truncate the PV at the beginning (instead of at the end).
+               // We assume here that toplay is 'W'. We also assume that if
+               // opt_showlast is set, then it is the history, and thus,
+               // the UI should be to expand the history.
+               pv = '(<a class="move" href="javascript:collapse_history(false)">…</a>) ';
+               i = pretty_pv.length - opt_limit;
+               if (i % 2 == 1) {
+                       ++i;
+               }
+               move_num += i / 2;
+       } else if (toplay == 'B' && pretty_pv.length > 0) {
+               var move = "<a class=\"move\" id=\"automove" + line_num + "-0\" href=\"javascript:show_line(" + line_num + ", " + 0 + ");\">" + pretty_pv[0] + "</a>";
+               pv = move_num + '. … ' + move;
                toplay = 'W';
-               ++i;    
+               ++i;
+               ++move_num;
        }
-       ++move_num;
        for ( ; i < pretty_pv.length; ++i) {
+               var move = "<a class=\"move\" id=\"automove" + line_num + "-" + i + "\" href=\"javascript:show_line(" + line_num + ", " + i + ");\">" + pretty_pv[i] + "</a>";
+
                if (toplay == 'W') {
-                       if (i > limit) {
+                       if (i > opt_limit && !opt_showlast) {
                                return pv + ' (…)';
                        }
                        if (pv != '') {
                                pv += ' ';
                        }
-                       pv += move_num + '. ' + pretty_pv[i];
+                       pv += move_num + '. ' + move;
                        ++move_num;
                        toplay = 'B';
                } else {
-                       pv += ' ' + pretty_pv[i];
+                       pv += ' ' + move;
                        toplay = 'W';
                }
        }
        return pv;
 }
 
-var compare_by_sort_key = function(data, a, b) {
-       var ska = data.refutation_lines[a].sort_key;
-       var skb = data.refutation_lines[b].sort_key;
-       if (ska < skb) return -1;
-       if (ska > skb) return 1;
-       return 0;
-};
-       
-var update_highlight = function()  {
+var update_highlight = function() {
        $("#board").find('.square-55d63').removeClass('nonuglyhighlight');
-       if (highlight_from !== undefined && highlight_to !== undefined) {
+       if ((current_display_line === null || current_display_line_is_history) &&
+           highlight_from !== undefined && highlight_to !== undefined) {
                $("#board").find('.square-' + highlight_from).addClass('nonuglyhighlight');
                $("#board").find('.square-' + highlight_to).addClass('nonuglyhighlight');
        }
 }
 
-var update_board = function(board, data, num_viewers) {
-       // The headline.
-       var headline = 'Analysis';
-       if (data.position.last_move !== 'none') {
-               headline += ' after ' + data.position.move_num + '. ';
-               if (data.position.toplay == 'W') {
-                       headline += '… ';
+var update_history = function() {
+       if (display_lines[0] === null || display_lines[0].pretty_pv.length == 0) {
+               $("#history").html("No history");
+       } else if (truncate_display_history) {
+               $("#history").html(print_pv(0, display_lines[0].pretty_pv, 1, 'W', 8, true));
+       } else {
+               $("#history").html(
+                       '(<a class="move" href="javascript:collapse_history(true)">collapse</a>) ' +
+                       print_pv(0, display_lines[0].pretty_pv, 1, 'W'));
+       }
+}
+
+/**
+ * @param {!boolean} truncate_history
+ */
+var collapse_history = function(truncate_history) {
+       truncate_display_history = truncate_history;
+       update_history();
+}
+window['collapse_history'] = collapse_history;
+
+var update_refutation_lines = function() {
+       if (fen === null) {
+               return;
+       }
+       if (display_lines.length > 2) {
+               display_lines = [ display_lines[0], display_lines[1] ];
+       }
+
+       var tbl = $("#refutationlines");
+       tbl.empty();
+
+       var moves = [];
+       for (var move in refutation_lines) {
+               moves.push(move);
+       }
+       var compare = sort_refutation_lines_by_score ? compare_by_score : compare_by_sort_key;
+       moves = moves.sort(function(a, b) { return compare(refutation_lines, a, b) });
+       for (var i = 0; i < moves.length; ++i) {
+               var line = refutation_lines[moves[i]];
+
+               var tr = document.createElement("tr");
+
+               var move_td = document.createElement("td");
+               tr.appendChild(move_td);
+               $(move_td).addClass("move");
+               if (line['pv_pretty'].length == 0) {
+                       $(move_td).text(line['pretty_move']);
+               } else {
+                       var move = "<a class=\"move\" href=\"javascript:show_line(" + display_lines.length + ", " + 0 + ");\">" + line['pretty_move'] + "</a>";
+                       $(move_td).html(move);
                }
-               headline += data.position.last_move;
+
+               var score_td = document.createElement("td");
+               tr.appendChild(score_td);
+               $(score_td).addClass("score");
+               $(score_td).text(line['pretty_score']);
+
+               var depth_td = document.createElement("td");
+               tr.appendChild(depth_td);
+               $(depth_td).addClass("depth");
+               $(depth_td).text("d" + line['depth']);
+
+               var pv_td = document.createElement("td");
+               tr.appendChild(pv_td);
+               $(pv_td).addClass("pv");
+               $(pv_td).html(add_pv(fen, line['pv_pretty'], move_num, toplay, 10));
+
+               tbl.append(tr);
+       }
+
+       // Make one of the links clickable and the other nonclickable.
+       if (sort_refutation_lines_by_score) {
+               $("#sortbyscore0").html("<a href=\"javascript:resort_refutation_lines(false)\">Move</a>");
+               $("#sortbyscore1").html("<strong>Score</strong>");
+       } else {
+               $("#sortbyscore0").html("<strong>Move</strong>");
+               $("#sortbyscore1").html("<a href=\"javascript:resort_refutation_lines(true)\">Score</a>");
+       }
+}
+
+/**
+ * @param {Object} data
+ * @param {?Object} display_data
+ */
+var update_board = function(current_data, display_data) {
+       var data = display_data || current_data;
+
+       display_lines = [];
+
+       // Print the history. This is pretty much the only thing that's
+       // unconditionally taken from current_data (we're not interested in
+       // historic history).
+       if (current_data['position']['pretty_history']) {
+               add_pv('start', current_data['position']['pretty_history'], 1, 'W', 8, true);
+       } else {
+               display_lines.push(null);
+       }
+       update_history();
+
+       // The headline. Names are always fetched from current_data;
+       // the rest can depend a bit.
+       var headline;
+       if (current_data &&
+           current_data['position']['player_w'] && current_data['position']['player_b']) {
+               headline = current_data['position']['player_w'] + '–' +
+                       current_data['position']['player_b'] + ', analysis';
+       } else {
+               headline = 'Analysis';
        }
 
+       var last_move;
+       if (display_data) {
+               // Displaying some non-current position, pick out the last move
+               // from the history. This will work even if the fetch failed.
+               last_move = format_move_with_number(
+                       current_display_line.pretty_pv[current_display_move],
+                       Math.floor((current_display_move + 1) / 2) + 1,
+                       (current_display_move % 2 == 1));
+               headline += ' after ' + last_move;
+       } else if (data['position']['last_move'] !== 'none') {
+               last_move = format_move_with_number(
+                       data['position']['last_move'],
+                       data['position']['move_num'],
+                       data['position']['toplay'] == 'W');
+               headline += ' after ' + last_move;
+       } else {
+               last_move = null;
+       }
        $("#headline").text(headline);
 
-       if (num_viewers === null) {
-               $("#numviewers").text("");
-       } else if (num_viewers == 1) {
-               $("#numviewers").text("You are the only current viewer");
+       // The <title> contains a very brief headline.
+       var title_elems = [];
+       if (data['short_score'] !== undefined && data['short_score'] !== null) {
+               title_elems.push(data['short_score'].replace(/^ /, ""));
+       }
+       if (last_move !== null) {
+               title_elems.push(last_move);
+       }
+
+       if (title_elems.length != 0) {
+               document.title = '(' + title_elems.join(', ') + ') analysis.sesse.net';
        } else {
-               $("#numviewers").text(num_viewers + " current viewers");
+               document.title = 'analysis.sesse.net';
+       }
+
+       // The last move (shown by highlighting the from and to squares).
+       if (data['position'] && data['position']['last_move_uci']) {
+               highlight_from = data['position']['last_move_uci'].substr(0, 2);
+               highlight_to = data['position']['last_move_uci'].substr(2, 2);
+       } else if (current_display_line_is_history && current_display_move >= 0) {
+               // We don't have historic analysis for this position, but we
+               // can reconstruct what the last move was by just replaying
+               // from the start.
+               var hiddenboard = new Chess();
+               for (var i = 0; i <= current_display_move; ++i) {
+                       hiddenboard.move(current_display_line.pretty_pv[i]);
+               }
+               var moves = hiddenboard.history({ verbose: true });
+               var last_move = moves.pop();
+               highlight_from = last_move.from;
+               highlight_to = last_move.to;
+       } else {
+               highlight_from = highlight_to = undefined;
+       }
+       update_highlight();
+
+       if (data['failed']) {
+               $("#score").text("No analysis for this move");
+               $("#pv").empty();
+               $("#searchstats").html("&nbsp;");
+               $("#refutationlines").empty();
+               $("#whiteclock").empty();
+               $("#blackclock").empty();
+               refutation_lines = [];
+               update_refutation_lines();
+               clear_arrows();
+               update_displayed_line();
+               return;
+       }
+
+       update_clock();
+
+       // The engine id.
+       if (data['id'] && data['id']['name'] !== null) {
+               $("#engineid").text(data['id']['name']);
        }
 
        // The score.
-       if (data.score !== null) {
-               $("#score").text(data.score);
+       if (data['score'] !== null) {
+               $("#score").text(data['score']);
        }
 
        // The search stats.
-       if (data.nodes && data.nps && data.depth) {
-               var stats = thousands(data.nodes) + ' nodes, ' + thousands(data.nps) + ' nodes/sec, depth ' + data.depth + ' ply';
-               if (data.seldepth) {
-                       stats += ' (' + data.seldepth + ' selective)';
+       if (data['tablebase'] == 1) {
+               $("#searchstats").text("Tablebase result");
+       } else if (data['nodes'] && data['nps'] && data['depth']) {
+               var stats = thousands(data['nodes']) + ' nodes, ' + thousands(data['nps']) + ' nodes/sec, depth ' + data['depth'] + ' ply';
+               if (data['seldepth']) {
+                       stats += ' (' + data['seldepth'] + ' selective)';
                }
-               if (data.tbhits && data.tbhits > 0) {
-                       if (data.tbhits == 1) {
-                               stats += ', one Nalimov hit';
+               if (data['tbhits'] && data['tbhits'] > 0) {
+                       if (data['tbhits'] == 1) {
+                               stats += ', one Syzygy hit';
                        } else {
-                               stats += ', ' + data.tbhits + ' Nalimov hits';
+                               stats += ', ' + thousands(data['tbhits']) + ' Syzygy hits';
                        }
                }
-               
 
                $("#searchstats").text(stats);
+       } else {
+               $("#searchstats").text("");
        }
 
        // Update the board itself.
-       board.position(data.position.fen);
-
-       if (data.position.last_move_uci) {
-               highlight_from = data.position.last_move_uci.substr(0, 2);
-               highlight_to = data.position.last_move_uci.substr(2, 4);
-       } else {
-               highlight_from = highlight_to = undefined;
-       }
-       update_highlight();
+       fen = data['position']['fen'];
+       update_displayed_line();
 
        // Print the PV.
-       var pv = print_pv(data.pv_pretty, data.position.move_num, data.position.toplay);
-       $("#pv").text(pv);
+       $("#pv").html(add_pv(data['position']['fen'], data['pv_pretty'], data['position']['move_num'], data['position']['toplay']));
 
        // Update the PV arrow.
        clear_arrows();
-       if (data.pv_uci.length >= 1) {
+       if (data['pv_uci'].length >= 1) {
                // draw a continuation arrow as long as it's the same piece
-               for (var i = 0; i < data.pv_uci.length; i += 2) {
-                       var from = data.pv_uci[i].substr(0, 2);
-                       var to = data.pv_uci[i].substr(2,4);
-                       if ((i >= 2 && from != data.pv_uci[i - 2].substr(2, 4)) ||
+               for (var i = 0; i < data['pv_uci'].length; i += 2) {
+                       var from = data['pv_uci'][i].substr(0, 2);
+                       var to = data['pv_uci'][i].substr(2,4);
+                       if ((i >= 2 && from != data['pv_uci'][i - 2].substr(2, 2)) ||
                             interfering_arrow(from, to)) {
                                break;
                        }
@@ -357,28 +786,28 @@ var update_board = function(board, data, num_viewers) {
                var alt_moves = find_nonstupid_moves(data, 30);
                for (var i = 1; i < alt_moves.length && i < 3; ++i) {
                        create_arrow(alt_moves[i].substr(0, 2),
-                                    alt_moves[i].substr(2, 4), '#f66', 1, 10);
+                                    alt_moves[i].substr(2, 2), '#f66', 1, 10);
                }
        }
 
        // See if all semi-reasonable moves have only one possible response.
-       if (data.pv_uci.length >= 2) {
+       if (data['pv_uci'].length >= 2) {
                var nonstupid_moves = find_nonstupid_moves(data, 300);
-               var response = data.pv_uci[1];
+               var response = data['pv_uci'][1];
                for (var i = 0; i < nonstupid_moves.length; ++i) {
-                       if (nonstupid_moves[i] == data.pv_uci[0]) {
+                       if (nonstupid_moves[i] == data['pv_uci'][0]) {
                                // ignore the PV move for refutation lines.
                                continue;
                        }
-                       if (!data.refutation_lines ||
-                           !data.refutation_lines[nonstupid_moves[i]] ||
-                           !data.refutation_lines[nonstupid_moves[i]].pv_uci ||
-                           data.refutation_lines[nonstupid_moves[i]].pv_uci.length < 1) {
+                       if (!data['refutation_lines'] ||
+                           !data['refutation_lines'][nonstupid_moves[i]] ||
+                           !data['refutation_lines'][nonstupid_moves[i]]['pv_uci'] ||
+                           data['refutation_lines'][nonstupid_moves[i]]['pv_uci'].length < 1) {
                                // Incomplete PV, abort.
                                response = undefined;
                                break;
                        }
-                       var this_response = data.refutation_lines[nonstupid_moves[i]].pv_uci[1];
+                       var this_response = data['refutation_lines'][nonstupid_moves[i]]['pv_uci'][1];
                        if (response !== this_response) {
                                // Different response depending on lines, abort.
                                response = undefined;
@@ -388,61 +817,322 @@ var update_board = function(board, data, num_viewers) {
 
                if (nonstupid_moves.length > 0 && response !== undefined) {
                        create_arrow(response.substr(0, 2),
-                                    response.substr(2, 4), '#66f', 6, 20);
+                                    response.substr(2, 2), '#66f', 6, 20);
                }
        }
 
-       // Show the refutation lines.
-       var tbl = $("#refutationlines");
-       tbl.empty();
+       // Update the refutation lines.
+       fen = data['position']['fen'];
+       move_num = data['position']['move_num'];
+       toplay = data['position']['toplay'];
+       refutation_lines = data['refutation_lines'];
+       update_refutation_lines();
+}
 
-       moves = [];
-       for (var move in data.refutation_lines) {
-               moves.push(move);
+/**
+ * @param {number} num_viewers
+ */
+var update_num_viewers = function(num_viewers) {
+       if (num_viewers === null) {
+               $("#numviewers").text("");
+       } else if (num_viewers == 1) {
+               $("#numviewers").text("You are the only current viewer");
+       } else {
+               $("#numviewers").text(num_viewers + " current viewers");
        }
-       moves = moves.sort(function(a, b) { return compare_by_sort_key(data, a, b) });
-       for (var i = 0; i < moves.length; ++i) {
-               var line = data.refutation_lines[moves[i]];
+}
 
-               var tr = document.createElement("tr");
+var update_clock = function() {
+       clearTimeout(clock_timer);
 
-               var move_td = document.createElement("td");
-               tr.appendChild(move_td);
-               $(move_td).addClass("move");
-               $(move_td).text(line.pretty_move);
+       var data = displayed_analysis_data || current_analysis_data;
+       if (data['position']) {
+               var result = data['position']['result'];
+               if (result === '1-0') {
+                       $("#whiteclock").text("1");
+                       $("#blackclock").text("0");
+                       return;
+               }
+               if (result === '1/2-1/2') {
+                       $("#whiteclock").text("1/2");
+                       $("#blackclock").text("1/2");
+                       return;
+               }       
+               if (result === '0-1') {
+                       $("#whiteclock").text("0");
+                       $("#blackclock").text("1");
+                       return;
+               }
+       }
 
-               var score_td = document.createElement("td");
-               tr.appendChild(score_td);
-               $(score_td).addClass("score");
-               $(score_td).text(line.pretty_score);
+       var white_clock = "";
+       var black_clock = "";
 
-               var depth_td = document.createElement("td");
-               tr.appendChild(depth_td);
-               $(depth_td).addClass("depth");
-               $(depth_td).text("d" + line.depth);
+       // Static clocks.
+       if (data['position'] &&
+           data['position']['white_clock'] &&
+           data['position']['black_clock']) {
+               white_clock = data['position']['white_clock'];
+               black_clock = data['position']['black_clock'];
+       }
 
-               var pv_td = document.createElement("td");
-               tr.appendChild(pv_td);
-               $(pv_td).addClass("pv");
-               $(pv_td).text(print_pv(line.pv_pretty, data.position.move_num, data.position.toplay, 10));
+       // Dynamic clock (only one, obviously).
+       var color;
+       if (data['position']['white_clock_target']) {
+               color = "white";
+       } else if (data['position']['black_clock_target']) {
+               color = "black";
+       }
+       if (color) {
+               var now = new Date().getTime() + client_clock_offset_ms;
+               var remaining_ms = data['position'][color + '_clock_target'] * 1000 - now;
+               if (color === "white") {
+                       white_clock = format_clock(remaining_ms);
+               } else {
+                       black_clock = format_clock(remaining_ms);
+               }
 
-               tbl.append(tr);
+               // See when the clock will change next, and update right after that.
+               var next_update_ms = remaining_ms % 1000 + 100;
+               clock_timer = setTimeout(update_clock, next_update_ms);
+       }
+
+       $("#whiteclock").text(white_clock);
+       $("#blackclock").text(black_clock);
+}
+
+/**
+ * @param {Number} remaining_ms
+ */
+var format_clock = function(remaining_ms) {
+       if (remaining_ms <= 0) {
+               return "00:00:00";
        }
 
-       // Next update.
-       setTimeout(function() { request_update(board); }, 100);
+       var remaining = Math.floor(remaining_ms / 1000);
+       var seconds = remaining % 60;
+       remaining = (remaining - seconds) / 60;
+       var minutes = remaining % 60;
+       remaining = (remaining - minutes) / 60;
+       var hours = remaining;
+       return format_2d(hours) + ":" + format_2d(minutes) + ":" + format_2d(seconds);  
 }
 
+/**
+ * @param {Number} x
+ */
+var format_2d = function(x) {
+       if (x >= 10) {
+               return x;
+       } else {
+               return "0" + x;
+       }
+}
+
+/**
+ * @param {string} move
+ * @param {Number} move_num
+ * @param {boolean} white_to_play
+ */
+var format_move_with_number = function(move, move_num, white_to_play) {
+       var ret;
+       if (white_to_play) {
+               ret = (move_num - 1) + '… ';
+       } else {
+               ret = move_num + '. ';
+       }
+       ret += move;
+       return ret;
+}
+
+/**
+ * @param {boolean} sort_by_score
+ */
+var resort_refutation_lines = function(sort_by_score) {
+       sort_refutation_lines_by_score = sort_by_score;
+       if (supports_html5_storage()) {
+               localStorage['sort_refutation_lines_by_score'] = sort_by_score ? 1 : 0;
+       }
+       update_refutation_lines();
+}
+window['resort_refutation_lines'] = resort_refutation_lines;
+
+/**
+ * @param {boolean} truncate_history
+ */
+var set_truncate_history = function(truncate_history) {
+       truncate_display_history = truncate_history;
+       update_refutation_lines();
+}
+window['set_truncate_history'] = set_truncate_history;
+
+/**
+ * @param {number} line_num
+ * @param {number} move_num
+ */
+var show_line = function(line_num, move_num) {
+       if (line_num == -1) {
+               current_display_line = null;
+               current_display_move = null;
+               if (displayed_analysis_data) {
+                       // TODO: Support exiting to history position if we are in an
+                       // analysis line of a history position.
+                       displayed_analysis_data = null;
+                       update_board(current_analysis_data, displayed_analysis_data);
+               }
+       } else {
+               current_display_line = display_lines[line_num];
+               current_display_move = move_num;
+       }
+       current_display_line_is_history = (line_num == 0);
+
+       update_historic_analysis();
+       update_displayed_line();
+       update_highlight();
+       redraw_arrows();
+}
+window['show_line'] = show_line;
+
+var prev_move = function() {
+       if (current_display_move > -1) {
+               --current_display_move;
+       }
+       update_historic_analysis();
+       update_displayed_line();
+}
+window['prev_move'] = prev_move;
+
+var next_move = function() {
+       if (current_display_line && current_display_move < current_display_line.pretty_pv.length - 1) {
+               ++current_display_move;
+       }
+       update_historic_analysis();
+       update_displayed_line();
+}
+window['next_move'] = next_move;
+
+var update_historic_analysis = function() {
+       if (!current_display_line_is_history) {
+               return;
+       }
+       if (current_display_move == current_display_line.pretty_pv.length - 1) {
+               displayed_analysis_data = null;
+               update_board(current_analysis_data, displayed_analysis_data);
+       }
+
+       // Fetch old analysis for this line if it exists.
+       var hiddenboard = new Chess();
+       for (var i = 0; i <= current_display_move; ++i) {
+               hiddenboard.move(current_display_line.pretty_pv[i]);
+       }
+       var filename = "/history/move" + (current_display_move + 1) + "-" +
+               hiddenboard.fen().replace(/ /g, '_').replace(/\//g, '-') + ".json";
+
+       $.ajax({
+               url: filename
+       }).done(function(data, textstatus, xhr) {
+               displayed_analysis_data = data;
+               update_board(current_analysis_data, displayed_analysis_data);
+       }).fail(function() {
+               displayed_analysis_data = {'failed': true};
+               update_board(current_analysis_data, displayed_analysis_data);
+       });
+}
+
+var update_displayed_line = function() {
+       if (highlighted_move !== null) {
+               highlighted_move.removeClass('highlight'); 
+       }
+       if (current_display_line === null) {
+               $("#linenav").hide();
+               $("#linemsg").show();
+               board.position(fen);
+               return;
+       }
+
+       $("#linenav").show();
+       $("#linemsg").hide();
+
+       if (current_display_move <= 0) {
+               $("#prevmove").html("Previous");
+       } else {
+               $("#prevmove").html("<a href=\"javascript:prev_move();\">Previous</a></span>");
+       }
+       if (current_display_move == current_display_line.pretty_pv.length - 1) {
+               $("#nextmove").html("Next");
+       } else {
+               $("#nextmove").html("<a href=\"javascript:next_move();\">Next</a></span>");
+       }
+
+       var hiddenboard = new Chess();
+       hiddenboard.load(current_display_line.start_fen);
+       for (var i = 0; i <= current_display_move; ++i) {
+               hiddenboard.move(current_display_line.pretty_pv[i]);
+       }
+
+       highlighted_move = $("#automove" + current_display_line.line_number + "-" + current_display_move);
+       highlighted_move.addClass('highlight'); 
+
+       board.position(hiddenboard.fen());
+}
+
+/**
+ * @param {boolean} param_enable_sound
+ */
+var set_sound = function(param_enable_sound) {
+       enable_sound = param_enable_sound;
+       if (enable_sound) {
+               $("#soundon").html("<strong>On</strong>");
+               $("#soundoff").html("<a href=\"javascript:set_sound(false)\">Off</a>");
+
+               // Seemingly at least Firefox prefers MP3 over Opus; tell it otherwise,
+               // and also preload the file since the user has selected audio.
+               var ding = document.getElementById('ding');
+               if (ding && ding.canPlayType && ding.canPlayType('audio/ogg; codecs="opus"') === 'probably') {
+                       ding.src = 'ding.opus';
+                       ding.load();
+               }
+       } else {
+               $("#soundon").html("<a href=\"javascript:set_sound(true)\">On</a>");
+               $("#soundoff").html("<strong>Off</strong>");
+       }
+       if (supports_html5_storage()) {
+               localStorage['enable_sound'] = enable_sound ? 1 : 0;
+       }
+}
+window['set_sound'] = set_sound;
+
 var init = function() {
+       unique = get_unique();
+
+       // Load settings from HTML5 local storage if available.
+       if (supports_html5_storage() && localStorage['enable_sound']) {
+               set_sound(parseInt(localStorage['enable_sound']));
+       } else {
+               set_sound(false);
+       }
+       if (supports_html5_storage() && localStorage['sort_refutation_lines_by_score']) {
+               sort_refutation_lines_by_score = parseInt(localStorage['sort_refutation_lines_by_score']);
+       } else {
+               sort_refutation_lines_by_score = true;
+       }
+
        // Create board.
-       board = new ChessBoard('board', 'start');
+       board = new window.ChessBoard('board', 'start');
 
-       request_update(board);
+       request_update();
        $(window).resize(function() {
                board.resize();
                update_highlight();
                redraw_arrows();
        });
+       $(window).keyup(function(event) {
+               if (event.which == 39) {
+                       next_move();
+               } else if (event.which == 37) {
+                       prev_move();
+               }
+       });
 };
 $(document).ready(init);