var uid, noah, running, timer_start, timer_finish, timer_left,timer_now, dh,dm,ds, h,m,s;
function timer(x,startup) {
if (startup) running = startup;
switch (x) {
case 0: // start
if (running == 1) {
if (startup) {
if(noah) document.getElementById('stopstart').innerHTML = '';
dh = parseInt(document.getElementById('dh').value);
dm = parseInt(document.getElementById('dm').value);
ds = parseInt(document.getElementById('ds').value);
timer_start = new Date();
timer_finish = new Date();
timer_finish.setHours(timer_finish.getHours()+dh);
timer_finish.setMinutes(timer_finish.getMinutes()+dm);
timer_finish.setSeconds(timer_finish.getSeconds()+ds);
}
timer_now = new Date;
timer_left = timer_finish-timer_now;
var h = Math.floor(timer_left/3600000);
var m = Math.floor((timer_left-(h*3600000))/60000);
var s = Math.floor((timer_left-(h*3600000)-(m*60000))/1000);
document.getElementById('dh').value = h;
document.getElementById('dm').value = m;
document.getElementById('ds').value = s;
if (h < 0 || m < 0 || s < 0 ) {
document.getElementById('timer_play').play();
timer(2);
}
else window.setTimeout(function(){timecheck()}, 100);
}
break;
case 1: // stop
running = 0;
if(noah) document.getElementById('stopstart').innerHTML = '';
break;
case 2: // clear
if (running == 0) {
document.getElementById('dh').value = 0;
document.getElementById('dm').value = 0;
document.getElementById('ds').value = 0;
}
else {
timer(1);
document.getElementById('dh').value = dh;
document.getElementById('dm').value = dm;
document.getElementById('ds').value = ds;
}
break;
}
}
function timecheck() {
if (running == 1) {
timer(0);
}
}
function update_timer() {
var state = gapi.hangout.data.getState();
dh = parseInt(state.dh);
dm = parseInt(state.dm);
ds = parseInt(state.ds);
if(parseInt(state.state) == 0) timer(0,1);
if(parseInt(state.state) == 1) timer(1);
if(parseInt(state.state) == 2) timer(2);
}
function changeTimerState(state) {
if(state == 0) {
dh = document.getElementById('dh').value;
dm = document.getElementById('dm').value;
ds = document.getElementById('ds').value;
}
gapi.hangout.data.submitDelta(
{"state":state.toString(),"dh":dh,"dm":dm,"ds":ds}
);
update_timer();
}
if (gapi && gapi.hangout) {
var initHangout = function() {
var hid = gapi.hangout.getParticipantId();
var part = gapi.hangout.getParticipantById(hid);
var uid = part.id;
noah = (uid == 118014954414967440482);
gapi.hangout.data.addStateChangeListener(update_timer);
}
gapi.hangout.addApiReadyListener(initHangout);
}