
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, fulltitle)
{
  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" width="10">' + title + '</td>\n' +
              '<td bgcolor="#' + color + '" valign="top" align="right">' + corner2 + '</td>\n';
  if (! fulltitle) {
    tableStr1 += '<td width="80%">&nbsp;</td>\n';
  }
  tableStr1 += '</tr>\n' + 
	       '<tr>\n' +
               '<td class="fbody" align="left" style="border-color: #' + color + '" colspan="' + ((fulltitle) ? '3' : '4') + 
               '" bgcolor="#' + bgcolor + '">\n';
  
  tableStr3 = '</td>\n</tr>\n</table>\n<br>\n\n';
  return tableStr1 + content + tableStr3;
}


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

function wmbrXmlInfo2Html(xmlDoc)
{
  if (null == xmlDoc) {
    // alert("null result from responseXML");
    return '';
  }

  var infoStr = getElementText(xmlDoc.getElementsByTagName("time"));
  if (infoStr.length == 0) {
    return '';
  }
  //  var now = new Date();
  //  infoStr += ' ' + now.getSeconds();
    
  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;
    }
  }

  return infoStr;    
}

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

function getInfoBoxes()
{
  // first get the most recent playlists from track-blaster

  xmlHttp.open("GET", "/recent_plays.php", false);
  xmlHttp.send(null);

  if (xmlHttp.status != 200) {
    contentStr = 'status = ' + xmlHttp.status;
  } else if (null == (contentStr = xmlHttp.responseText)) {
    contentStr = "empty";
  }

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

  // second, get the most recent tweets from the twitter search api

  xmlHttp.open("GET", "/recent_tweets.php", false);
  xmlHttp.send(null);

  if (xmlHttp.status != 200) {
    contentStr = 'status = ' + xmlHttp.status;
  } else if (null == (contentStr = xmlHttp.responseText)) {
    contentStr = "empty";
  }

  twitblock = document.getElementById("recent_tweets");
  if (null != twitblock) {
    if (contentStr.length > 0) {
      twitblock.innerHTML = getBox('Recent WMBR Tweets', '33AACC', 'F4F4F4', 12, 9, contentStr, 0);
    } else {
      twitblock.innerHTML = '';
    }
  }

  // finally, get the current show, time, and weather

  xmlHttp.open("GET", "/cgi-bin/xmlinfo", false);
  xmlHttp.send(null);

  contentStr = wmbrXmlInfo2Html(xmlHttp.responseXML);

  infoblock = document.getElementById("wmbr_info_block");
  if (null != infoblock) {
    infoblock.innerHTML = contentStr;
  }

  // repeat every 20 seconds
  setTimeout(getInfoBoxes, 20000);
}

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

function startAJAX()
{
  xmlHttp = getXmlHttpObject();
  if (null == xmlHttp) {
    //  alert("Browser doesn't support ajax");
    return;
  }

  getInfoBoxes();
}

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


