//The Playlist Results
//2007-11-12, Luke McKenzie theclockspot.com

//This .js generates playlist results
//controls dom @ <div id='playlistresultsdiv'>
	//suffices for swirly as well

//requires ajax.js

//request object (requires ajax.js)
var requestp = getAjaxRequestObj();

function lookupPlaylist(startdate,enddate,showid,order) {

	//run to get playlist stuff
	//requires pathtohome!
	var url = pathtohome+'data/lookupPlaylist.php?dummy=' + new Date().getTime(); //to prevent browser caching
	if(!startdate && enddate) { startdate=enddate; enddate=''; } //fliparoony - don't see how this could even happen but whatevs
	if(!startdate) alert('No date(s) specified!'); else {
		url+='&startdate='+startdate;
		if(enddate) url+='&enddate='+enddate;
		if(showid) url+='&showid='+showid;
		url+='&order='+order;
		//in lieu of spinner
		//document.getElementById("playlistresultsdiv").innerHTML='<p>Loooooading...</p>';
		requestp.open("GET", url, true);
		requestp.onreadystatechange = lookupPlaylistUpdate;
		requestp.send(null); //there it gooooes
	}
}

function lookupPlaylistUpdate() {
	
	//THIS TOTALLY has to be updated to indicate dates and shows
	if(requestp.readyState == 4) { //when it's done
		if(requestp.status == 200) {
			//var xmadoc = requestp.responseText; //Don't delete these two lines!
			//alert('hello: '+xmadoc); //They are good for debugging PHP errors in lookupSuggestions.php
			//document.getElementById("playlistresultsdiv").innerHTML = xmadoc;
			var xmlDoc = requestp.responseXML; //as opposed to responseText, this is XML, which turns into a DOM tree
			//expecting: multishows, new, time, date, show, showname, artist, album, song
			//dates displayed all the time
			var xmlMultishows = xmlDoc.getElementsByTagName('multishows');
			var multishows=getNodeValue(xmlMultishows,0);
			var lastshow=''; //to keep track of show changes
			var lastdate=''; //to keep track of date changes
			var nextshowname=0; //to keep track of our place in the showname array, which is the only one of smaller size (can't use i)
			var xmlNew = xmlDoc.getElementsByTagName('new');
			var xmlTime = xmlDoc.getElementsByTagName('time');
			var xmlDate = xmlDoc.getElementsByTagName('date');
			var xmlShow = xmlDoc.getElementsByTagName('show');
			var xmlShowName = xmlDoc.getElementsByTagName('showname');
			var xmlArtist = xmlDoc.getElementsByTagName('artist');
			var xmlAlbum = xmlDoc.getElementsByTagName('album');
			var xmlSong = xmlDoc.getElementsByTagName('song');
			var threet = 0;
			if(xmlNew.length==0) {
				newTable ='<p class=\'redtext\'>Sorry, I got nothing.</p>';
			} else {
				newTable = '<table class=\'last5songs\'; border=0 cellpadding=0 cellspacing=4>\n';
				newTable += '<tr><td width=15%>Time</td><td width=25%>Album</td><td width=25%>Artist</td><td width=35%>Song</td></tr>\n';
				for(i=0; i<xmlNew.length; i++) {
					dat=getNodeValue(xmlDate,i);
					if(dat!=lastdate) { //when the date changes, put it in a banner
						lastdate=dat;
						//lastshow=''; //CAN'T reset this for each day, as xmlShowName doesn't have extra entries to let you do this
						newTable += '<tr><td colspan=4><h3 class=\'underscore\' style="margin: 11px auto 0; padding: 0px;">'+niceDate(dat)+'</h3></td></tr>\n';
					}
					if(multishows==1) { //==1 : famous last characters
						//when the show changes, put it in a banner - if indeed we're keeping track of that (multishows)
						shoo=getNodeValue(xmlShow,i);
						if(lastshow!=shoo) {
							lastshow=shoo;
							sn=getNodeValue(xmlShowName,nextshowname);
							if(!sn) {
								if (shoo<0) {
									sn='Unspecified Show';
								} else {
									sn='Show Code '+shoo;
								}
							}
							nextshowname++;
							newTable += '<tr><td colspan=4><h4 class=\'underscore\' style="margin: 7px auto 0;">'+sn+'</h4></td></tr>\n';
						}
					}
					a=getNodeValue(xmlNew,i);
					b=getNodeValue(xmlTime,i);
					c=getNodeValue(xmlArtist,i);
					d=getNodeValue(xmlAlbum,i);
					e=getNodeValue(xmlSong,i);
					newTable += '<tr><td'+(a==1 ? " class='redtext'>" : ">");
					newTable += '&nbsp;&nbsp;&nbsp;'+b;
					newTable += '</td><td'+(a==1 ? " class='redtext'>" : ">");
					newTable += unescape(d);
					newTable += '</td><td'+(a==1 ? " class='redtext'>" : ">");
					newTable += unescape(c);
					newTable += '</td><td'+(a==1 ? " class='redtext'>" : ">");
					newTable += unescape(e);
					newTable += '</td></tr>\n';
				}
			}
			newTable += '</table>';

			//stopSwirly('played');
			document.getElementById("playlistresultsdiv").innerHTML = newTable;

		} else { //if the status isn't 200, this will alert us to problems //FIX EVERYWHERE
			var msgs = 'Error! requestp came back with status '+requestp.status+'.';
			var bitsy = requestp.getResponseHeader("Status");
			if(bitsy) msgs += '\n\n'+bitsy;
			alert(msgs);
		} //close if requestp.status = 200
	}
}

function getNodeValue(x,i) {
	//x=object HTMLCollection (basically an array)
	//i=integer, the index to return
	//Separated out from the mains for simplicity,
	//and because if(!multishows), the checks inside (on nonexistent show thingies) still killed the process.
	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;
}

var niceMonth = new Array('','January','February','March','April','May','June','July','August','September','October','November','December');
var niceDay = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'); //extra sunday so 0 or 7
function niceDate(dat) {
	da=new Date(dat.substring(0,4),dat.substring(5,7)-1,dat.substring(8,10)); //dat is yyyy-mm-dd
	return niceDay[da.getDay()]+' '+da.getDate()+' '+niceMonth[da.getMonth()+1]+' '+da.getFullYear();
}
