var redBloodClock = { "clock": 0, "clockSnapshot": 0, "clockLastStopped": 0, "clockExpected": 0, "clockState": { "bumpClockUpButton": null, "bumpClockDownButton": null, }, "isClockFlashing": false, "stopClockFlag": false, "reset": function(){ $("#redBloodStopClockButton").click(); $("#redBloodRewindClockButton").addClass("disabled"); redBloodClock.setClock(scoreClock.config.colorClockLengths.redBloodLength); }, "bumpClockUp": function() { length = redBloodClock.clock + 1; redBloodClock.setClock(length); }, "bumpClockDown": function() { if (redBloodClock.clock == 0) { $("#redBloodBumpClockDownButton").addClass("disabled"); return; } length = redBloodClock.clock - 1; redBloodClock.setClock(length); }, "flashClock": function() { if (redBloodClock.stopClockFlag) { redBloodClock.isClockFlashing = false; $("#redBloodMinsField").css( "background-color", "rgb(255, 255, 255)"); $("#redBloodSecsField").css( "background-color", "rgb(255, 255, 255)"); return; } var backgroundColor = $("#redBloodMinsField").css( "background-color" ); //window.alert(backgroundColor) if (backgroundColor == "rgb(255, 255, 255)") { $("#redBloodMinsField").css( "background-color", "rgb(51, 51, 51)"); $("#redBloodSecsField").css( "background-color", "rgb(51, 51, 51)"); setTimeout(redBloodClock.flashClock, 500); } else if (backgroundColor == "rgb(51, 51, 51)") { $("#redBloodMinsField").css( "background-color", "rgb(255, 255, 255)"); $("#redBloodSecsField").css( "background-color", "rgb(255, 255, 255)"); setTimeout(redBloodClock.flashClock, 500); } //window.alert(backgroundColor); }, "rewindClock": function() { if (redBloodClock.clockSnapshot > 0) { redBloodClock.clockLastStopped = redBloodClock.clockSnapshot; redBloodClock.setClock(redBloodClock.clockSnapshot); } $("#redBloodRewindClockButton").addClass("disabled"); }, "setClock": function (length) { redBloodClock.clock = length; var min = Math.floor(length / 60); var secs = length % 60; if (secs < 10) { secs = "0" + secs; } $("#redBloodMinsField").val(min); $("#redBloodSecsField").val(secs); scoreClock.syncColorClock("redBlood", min, secs); }, "startClock": function() { redBloodClock.clockState.bumpClockUpButton = $("#redBloodBumpClockUpButton").hasClass("disabled") ? "disabled" : "enabled"; redBloodClock.clockState.bumpClockUpButton = $("#redBloodBumpClockUpButton").hasClass("disabled") ? "disabled" : "enabled"; redBloodClock.clockState.bumpClockDownButton = $("#redBloodBumpClockDownButton").hasClass("disabled") ? "disabled" : "enabled"; $("#redBloodBumpClockUpButton").addClass("disabled"); $("#redBloodBumpClockDownButton").addClass("disabled"); $("#redBloodRewindClockButton").addClass("disabled"); redBloodClock.clockSnapshot = redBloodClock.clock; redBloodClock.stopClockFlag = false; redBloodClock.clockExpected = Date.now() + 1000; setTimeout(redBloodClock.stepClock, 1000); }, "stepClock": function() { var dt = Date.now() - redBloodClock.clockExpected; // the drift (positive for overshooting) if (dt > 1000) { // something really bad happened. Maybe the browser (tab) was inactive? // possibly special handling to avoid futile "catch up" run } if (!redBloodClock.stopClockFlag) { var newLength = redBloodClock.clock - 1; if (newLength <= 0 && !redBloodClock.isClockFlashing) { redBloodClock.isClockFlashing = true; redBloodClock.flashClock(); } if (newLength < 0) { return; } redBloodClock.setClock(newLength); redBloodClock.clockExpected += 1000; setTimeout(redBloodClock.stepClock, Math.max(0, 1000 - dt)); // take into account drift } }, "stopClock": function() { if (redBloodClock.clockState.bumpClockUpButton != "disabled") { $("#redBloodBumpClockUpButton").removeClass("disabled"); } if (redBloodClock.clockState.bumpClockUpButton != "disabled") { $("#redBloodBumpClockUpButton").removeClass("disabled"); } if (redBloodClock.clockState.bumpClockDownButton != "disabled") { $("#redBloodBumpClockDownButton").removeClass("disabled"); } if (redBloodClock.clockSnapshot > 0) { $("#redBloodRewindClockButton").removeClass("disabled"); } redBloodClock.clockLastStopped = redBloodClock.clock; redBloodClock.stopClockFlag = true; }, };