var performance_on = 0;
var looping = 1;

function noLoops() {  // call to avoid flashing on FF2 on Mac
	looping = 0;
}

function setPerformanceOn(value) {
    performance_on = value;
}

function perfOnCallback() { 
    if(http.readyState == 4){
        var response = http.responseText
        performanceOn = parseInt(response) 
    } 
    // do it again in a little while
    setTimeout('checkIfPerformanceOn();', 60*10000);
}

function checkIfPerformanceOn() { 
    sndReq("perf_on.php", perfOnCallback)
}

//------------------------------------------------------------------------------
// Countdown to start time
function setCountdown(seconds_left) {
  // set up
  clientTargetTime = new Date().getTime()/1000 + seconds_left;
  countdown(looping); // countdown to start time
}
//------------------------------------------------------------------------------
function tomorrowCallback() {
    if(http.readyState == 4){
        var response = http.responseText;
        seconds_left=parseInt(response) 
        clientTargetTime = new Date().getTime()/1000 + seconds_left;          
        setPerformanceOn(1);
        clock(0);
        flipForecast();
    } 
}
//------------------------------------------------------------------------------
function countdown(loop) {
  secondsLeft = Math.round(clientTargetTime - (new Date().getTime() / 1000));
  if(secondsLeft > 0)
  {
    hoursLeft = Math.floor(secondsLeft / (60 * 60));
    secondsLeft %= (60*60);
    minutesLeft = Math.floor(secondsLeft / 60);
    secondsLeft %= 60;

    hps = 's'; mps = 's'; sps = 's';
    //ps is short for plural suffix.
    if(hoursLeft == 1) hps ='';
    if(minutesLeft == 1) mps ='';
    if(secondsLeft == 1) sps ='';

    if (performance_on) {
        document.getElementById("timeuntil").innerHTML = 'Tomorrow\'s performance starts in';
    }
    else {
        document.getElementById("timeuntil").innerHTML = 'Next performance starts in';
    }
    
    if (hoursLeft < 1) {	
        document.getElementById("timebox").innerHTML = '';
        if (minutesLeft > 0) document.getElementById("timebox").innerHTML += minutesLeft + ' minute' + mps + ' ';
        document.getElementById("timebox").innerHTML += secondsLeft + ' second' + sps;
    }
    else {
        document.getElementById("timebox").innerHTML = hoursLeft + ' hour' + hps + ' ';
        if (minutesLeft > 0) document.getElementById("timebox").innerHTML += minutesLeft + ' minute' + mps + ' ';
    }
 }
  else {
    // we flipped over.. we need to get a new seconds left value
    sndReq("tomorrow.php", tomorrowCallback)
  }
  //Recursive call, keeps the clock ticking.
  if(loop==1) setTimeout('countdown(1);', 1000);
}

//------------------------------------------------------------------------------
// Cambridge local time
function setClock(h,m,am) {
  local_h = h;
  local_m = parseInt(m);
  local_am = am;
  // skip the first update other we'll be off by 1 min
  local_skip = 1;
  clock(looping);
}

//------------------------------------------------------------------------------
function clock(loop) { 
	if (local_skip == 1) { local_skip = 0;}
	else {
        local_m = parseInt(local_m);
		local_m += 1;
		if (local_m == 60) {
			local_m = 0;
			local_h += 1;
			if (local_h == 13) {
				local_h = 0;
				if (local_am == 'am') local_am = 'pm';
				else local_am = 'am';
			}
		}
	}
    // add a zero in front of minutes if only 1 digit
    local_m = (local_m < 10 ? "0" : "" ) + local_m; 
    if (performance_on) {
        document.getElementById("localtime").innerHTML =  local_h + ':' + local_m + local_am + " - Performance is on!";
    }
    else {
        document.getElementById("localtime").innerHTML =  local_h + ':' + local_m + local_am + " - Current time in Cambridge, Ontario";
    }

	if (loop==1) setTimeout('clock(1);', 60*1000);
}

function flipForecast() {
    document.getElementById("timenext").innerHTML = tomorrow_perf + " - Start of tomorrow's performance.";
}

