+ // Update the sparkline last, since its size depends on how everything else reflowed.
+ update_sparkline(data);
+}
+
+var update_sparkline = function(data) {
+ if (data && data['score_history']) {
+ var first_move_num = undefined;
+ for (var halfmove_num in data['score_history']) {
+ halfmove_num = parseInt(halfmove_num);
+ if (first_move_num === undefined || halfmove_num < first_move_num) {
+ first_move_num = halfmove_num;
+ }
+ }
+ if (first_move_num !== undefined) {
+ var last_move_num = data['position']['move_num'] * 2 - 3;
+ if (data['position']['toplay'] === 'B') {
+ ++last_move_num;
+ }
+
+ // Possibly truncate some moves if we don't have enough width.
+ // FIXME: Sometimes width() for #scorecontainer (and by extent,
+ // #scoresparkcontainer) on Chrome for mobile seems to start off
+ // at something very small, and then suddenly snap back into place.
+ // Figure out why.
+ var max_moves = Math.floor($("#scoresparkcontainer").width() / 5) - 5;
+ if (last_move_num - first_move_num > max_moves) {
+ first_move_num = last_move_num - max_moves;
+ }
+
+ var min_score = -100;
+ var max_score = 100;
+ var last_score = null;
+ var scores = [];
+ for (var halfmove_num = first_move_num; halfmove_num <= last_move_num; ++halfmove_num) {
+ if (data['score_history'][halfmove_num]) {
+ var score = compute_plot_score(data['score_history'][halfmove_num]);
+ last_score = score;
+ if (score < min_score) min_score = score;
+ if (score > max_score) max_score = score;
+ }
+ scores.push(last_score);
+ }
+ if (data['score']) {
+ scores.push(compute_plot_score(data['score']));
+ }
+ // FIXME: at some widths, calling sparkline() seems to push
+ // #scorecontainer under the board.
+ $("#scorespark").sparkline(scores, {
+ type: 'bar',
+ zeroColor: 'gray',
+ chartRangeMin: min_score,
+ chartRangeMax: max_score,
+ tooltipFormatter: function(sparkline, options, fields) {
+ return format_tooltip(data, fields[0].offset + first_move_num);
+ }
+ });
+ } else {
+ $("#scorespark").text("");
+ }
+ } else {
+ $("#scorespark").text("");
+ }
+}
+
+/**
+ * @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");
+ }
+}
+
+var update_clock = function() {
+ clearTimeout(clock_timer);
+
+ var data = displayed_analysis_data || current_analysis_data;
+ if (!data) return;
+
+ if (data['position']) {
+ var result = data['position']['result'];
+ if (result === '1-0') {
+ $("#whiteclock").text("1");
+ $("#blackclock").text("0");
+ $("#whiteclock").removeClass("running-clock");
+ $("#blackclock").removeClass("running-clock");
+ return;
+ }
+ if (result === '1/2-1/2') {
+ $("#whiteclock").text("1/2");
+ $("#blackclock").text("1/2");
+ $("#whiteclock").removeClass("running-clock");
+ $("#blackclock").removeClass("running-clock");
+ return;
+ }
+ if (result === '0-1') {
+ $("#whiteclock").text("0");
+ $("#blackclock").text("1");
+ $("#whiteclock").removeClass("running-clock");
+ $("#blackclock").removeClass("running-clock");
+ return;
+ }
+ }
+
+ var white_clock_ms = null;
+ var black_clock_ms = null;
+ var show_seconds = false;
+
+ // Static clocks.
+ if (data['position'] &&
+ data['position']['white_clock'] &&
+ data['position']['black_clock']) {
+ white_clock_ms = data['position']['white_clock'] * 1000;
+ black_clock_ms = data['position']['black_clock'] * 1000;
+ }
+
+ // Dynamic clock (only one, obviously).
+ var color;
+ if (data['position']['white_clock_target']) {
+ color = "white";
+ $("#whiteclock").addClass("running-clock");
+ $("#blackclock").removeClass("running-clock");
+ } else if (data['position']['black_clock_target']) {
+ color = "black";
+ $("#whiteclock").removeClass("running-clock");
+ $("#blackclock").addClass("running-clock");
+ } else {
+ $("#whiteclock").removeClass("running-clock");
+ $("#blackclock").removeClass("running-clock");
+ }
+ var remaining_ms;
+ if (color) {
+ var now = new Date().getTime() + client_clock_offset_ms;
+ remaining_ms = data['position'][color + '_clock_target'] * 1000 - now;
+ if (color === "white") {
+ white_clock_ms = remaining_ms;
+ } else {
+ black_clock_ms = remaining_ms;
+ }
+ }
+
+ if (white_clock_ms === null || black_clock_ms === null) {
+ $("#whiteclock").empty();
+ $("#blackclock").empty();
+ return;
+ }
+
+ // If either player has ten minutes or less left, add the second counters.
+ var show_seconds = (white_clock_ms < 60 * 10 * 1000 || black_clock_ms < 60 * 10 * 1000);
+
+ if (color) {
+ // See when the clock will change next, and update right after that.
+ var next_update_ms;
+ if (show_seconds) {
+ next_update_ms = remaining_ms % 1000 + 100;
+ } else {
+ next_update_ms = remaining_ms % 60000 + 100;
+ }
+ clock_timer = setTimeout(update_clock, next_update_ms);
+ }
+
+ $("#whiteclock").text(format_clock(white_clock_ms, show_seconds));
+ $("#blackclock").text(format_clock(black_clock_ms, show_seconds));
+}
+
+/**
+ * @param {Number} remaining_ms
+ * @param {boolean} show_seconds
+ */
+var format_clock = function(remaining_ms, show_seconds) {
+ if (remaining_ms <= 0) {
+ if (show_seconds) {
+ return "00:00:00";
+ } else {
+ return "00:00";
+ }
+ }
+
+ 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;
+ if (show_seconds) {
+ return format_2d(hours) + ":" + format_2d(minutes) + ":" + format_2d(seconds);
+ } else {
+ return format_2d(hours) + ":" + format_2d(minutes);
+ }
+}
+
+/**
+ * @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 {string} move
+ * @param {Number} halfmove_num
+ */
+var format_halfmove_with_number = function(move, halfmove_num) {
+ return format_move_with_number(
+ move,
+ Math.floor(halfmove_num / 2) + 1,
+ halfmove_num % 2 == 0);
+}
+
+/**
+ * @param {Object} data
+ * @param {Number} halfmove_num
+ */
+var format_tooltip = function(data, halfmove_num) {
+ if (data['score_history'][halfmove_num] ||
+ halfmove_num === data['position']['pretty_history'].length) {
+ var move;
+ var short_score;
+ if (halfmove_num === data['position']['pretty_history'].length) {
+ move = data['position']['last_move'];
+ short_score = format_short_score(data['score']);
+ } else {
+ move = data['position']['pretty_history'][halfmove_num];
+ short_score = format_short_score(data['score_history'][halfmove_num]);
+ }
+ var move_with_number = format_halfmove_with_number(move, halfmove_num);
+
+ return "After " + move_with_number + ": " + short_score;
+ } else {
+ for (var i = halfmove_num; i --> 0; ) {
+ if (data['score_history'][i]) {
+ var move = data['position']['pretty_history'][i];
+ return "[Analysis kept from " + format_halfmove_with_number(move, i) + "]";
+ }
+ }
+ }