//onair.js
//On-air (currently playing) information, in this case for 
//2007-11-14, Luke McKenzie theclockspot.com

//vars and arrays
var status = '';
var show = new Array();
var nextshow = new Array();
var songs = new Array();
var requestOnAir = getAjaxRequestObj(); //request object (requires ajax.js)

//startup function (to be launched on parent doc's onLoad)
function initOnAir() {
	doOnAir();
}

//requires pathtohome variable to be set in main doc
function doOnAir() {
	var url=pathtohome+'data/lookupOnAir.php?dummy=' + new Date().getTime(); //to prevent browser caching
	requestOnAir.open("GET", url, true);
	requestOnAir.onreadystatechange = doOnAirUpdate;
	requestOnAir.send(null);
}

function doOnAirUpdate() {
	if(requestOnAir.readyState == 4) { //when the data comes back
		if(requestOnAir.status == 200) { //if successful
			//var xmadoc = requestOnAir.responseText; alert(xmadoc); //for debugging
			var xmlDoc = requestOnAir.responseXML;

			/*expecting:
			<onair servertime status(live, auto, off)>
				<show type(regular, special) name dj text />
				<nextshow type name dj text showdate showtime timetext />
				<song title artist album playdate playtime />
			</onair>
			*/
			
			//get data from xml and set it in javascript objects
			//status
			status = xmlDoc.getElementsByTagName("onair").item(0).getAttribute("status");
			//show
			xmlShow = xmlDoc.getElementsByTagName("show");
			show['name'] = unescape(xmlShow.item(0).getAttribute("name"));
			show['dj'] = unescape(xmlShow.item(0).getAttribute("dj"));
			show['url'] = unescape(xmlShow.item(0).getAttribute("url"));
			show['text'] = unescape(xmlShow.item(0).getAttribute("text"));
			//nextshow
			xmlNextShow = xmlDoc.getElementsByTagName("nextshow");
			nextshow['name'] = unescape(xmlNextShow.item(0).getAttribute("name"));
			nextshow['dj'] = unescape(xmlNextShow.item(0).getAttribute("dj"));
			nextshow['url'] = unescape(xmlNextShow.item(0).getAttribute("url"));
			nextshow['showdate'] = xmlNextShow.item(0).getAttribute("showdate");
			nextshow['showtime'] = xmlNextShow.item(0).getAttribute("showtime");
			nextshow['text'] = unescape(xmlNextShow.item(0).getAttribute("text"));
			//song(s)
			xmlSongs = xmlDoc.getElementsByTagName("song");
			for(i=0;i<xmlSongs.length;i++) {
				songs[i] = new Array();
				songs[i]['title'] = unescape(xmlSongs.item(i).getAttribute("title"));
				songs[i]['artist'] = unescape(xmlSongs.item(i).getAttribute("artist"));
				songs[i]['album'] = unescape(xmlSongs.item(i).getAttribute("album"));
				songs[i]['playdate'] = xmlSongs.item(i).getAttribute("playdate");
				songs[i]['playtime'] = xmlSongs.item(i).getAttribute("playtime");
				songs[i]['url'] = xmlSongs.item(i).getAttribute("url");
			}
			
			//ALEX HAYS removed this in 2011
			//cuzz updated it with a auto-reloating div that shows the current song and show
			//its in tuner/reload_header.php
			
		/*	//update document
			document.getElementById("smalls1").innerHTML = 'ON AIR';
			if(show['url']) x="<a href='"+show['url']+"'>"+show['text']+"</a>"; else x=show['text'];
			document.getElementById("bigs1").innerHTML = x;
			if(status=='auto') {	
				document.getElementById("smalls2").innerHTML = 'NEXT SHOW';
				if(nextshow['url']) x="<a href='"+nextshow['url']+"'>"+nextshow['text']+"</a>"; else x=nextshow['text'];
				document.getElementById("bigs2").innerHTML = x;
			} else {
				document.getElementById("smalls2").innerHTML = 'SONG';
				if(songs[0]['url']) x="<a href='"+songs[0]['url']+"'>"+songs[0]['title']+"</a>"; else x=songs[0]['title'];
				document.getElementById("bigs2").innerHTML = x; //replace with rotating display thing
			}*/
			
			//eventually reload every 20 seconds or something

		} else { //if the status isn't 200, this will alert us to problems with the ajax
	      var message = requestOnAir.getResponseHeader("Status");
	      if ((message.length == null) || (message.length <= 0)) alert("requestOnAir error: " + requestOnAir.status);
	      else alert("Error! "+message);
		  return null;
	    } //close if requestOnAir.status = 200
	}
}

/*function getNodeValue(x,i) {
	//see playlistresults.js
	if(x) { //make sure not null
		if(x[i].firstChild) { //make sure not null
			return x[i].firstChild.nodeValue;
		} else return null;
	} else return null;
}*/

