function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function swapAvatar() {
	MM_swapImage('avatar', 'avatars/' + userId + '-avatar-static.png', 'avatars/' + userId + '-avatar-anim.gif',1);
}
function restoreAvatar() {
	MM_swapImgRestore();
}
var xmlHttpDoc = new Array();
var xmlHttpDocUrl = new Array();
var xmlHttpDocTag = new Array();
var xmlHttpDocCallback = new Array();
var xmlHttpDocWaiting = new Array();
function loadDoc(url, tag, callback) {
	var index;
	for (index=0; index < xmlHttpDoc.length; index++) {
		if (xmlHttpDocTag[index] == tag) {
			break;
		}
	}
	url += "?" + new Date().getTime();
	xmlHttpDoc[index] = null;
	xmlHttpDocTag[index] = tag;
	xmlHttpDocCallback[index] = callback;
	xmlHttpDocUrl[index] = url;
	xmlHttpDocWaiting[index] = false;
	if (window.XMLHttpRequest) {
		xmlHttpDoc[index] = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		// old IE doesn't understand XMLHttpRequest
		xmlHttpDoc[index] = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlHttpDoc[index] != null) {
		xmlHttpDocWaiting[index] = true;
		xmlHttpDoc[index].onreadystatechange=loadDocCB;
		xmlHttpDoc[index].open("GET", url, true);
		xmlHttpDoc[index].send(null);
	}
}
function reloadDoc(tag) {
	var index;
	for (index=0; index < xmlHttpDocTag.length; index++) {
		if (xmlHttpDocTag[index] == tag) {
			break;
		}
	}
	if (index < xmlHttpDocTag.length) {
		xmlHttpDoc[index] = null;
		xmlHttpDocWaiting[index] = false;
		if (window.XMLHttpRequest) {
			xmlHttpDoc[index] = new XMLHttpRequest();
		}
		else if (window.ActiveXObject) {
			// old IE doesn't understand XMLHttpRequest
			xmlHttpDoc[index] = new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (xmlHttpDoc[index] != null) {
			xmlHttpDocWaiting[index] = true;
			xmlHttpDoc[index].onreadystatechange=loadDocCB;
			xmlHttpDoc[index].open("GET", xmlHttpDocUrl[index], true);
			xmlHttpDoc[index].send(null);
		}
	}
}
function loadDocCB() {
	var index;
	// load all docs that are ready
	for (index=0; index < xmlHttpDoc.length; index++) {
		if (xmlHttpDoc[index] != null && xmlHttpDocWaiting[index]) {
			if (xmlHttpDoc[index].readyState == 4) {
				xmlHttpDocWaiting[index] = false;
				if (xmlHttpDoc[index].status == 200) {
					if (xmlHttpDocCallback[index] == null || xmlHttpDocCallback[index](xmlHttpDoc[index])) {
						document.getElementById(xmlHttpDocTag[index]).innerHTML = xmlHttpDoc[index].responseText;
					}
				}
			}
		}
	}
}

function loadXMLDoc(url, tag) {
	var index;
	for (index=0; index < xmlHttpDoc.length; index++) {
		if (xmlHttpDocTag[index] == tag) {
			break;
		}
	}
	xmlHttpDoc[index] = null;
	xmlHttpDocTag[index] = tag;
	xmlHttpDocUrl[index] = url;
	xmlHttpDocWaiting[index] = false;
	if (window.XMLHttpRequest) {
		xmlHttpDoc[index] = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		// old IE doesn't understand XMLHttpRequest
		xmlHttpDoc[index] = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlHttpDoc[index] != null) {
		xmlHttpDocWaiting[index] = true;
		xmlHttpDoc[index].onreadystatechange=loadXMLDocCB;
		xmlHttpDoc[index].open("GET", url, true);
		xmlHttpDoc[index].send(null);
	}
}
function reloadXMLDoc(tag) {
	var index;
	for (index=0; index < xmlHttpDocTag.length; index++) {
		if (xmlHttpDocTag[index] == tag) {
			break;
		}
	}
	if (index < xmlHttpDocTag.length) {
		xmlHttpDoc[index] = null;
		xmlHttpDocWaiting[index] = false;
		if (window.XMLHttpRequest) {
			xmlHttpDoc[index] = new XMLHttpRequest();
		}
		else if (window.ActiveXObject) {
			// old IE doesn't understand XMLHttpRequest
			xmlHttpDoc[index] = new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (xmlHttpDoc[index] != null) {
			xmlHttpDocWaiting[index] = true;
			xmlHttpDoc[index].onreadystatechange=loadXMLDocCB;
			xmlHttpDoc[index].open("GET", xmlHttpDocUrl[index], true);
			xmlHttpDoc[index].send(null);
		}
	}
}
function dumpDom(el) {
	var i;
	document.getElementById("debugPane").innerHTML += "Node name: " + el.nodeName;
	if (el.attributes != null)
		document.getElementById("debugPane").innerHTML += " has " + el.attributes.length + " attributes";
	if (el.childNodes != null)
		document.getElementById("debugPane").innerHTML += " has " + el.childNodes.length + " children";
	document.getElementById("debugPane").innerHTML += "<br/>";
	if (el.attributes != null)
		for (i = 0; i < el.attributes.length; i++)
			document.getElementById("debugPane").innerHTML += "attribute: " + el.attributes.item(i).value + "<br/>";

	if (el.childNodes != null)
		for (i = 0; i < el.childNodes.length; i++) {
			document.getElementById("debugPane").innerHTML += "node: " + el.nodeName + "[" + i + "]: " + el.childNodes[i].nodeName + "<br/>";
			//dumpDom(el.childNodes[i]);
		}
	document.getElementById("debugPane").innerHTML += "<br/>";
}

function debug(str) {
	/*
	document.getElementById("debugPane").innerHTML += str;
	document.getElementById("debugPane").innerHTML += "<br/>";
	*/
	alert(str);
}

function loadXMLDocCB() {
	var index;
	// load all docs that are ready
	for (index=0; index < xmlHttpDoc.length; index++) {
		if (xmlHttpDoc[index] != null && xmlHttpDocWaiting[index]) {
			if (xmlHttpDoc[index].readyState == 4) {
				xmlHttpDocWaiting[index] = false;
				if (xmlHttpDoc[index].status == 200) {
					var parentNode = document.getElementById(xmlHttpDocTag[index]);
					while (parentNode != null && parentNode.firstChild != null)
						parentNode.removeChild(parentNode.firstChild);
						
					if (parentNode != null)
						parentNode.appendChild(xmlHttpDoc[index].responseXML.firstChild);
					//dumpDom(xmlHttpDoc[index].responseXML);
				}
			}
		}
	}
}

function showOnAir(tag) {
	if (isPlaying)
		document.getElementById(tag).innerHTML = '<div class="onAir">ON AIR</div>';
	else
		document.getElementById(tag).innerHTML = '<div class="onAirPaused">ON AIR</div>';
}

var friends = new Array();
var friendTags = new Array();
function addFriend(tag, friend) {
	var index;
	for (index=0; index < friendTags.length; index++) {
		if (friendTags[index].length == 0 || friendTags[index] == tag) {
			break;
		}
	}
	friends[index] = friend;
	friendTags[index] = tag;
}

function showFriendSong(tag, friend) {
	loadDoc("/" + friend + "-embed.xsl", tag, null);
}

function showFriends() {
	var index;
	for (index=0; index < friends.length; index++) {
		if (friends[index] != null && friendTags[index] != null) {
			showFriendSong(friendTags[index], friends[index]);
		}
	}
}

var previousTitle = null;
function checkTitleCB(document) {
	if (previousTitle == null || previousTitle != document.responseText) {
		previousTitle = document.responseText;
		showSongBits();
	}

	return false; // don't load the element, which probably doesn't exist anyways
}

function checkTitle() {
	loadDoc("/" + userId + "-title.xsl", "TITLE", checkTitleCB);
}

var previousOnAir = null;
function checkOnAirCB(document) {
	if (previousOnAir == null || previousOnAir != document.responseText) {
		previousOnAir = document.responseText;
		showOnAir();
	}

	return false; // don't load the element, which probably doesn't exist anyways
}

function checkOnAir() {
	loadDoc("/" + userId + "-isonair.xsl", "CHECKONAIR", checkOnAirCB);
}

function showSongBits() {
	showPlaylist();
	showRecent();
	showNowPlaying();
	showComment();
	showAlbum();
	showMenu();
	//showBanner();
}

function showAllBits() {
	showFriends();
	showSongBits();
	showOnAir();
}

function setupAllBits() {
	showFriends();
	checkTitle();
	checkOnAir();
	setInterval('showFriends()', 10000);
	setInterval('checkTitle()', 5000);
	setInterval('checkOnAir()', 5000);
}

function showPlaylist() {
	loadDoc("/" + userId + "-playlist.xsl", "PLAYLIST", null);
}
function showRecent() {
	loadDoc("/" + userId + "-recent.xsl", "RECENT", null);
}

function showNowPlaying() {
	loadDoc("/" + userId + "-nowplaying.xsl", "NOWPLAYING", null);
}

function showOnAir() {
	loadDoc("/" + userId + "-onair.xsl", "ONAIR", null);
}

function showComment() {
	loadDoc("/" + userId + "-comment.xsl", "COMMENT", null);
}

function showAlbum() {
	loadDoc("/" + userId + "-album.xsl", "ALBUM", null);
}

function showMenu() {
	loadDoc("/" + userId + "-menu.xsl", "MENU", null);
}

function showBanner() {
	loadDoc("/" + userId + "-banner.xsl", "BANNER", null);
}

function loadTitle() {
	loadDoc("/" + userId + "-title.xsl", "TITLE", null);
}
var userId="";
function setUserId(u) {
	userId = u;
}
var isPlaying=false;
var isPlayPauseSwapped=false;
function updatePlayPause() {
	var playPause = document.getElementById("star-play");
	if (playPause != null) {
		if ((isPlaying && !isPlayPauseSwapped) || (!isPlaying && isPlayPauseSwapped)) {
			//playPause.setAttribute("src", "images/star.jpg");
			playPause.setAttribute("src", "img/nowPlayButton.png");
		}
		else {
			//playPause.setAttribute("src", "images/pause.jpg");
			playPause.setAttribute("src", "img/stopPlayButton.png");
		}
	}
}
function playMusic() {
	var holder = document.getElementById("EMBED_TAG");
	if (holder != null) {
		holder.innerHTML = "<embed type='application/x-mplayer2' src='" 
		+ userId + ".mp3.m3u' autostart='1' loop='0' height='100' width='500'/>"
		isPlaying = true;
		//updatePlayPause();
		showOnAir("onAir");
	}
	else
		alert("playMusic didn't find the element!");
}
function playMusic2() {
	var holder = document.getElementById("EMBED_TAG");
	if (holder != null) {
		holder.innerHTML = "<embed type='audio/mpeg' src='"
		+ userId + ".mp3.m3u' autostart='1' loop='0' height='100' width='500'/>"
		isPlaying = true;
		showOnAir("onAir");
		//updatePlayPause();
	}
	else
		alert("playMusic didn't find the element!");
}
function playMusicWindow() {
	window.open("/" + userId + ".mp3.m3u");
}
function stopMusic() {
	var holder = document.getElementById("EMBED_TAG");
	if (holder != null) {
		holder.innerHTML = "";
		isPlaying = false;
		showOnAir("onAir");
		//updatePlayPause();
	}
	else
		alert("stopMusic didn't find the element!");
}
function toggleMusic() {
	if (isPlaying)
		stopMusic();
	else
		playMusic();
}

