var xmlHttp;
var lastDay;
var lastHour;

/* 
  Given integers representing the current day of the week
  and the hour of the day (0-indexed), highlight the
  corresponding table cell and reset the color of the
  currently highlighted cell (if any).
*/
function highlightCurrentlyPlaying(day, hour) {
    var schedule = document.getElementById('schedule');
    if(lastHour) {
	schedule.rows[lastHour].cells[lastDay].style.border = '';//oldColor;
    }

    var current = schedule.rows[hour].cells[day].style.border = 'solid #c41230 6px';//red from wzen.org logo
    lastDay = day;
    lastHour = hour;
}


/* 
  ========================================================
  Associated simple ajax-y request and handler to grab the
  server-side day of the week and time of day every sixty 
  seconds, calling highlightCurrentlyPlaying() on success.
  ========================================================
*/

function getCurrentTime() {
    if(window.ActiveXObject) {
	xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else if (window.XMLHttpRequest) {
	xmlHttp = new XMLHttpRequest();
    }

    xmlHttp.onreadystatechange = timeReceiptHandler;
    xmlHttp.open("GET","current_time.php");
    xmlHttp.send(null);
    setTimeout('getCurrentTime();',60000);
}

function timeReceiptHandler() {
    if(xmlHttp.readyState == 4) {
	if(xmlHttp.status == 200) {
	    var results = xmlHttp.responseText.split(',');
	    var day_of_week = parseInt(results[0]);
	    var hour = parseInt(results[1])%12; /*added "%12" for a 12-hour grid*/
	    highlightCurrentlyPlaying(day_of_week,hour);
	}
    }
}