var action = 'default';
var listName = 'none';
var tmpid = 0;
var activeEdit = 0;
function addList(listName)
{
	var url = 'http://bluelaguna.net/scripts/playlist.php?action=addlist&listname=' + escape(listName);
	var xmlRequest = new XHConn();
	var responseFunction = function responseFunction(response)
	{
		eval("var listId = " + response.responseText);
		var listElement = document.createElement('li');
		listElement.setAttribute('id','list'+listId);
		listElement.appendChild(document.createTextNode(listName));
		var playLink = document.createElement('a');
		playLink.setAttribute('href',"/playlists/" + listId + "/play.php");
		playLink.appendChild(document.createTextNode(" [Play] "));
		listElement.appendChild(playLink);
		var editLink = document.createElement('span');
		editLink.className = "blue";
		editLink.setAttribute('style','cursor: pointer');
		editLink.onclick = function(){ editList(listId);}
		editLink.appendChild(document.createTextNode("[Edit] "));
		listElement.appendChild(editLink);
		document.getElementById('currentlists').appendChild(listElement);
	
	}
	xmlRequest.connect(url, "POST",'',responseFunction);
}
function delList(id,tmpname){
	var answer = confirm('Are you sure you want to delete the playlist "' + tmpname + '"?');
	if (answer){
		var url = 'http://bluelaguna.net/scripts/playlist.php?action=rmlist&listid=' + id;
		action = 'rmlist';
		tmpid = id;
		makeRequest(url);
	}
}
function editList(id)
{
	if(activeEdit != id){
		if(activeEdit != 0){
			prevactive = document.getElementById('list' + activeEdit);
			prevactive.removeChild(prevactive.lastChild);
		}
		activeEdit = id;
		tmpid = id;
		var url = 'http://bluelaguna.net/scripts/playlist.php?action=editlist&listid=' + id;
		action = 'editlist';
		makeRequest(url);
	}
}
function delSong(songid,listid)
{
	tmpSong = songid;
	var url = 'http://bluelaguna.net/scripts/playlist.php?action=delsong&listid=' + listid + '&songid=' + songid;
	action = 'delsong';
	makeRequest(url);
}
function moveSong(element,direction){
	var xmlRequest = new XHConn();
	var item1 = element;
	var order = element.getAttribute("order");
	var itemId = element.getAttribute("itemid");
	var listId = element.parentNode.parentNode.getAttribute("listId");
	var url = '/scripts/playlist.php?action=moveSong' + '&direction=' + direction  
		+ '&itemId=' + itemId+ '&order=' + order + '&listid=' + listId;
	var responseFunction = function responseFunction(response)
	{
	}
	if(direction == 'down'){
		if(element != element.parentNode.lastChild){
			item2 = item1.nextSibling;
			pnode = item1.parentNode;
			pnode.insertBefore(item1,item2.nextSibling);
			xmlRequest.connect(url, "POST",'',responseFunction);
		}
	}
	if(direction == 'up'){
		if(order > 0){
			item2 = item1.previousSibling;
			pnode = item1.parentNode;
			pnode.insertBefore(item1,item1.previousSibling);
			xmlRequest.connect(url, "POST",'',responseFunction);
		}
	}
}
function makeRequest(url){
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
	http_request = new XMLHttpRequest();
	if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
	}
	} else if (window.ActiveXObject) { // IE
	try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
		http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	}
	}
	if (!http_request) {
	alert('Giving up :( Cannot create an XMLHTTP instance');
	return false;
	}
	http_request.onload = showResponse;
	http_request.open('GET', url, true);
	http_request.send(null);
}
	
function showResponse(){
	if(action == 'editlist'){
		parentElement = 'list' + tmpid;
		document.getElementById(parentElement).innerHTML += http_request.responseText;
	}
	if(action == 'rmlist'){
		tmpElement = 'list' + tmpid; 
		listElement = document.getElementById(tmpElement);
		document.getElementById('currentlists').removeChild(listElement);
	}
	if(action == 'delsong'){
		tmpElement = 'edit' + tmpid; 
		listElement = document.getElementById(tmpElement);
		tmpElement = 'song' + tmpSong;
		songElement = document.getElementById(tmpElement);
		listElement.removeChild(songElement);
	
	}
}


