
/***********************************************************/
/* WMBR AJAX routines.  This code gets information about
 * the current show and current weather from the XML CGI
 * http://wmbr.org/cgi-bin/xmlinfo
 * It puts it in a document element with id = "wmbr_info_block".
 * Call getWmbrInfo() at load time to start a loop that
 * refreshes the info every 30 seconds.
 **/
/***********************************************************/

var xmlHttp;

function getXmlHttpObject()
{
  var xmlHttp = null;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  } catch (e) {
    // Internet Explorer
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
}

/***********************************************************/

function getElementText(nodeList)
{
  if ((null != nodeList) && (nodeList.length > 0) && (null != nodeList[0].firstChild)) {
    return nodeList[0].firstChild.nodeValue;
  }
  return "";
}

/***********************************************************/

function getBox(title, color, bgcolor, corner_w, corner_h, content)
{
  corner1 = '<img src="/arc/w' + corner_w + 'h' + corner_h + 'fg' + color + 'q1" ' +
            'width="' + corner_w + '" height="' + corner_h + '" alt="">';
  corner2 = '<img src="/arc/w' + corner_w + 'h' + corner_h + 'fg' + color + 'q2" ' +
            'width="' + corner_w + '" height="' + corner_h + '" alt="">';

  tableStr1 = '<table width="100%" border="0" cellpadding="0" cellspacing="0">\n' +
	      '<tr>\n' +
	      '<td bgcolor="#' + color + '" valign="top" align="left">' + corner1 + '</td>\n' +
              '<td bgcolor="#' + color + '" align="center" class="ftab" style="white-space: nowrap">' + title + '</td>\n' +
              '<td bgcolor="#' + color + '" valign="top" align="right">' + corner2 + '</td>\n' +
              '</tr>\n' + 
	      '<tr>\n' +
              '<td class="fbody" align="left" style="border-color: #' + color + '" colspan="4" bgcolor="#' + bgcolor + '">\n';
  
  tableStr3 = '</td>\n</tr>\n</table>\n<br>\n\n';
  return tableStr1 + content + tableStr3;
}


/***********************************************************/

function showWmbrInfo()
{
  if ((xmlHttp.readyState != 4) || (xmlHttp.status != 200)) {
    return;
  }
  var xmlDoc = xmlHttp.responseXML;
  if (null == xmlDoc) {
    // alert("null result from responseXML");
    return;
  }

  var infoStr = getElementText(xmlDoc.getElementsByTagName("time"));
  if (infoStr.length == 0) {
    return;
  }
    
  var showStr = getElementText(xmlDoc.getElementsByTagName("showname"));
  if (showStr.length > 0) {
    infoStr += " : now playing: &nbsp;<b>";
    var urlStr = getElementText(xmlDoc.getElementsByTagName("showurl"));
    if (urlStr.length > 0) {
      infoStr += "<a href=\"" + urlStr + "\" target=\"_blank\">" + showStr + "</a></b>";
    } else {
      infoStr += showStr + "</b>";
    }
  }
  var wxStr = getElementText(xmlDoc.getElementsByTagName("wx")); 
  var tempStr = getElementText(xmlDoc.getElementsByTagName("temp"));

  var wtStr = "";
  if (wxStr.length > 0) {
    wtStr = wxStr;
    if (tempStr.length > 0) {
      wtStr += "; " + tempStr;
    }
  } else {
    wtStr = tempStr;
  }
  if (wtStr.length > 0) {
    if (showStr.length > 0) {
      infoStr += "<br>" + wtStr;
    } else {
      infoStr += " : " + wtStr;
    }
  }
    
  infoblock = document.getElementById("wmbr_info_block");
  if (null != infoblock) {
    infoblock.innerHTML = infoStr;
  }
}

/***********************************************************/

function fixTime(s)
{
  if (0 <= (colon = s.indexOf(':'))) {
    var hourStr = s.substr(0, colon);
    var hour = parseInt(hourStr,10);
    var ampm = (hour < 12) ? 'a' : 'p';
    if (hour > 12) {
      hour -= 12;
    } else if (hour < 1) {
      hour = 12;
    }
      
    minStr = s.substr(colon + 1, 2);
    return hour + ':' + minStr;
  }
  return '';    
}

/***********************************************************/

function showRecentPlays()
{
  if ((xmlHttp.readyState != 4) || (xmlHttp.status != 200)) {
    return;
  }
  if (null == (xmlDoc = xmlHttp.responseXML)) {
    return;
  }
  if (null == (itemList = xmlDoc.getElementsByTagName("item"))) {
    return;
  }
  if (0 == itemList.length) {
    return;
  }

  var contentStr = '';
  for (i = 0; (i < itemList.length) && (i < 8); i++) {
    timeStr = fixTime(getElementText(itemList[i].getElementsByTagName("time")));
    artistStr = getElementText(itemList[i].getElementsByTagName("artist"));
    songStr = getElementText(itemList[i].getElementsByTagName("song"));
    contentStr += '<p class="recent">' + timeStr + '&nbsp;<b>' + artistStr + 
                  '</b>: ' + songStr + '</p>\n';
  }  
  contentStr += "</ul>\n"
  contentStr += '<a href="http://www.track-blaster.com/wmbr/playlist.php?date=latest" target="_blank">full playlist</a>\n';

  playblock = document.getElementById("recent_plays");
  if (null != playblock) {
    playblock.innerHTML = getBox('Recently Played', 'CC4444', 'F4F4F4', 12, 9, contentStr);
  }
}

/***********************************************************/

function getWmbrInfo()
{
  xmlHttp = getXmlHttpObject();
  if (null == xmlHttp) {
    //  alert("Browser doesn't support ajax");
    return;
  }
  xmlHttp.onreadystatechange = showWmbrInfo;
  xmlHttp.open("GET", "/cgi-bin/xmlinfo", true);
  xmlHttp.send(null);
  setTimeout(getRecentPlays, 10000);
}

/***********************************************************/

function getRecentPlays()
{
  xmlHttp = getXmlHttpObject();
  if (null == xmlHttp) {
    //  alert("Browser doesn't support ajax");
    return;
  }
  xmlHttp.onreadystatechange = showRecentPlays;
  xmlHttp.open("GET", "/recent_plays.php", true);
  xmlHttp.send(null);
  setTimeout(getWmbrInfo, 10000);
}

/***********************************************************/

