var playlistRequest;
var playlistURL;
var refreshPlaylist;
var refreshPlaylistInterval;

function xmlPlaylistLoaded() 
{
	if (playlistRequest.readyState == 4) {
		if (playlistRequest.status == 200) {
			playlistXML = playlistRequest.responseXML;
			if(navigator.appName=="Microsoft Internet Explorer")
			{
				var song = playlistXML.childNodes[1].getElementsByTagName("song")[0];
			} else {
				var song = playlistXML.childNodes[0].getElementsByTagName("song")[0];
			}
			var numSong = 1;
			var title = new Array();
			var artist = new Array();
			var album = new Array();
			while(song!=null)
			{
				if(song.nodeType==1){	// hack pour virer les lignes vides
					title[numSong] = song.getElementsByTagName("title")[0].firstChild.nodeValue;
					artist[numSong] = song.getElementsByTagName("artist")[0].firstChild.nodeValue;
					album[numSong] = song.getElementsByTagName("album")[0].firstChild.nodeValue;
					numSong++;
				}
				song = song.nextSibling;
			}
			var filename = artist[2]+"_"+album[2]+".jpg";
			filename = filename.replace(new RegExp(" ", "g"), "_");
			filename = filename.replace(new RegExp("'", "g"), "_");
			filename = filename.replace(new RegExp("/", "g"), "_");
			filename = filename.toLowerCase();
			// on définit l'url de l'image
			var jaquette = "/images/albums/"+filename;
			
			// on vérifie qu'elle existe
			imagexmlreq = XmlHttpRequestObject();
			imagexmlreq.open("GET", jaquette, false);
			imagexmlreq.send(null);
			if(imagexmlreq.readyState==4 && imagexmlreq.status!=200)
			{
				jaquette = '/images/albums/nopicture.png';	
			}
			document.getElementById("jaquette").innerHTML = '<img src="'+jaquette+'" alt="" height="64" width="64" />';
			var songsText = "";
			for(i=1;i<artist.length;i++)
			{
				if(i==1) {
					songsText += "<div><nobr>next: "+artist[i]+" - "+title[i]+"</nobr></div>";
				} else if(i==2) {
					songsText += "<div class='currentsong'><nobr><a href='http://www.morow.com/morow.m3u'><img src='/images/icons/speaker.png' alt='' /> "+artist[i]+" - "+title[i]+"</a></nobr></div>";
				} else {
					songsText += "<div><nobr>"+artist[i]+" - "+title[i]+"</nobr></div>";
				}
			}
			document.getElementById("songs").innerHTML = songsText;
		}
		else {
			document.getElementById("songs").innerHTML = "Error fetching data: HTTP status " + playlistRequest.status;
		}
	}
}

function refreshIcon(action)
{
	if(action=="show") { document.getElementById('refresh-icon').style.display = "inline"; }
	else if(action=="hide") { document.getElementById('refresh-icon').style.display = "none"; }
	else { document.getElementById('refresh-icon').style.display = "none"; }
}

function refreshPlaylist()
{
	//refreshIcon('show');
	playlistRequest.open("GET", playlistURL+"?"+new Date().getTime(), true);
	playlistRequest.onreadystatechange = xmlPlaylistLoaded;
	playlistRequest.send(null);
	refreshPlaylistInterval = setTimeout("refreshPlaylist()", 30000);
}

function stopTimer()
{
	clearTimeout(refreshPlaylistInterval);
}


function playlistInit()
{
	playlistURL = "/playlist.xml";
	playlistRequest = XmlHttpRequestObject();
}

function XmlHttpRequestObject()
{
	if (window.XMLHttpRequest){
		// If IE7, Mozilla, Safari, etc: Use native object
		var xmlHttp = new XMLHttpRequest();
	}
	else
	{
		if (window.ActiveXObject){
			// ...otherwise, use the ActiveX control for IE5.x and IE6
			var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}