+/**
+ * @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;
+