/* ------------------------------------------------------------------------- *
 * $Id$
 *
 * Copyright (c) 2007, Steven Bakker; all rights reserved.
 *
 * This code is released under the GNU Public License.
 *
 * ------------------------------------------------------------------------- */

/* ------------------------------------------------------------------------- *
 * xmHttp = GetXmlHttpObject();
 * ------------------------------------------------------------------------- */
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;
}

/* ------------------------------------------------------------------------- *
 * showPlaying();
 *    Callback for xmlHttp GET. Parses the resulting text and updates the
 *    "eltId" document elements to show the currently playing track.
 * ------------------------------------------------------------------------- */
function showPlaying(url, eltId)
{
	if (xmlHttp.readyState==4) { 
		str = xmlHttp.responseText;
		var info = ({
				"title"    : "-",
				"artist"   : "-",
				"album"    : "-",
				"duration" : 0, /* seconds */
				"started"  : 0  /* seconds since epoch */
			});

		if (str.length > 0) {
			info = eval('(' + str + ')');
		}

		/*
		document.getElementById(eltId).innerHTML =
			"<table>\n"
			+ '<tr><th>Title:</th><td>'+info.title+"</td>\n"
			+ '<td rowspan="3">'
			+ '<img src="http://blog.monkey-mind.net/playlist/cover.png"/>'
			+ "</td></tr>\n"
			+ '<tr><th>Artist:</th><td>'+info.artist+"</td></tr>\n"
			+ '<tr><th>Album:</th><td>'+info.album+"</td></tr>\n"
			+ "</table>\n";
		*/
		var date_now = new Date();
		var t_now = date_now.getTime(); /* milliseconds since epoch */
		var t_end = ( info.duration + info.started ) * 1000;
		var t_wait = t_end - t_now;
		var is_finished = t_now > t_end;

		var t_string = date_now.getDate()
					+ '-' + date_now.getMonth()
					+ '-' + (date_now.getYear() + 1900)
					+ ' ' + date_now.getHours()
					+ ':' + date_now.getMinutes()
					+ ':' + date_now.getSeconds();

		if (is_finished) {
			if (t_now - t_end > 10000) {
				document.getElementById(eltId+'.title').innerHTML  = '';
				document.getElementById(eltId+'.artist').innerHTML = '';
				document.getElementById(eltId+'.album').innerHTML  = '';
				var img = document.getElementById(eltId+'.cover').src;
				img = img.replace(/-null\./, '.');
				img = img.replace(/(\.[^\.]*)$/, '-null$1');
				img = img.replace(/\?.*$/, '');
				document.getElementById(eltId+'.cover').src =  img;
			}

			t_wait = t_now - t_end;
			if (t_wait > 180000) t_wait = 180000;
		}
		else {
			document.getElementById(eltId+'.title').innerHTML  = info.title;
			document.getElementById(eltId+'.artist').innerHTML = info.artist;
			document.getElementById(eltId+'.album').innerHTML  = info.album;
			var img = document.getElementById(eltId+'.cover').src;
			img = img.replace(/-null\./, '.');
			img = img.replace(/\?.*$/, '');
			img += '?'+date_now.getTime();
			document.getElementById(eltId+'.cover').src =  img;
			t_wait += 2000;
			if (t_wait > 60000) t_wait = 60000;
		}
		if (document.getElementById(eltId+'.updated')) {
			document.getElementById(eltId+'.updated').innerHTML = t_string;
		}

		/*
		alert("t_wait:"+t_wait+"\n"
			+ "t_now:"+t_now+"\n"
			+ "t_end:"+t_end+"\n"
			+ "duration:"+info.duration+"\n"
			+ "started:"+info.started+"\n"
		);
		/**/
		setTimeout("getPlaying('"+url+"', '"+eltId+"')", t_wait);
	}
}

/* ------------------------------------------------------------------------- *
 * getPlaying(url);
 *      Perform the asynchronous request to get the currently playing track.
 * ------------------------------------------------------------------------- */
function getPlaying(url, eltId)
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp == null) {
		/* alert ("Your browser does not support AJAX!"); */
		return;
	} 
	xmlHttp.onreadystatechange = function () { showPlaying(url, eltId) };
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}
