+var update_clock = function() {
+ clearTimeout(clock_timer);
+
+ 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");
+ $("#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 = "";
+ var black_clock = "";
+
+ // Static clocks.
+ if (data['position'] &&
+ data['position']['white_clock'] &&
+ data['position']['black_clock']) {
+ white_clock = data['position']['white_clock'].replace(/:[0-5][0-9]$/, "");
+ black_clock = data['position']['black_clock'].replace(/:[0-5][0-9]$/, "");
+ }
+
+ // 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");
+ }
+ 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);
+ }
+
+ // See when the clock will change next, and update right after that.
+ var next_update_ms = remaining_ms % 60000 + 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";
+ }
+
+ 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);
+}
+
+/**
+ * @param {Number} x
+ */
+var format_2d = function(x) {
+ if (x >= 10) {
+ return x;
+ } else {
+ return "0" + x;
+ }
+}
+