
PlaylistReader = function(url, callback) {
	var _url = url;
	var _callback = callback;
	var _tracks = [];
	this.init = function() {
		new AJAX(_url, initCallback).send(null);
	}
	var initCallback = function(root) {
		var node = root.firstChild;
		while (node != null) {
			if (node.nodeType == 1 && node.tagName == 'trackList') {
				parseTracklist(node);
			}
			node = node.nextSibling;
		}
		_callback(_tracks);
	}
	var parseTracklist = function(trackList) {
		var node = trackList.firstChild;
		while (node != null && node != undefined) {
			if (node.nodeType == 1 && node.tagName == 'track') {
				_tracks.push(parseTrack(node));
			}
			node = node.nextSibling;
		}
	}
	var parseTrack = function(track) {
		var node = track.firstChild;
		var trck = new Object();
		var nodeValue;

		while (node != null) {
			if (node.textContent) {
				nodeValue = node.textContent;
			} else {
				nodeValue = node.text;
			}
			switch(node.tagName) {
				case 'title' : 
					trck.title = nodeValue;
					break;
				case 'location' :
					trck.location = nodeValue;
					break;
				case 'info' :
					trck.info = nodeValue;
					break;
				case 'duration' :
					trck.duration = nodeValue;
					break;
			}
			node = node.nextSibling;
		}
		return trck;
	}
}

AJAX = function (url, callbackFunction) {
	var _url = url;
	var xmlhttp=null;
	if (window.XMLHttpRequest) { // Mozilla
		xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
		xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
	}
	if (xmlhttp == null) {
		alert('Your browser does not support XMLHTTP.');
	}
	this.send = function(/*String*/ data) {
		if (xmlhttp != null) {
			xmlhttp.onreadystatechange=stateChange;
			xmlhttp.open('GET', _url, true);
			//xmlhttp.overrideMimeType('text/xml');
			//xmlhttp.open('POST',url,true);
			//xmlhttp.setRequestHeader('Content-Type', 'text/xml');
			xmlhttp.send(null);
			//xmlhttp.send(data);
		} else {
			alert('Your browser does not support XMLHTTP.');
		}
	}
	function stateChange() {
		var root;
		var trackList;
		// if xmlhttp shows "loaded"
		if (xmlhttp.readyState == 4) {
			// if "OK"
			//if (xmlhttp.status == 200 || xmlhttp.status == 0) {
			if (xmlhttp.status == 200) {
				//root = xmlhttp.responseXML.documentElement;
				//tracks = root.getElementsByTagName('track');
				//trackList = root.firstChild.nextSibling;
				//alert(xmlhttp.responseXML);
				//alert(xmlhttp.responseXML.documentElement.tagName);
				callbackFunction(xmlhttp.responseXML.documentElement);
				//callbackFunction(xmlhttp.responseText);
			} else {
				//alert('Problem retrieving XML data, status = ' + xmlhttp.status);
			}
		}
	}
}
