function LZ(x) { return (x>=10||x<0?"":"0") + x }

Date.prototype.ISOlocaltimeStr =
  new Function("  /* Date.ISOlocaltimeStr */ with (this)\n    return " +
    "LZ(getHours())+':'+LZ(getMinutes())+':'+LZ(getSeconds())")

Date.prototype.ISOlocalclockStr =
  new Function("  /* Date.ISOlocaltimeStr */ with (this)\n    return " +
    "LZ(getHours())+':'+LZ(getMinutes())")


Date.prototype.ISOlocaldateStr =
  new Function("  /* Date.ISOlocaldateStr */ with (this)\n    return " +
    "getFullYear()+'-'+LZ(getMonth()+1)+'-'+LZ(getDate())")


Date.prototype.ISOlocalDTstr =
  new Function("  /* Date.ISOlocalDTstr */  with (this)\n    return " +
    "ISOlocaldateStr()+' '+ISOlocalclockStr()")


function addEvent(obj, eventType, afunction, isCapture) {
    // W3C DOM
    if (obj.addEventListener) {
       obj.addEventListener(eventType, afunction, isCapture);
       return true;
    }
    // Internet Explorer
    else if (obj.attachEvent) {
       return obj.attachEvent("on"+eventType, afunction);
    }
    else return false;
 }


function createRequestObject() {
	var A;
	try {
		A=new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			A=new ActiveXObject("Microsoft.XMLHTTP");
		} catch (oc) {
			A=null;
		}
	}
	if(!A && typeof XMLHttpRequest != "undefined")
		A = new XMLHttpRequest();
	return A;
}

function PositionScrobblerDiv(divScrobbler) {
	divScrobbler.style.top  = (document.documentElement.clientHeight - divScrobbler.clientHeight - 16) + 'px';
	divScrobbler.style.left = ((document.documentElement.clientWidth - divScrobbler.clientWidth) / 2) + 'px';
}

function InitScrobbler() {
	var div = document.createElement("div");
	div.className = "divScrobbler";
	div.id = "scrobbler";
	document.body.appendChild(div);
	PositionScrobblerDiv(div);

	div = document.createElement("div");
	div.className = "divBusy";
	//div.style.visibility = 'visible';
	div.id = "busy";
	document.body.appendChild(div);
	getRecentTracks();
}

function SetBusyVisibility(visible)
{
  var busyDiv      = document.getElementById('busy');
  var scrobblerDiv = document.getElementById('scrobbler');
  busyDiv.style.left = scrobblerDiv.style.left;
  busyDiv.style.top  = (scrobblerDiv.offsetTop - busyDiv.clientHeight) + 'px';
  busyDiv.style.visibility = visible?'visible':'hidden';
}

function getRecentTracks(){
	SetBusyVisibility(true);
	if (http && http.readyState < 4)
	{
		http.abort();
	}
	http.open('get', 'scrobbler/recent.php', true);
	http.onreadystatechange = handleRecentTracks; 
	http.send(null);
}

function createHRef(href, text)
{
	var ahr = document.createElement("a");
	ahr.setAttribute("href", href);
	ahr.setAttribute("target", "blanc_");
	ahr.innerHTML = text;
	return ahr;
}

function handleRecentTracks(){
	if(http.readyState == 4){ //Finished loading the response
		SetBusyVisibility(false);
		var response = http.responseXML;
		if (!response || !response.documentElement) return;
		var tracks = response.documentElement.getElementsByTagName('track');
		var divScrobbler = document.getElementById('scrobbler');
		var table = document.createElement("table");
		var tbody = document.createElement("tbody");
		table.className = "tblScrobbler";
		if (tracks.length == 0) {
			var tr =  document.createElement("tr");
			tr.className = "trScrobbler0";
			var td =  document.createElement("td");
			td.appendChild(document.createTextNode("No tracks to display..."));
			td.style.textAlign = 'center';
			td.style.padding = '0px 4px 2px 4px';
			tr.appendChild(td);
			tbody.appendChild(tr);

			tr =  document.createElement("tr");
			tr.className = "trScrobbler1";
			var td =  document.createElement("td");
			td.appendChild(document.createTextNode("Who knows..."));
			td.style.textAlign = 'center';
			td.style.padding = '0px 4px 2px 4px';
			tr.appendChild(td);
			tbody.appendChild(tr);

			tr =  document.createElement("tr");
			tr.className = "trScrobbler0";
			var td =  document.createElement("td");
			td.appendChild(document.createTextNode("Maybe I'm really not listening to any music right now :)"));
			td.style.textAlign = 'center';
			td.style.padding = '0px 4px 2px 4px';
			tr.appendChild(td);
			tbody.appendChild(tr);

			tr =  document.createElement("tr");
			tr.className = "trScrobbler1";
			var td =  document.createElement("td");
			td.appendChild(document.createTextNode(" "));
			tr.appendChild(td);
			tbody.appendChild(tr);
		}
		else {
			for(i = 0; i < tracks.length; i++){
				var tr =  document.createElement("tr");
				tr.className = "trScrobbler" + i%2;
				var td =  document.createElement("td");
				td.appendChild(document.createTextNode(tracks[i].getElementsByTagName('artist')[0].firstChild.nodeValue));
				tr.appendChild(td);
				td =  document.createElement("td");
				td.appendChild(document.createTextNode(tracks[i].getElementsByTagName('name')[0].firstChild.nodeValue));
				tr.appendChild(td);
				td = document.createElement("td");
				var time = new Date(tracks[i].getElementsByTagName('date')[0].getAttribute("uts") * 1000);
				td.appendChild(document.createTextNode(time.ISOlocalDTstr()));
				tr.appendChild(td);
				tbody.appendChild(tr);
			}
		}
		var tr2 =  document.createElement("tr");
		tr2.className = "trScrobblerPowered";
		td =  document.createElement("td");
		td.setAttribute("colspan", "2");
		td.className = "tdScrobblerPowered";
		td.appendChild(createHRef("http://www.last.fm/user/chadoe/", "My recent tracks"));
		td.appendChild(document.createTextNode(" is powered by "));
		td.appendChild(createHRef("http://www.last.fm/", "Last.fm"));
		tr2.appendChild(td);
		td =  document.createElement("td");
		td.className = "tdScrobblerPowered right";
		td.appendChild(createHRef("mailto:chadoe%40xbmc%2Eorg", "Contact"));
		tr2.appendChild(td);
		tbody.appendChild(tr2);
		divScrobbler.innerHTML = "";
		table.appendChild(tbody);
		divScrobbler.appendChild(table);
		
		PositionScrobblerDiv(divScrobbler);	
		divScrobbler.style.visibility = 'visible';
		
		setTimeout("getRecentTracks()", 60000);
	}
}

function HandleResize()
{
	PositionScrobblerDiv(document.getElementById('scrobbler'));
}

var http = createRequestObject();
addEvent(window, 'load', InitScrobbler, false);
addEvent(window, 'resize', HandleResize, false);

