+ 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;
+ } else {
+ current_display_line = display_lines[line_num];
+ current_display_move = move_num;
+ }
+ 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_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_displayed_line();
+}
+window['next_move'] = next_move;
+
+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.uci_pv.length - 1) {
+ $("#nextmove").html("Next");
+ } else {
+ $("#nextmove").html("<a href=\"javascript:next_move();\">Next</a></span>");
+ }
+
+ hiddenboard.position(current_display_line.start_fen, false);
+ for (var i = 0; i <= current_display_move; ++i) {
+ var pos = hiddenboard.position();
+ var move = current_display_line.uci_pv[i];
+ var source = move.substr(0, 2);
+ var target = move.substr(2, 2);
+ var promo = move.substr(4, 1);
+
+ // Check if we need to do en passant.
+ var piece = pos[source];
+ if (piece == "wP" || piece == "bP") {
+ if (source.substr(0, 1) != target.substr(0, 1) &&
+ pos[target] === undefined) {
+ var ep_square = target.substr(0, 1) + source.substr(1, 1);
+ delete pos[ep_square];
+ hiddenboard.position(pos, false);
+ }
+ }
+
+ move = source + "-" + target;
+ hiddenboard.move(move, false);
+ pos = hiddenboard.position();
+
+ // Do promotion if needed.
+ if (promo != "") {
+ pos[target] = pos[target].substr(0, 1) + promo.toUpperCase();
+ hiddenboard.position(pos, false);
+ }
+
+ // chessboard.js does not automatically move the rook on castling
+ // (issue #51; marked as won't fix), so update it ourselves.
+ if (move == "e1-g1" && hiddenboard.position().g1 == "wK") { // white O-O
+ hiddenboard.move("h1-f1", false);
+ } else if (move == "e1-c1" && hiddenboard.position().c1 == "wK") { // white O-O-O
+ hiddenboard.move("a1-d1", false);
+ } else if (move == "e8-g8" && hiddenboard.position().g8 == "bK") { // black O-O
+ hiddenboard.move("h8-f8", false);
+ } else if (move == "e8-c8" && hiddenboard.position().c8 == "bK") { // black O-O-O
+ hiddenboard.move("a8-d8", false);
+ }
+ }
+
+ highlighted_move = $("#automove" + current_display_line.line_number + "-" + current_display_move);
+ highlighted_move.addClass('highlight');
+
+ board.position(hiddenboard.position());