/**
* For working with AJAX and urlencoded form data/form elements.
*
* @param action The action URL to submit form data to.
*/
function FormAjax(action) {
	this.action = action;
	this.req = false;
	/**
	* Set this to GET or POST depending on how you want to send 
	* the request. 
	*/
	this.method = 'POST';
	this.send = null;	
	this.elements = new Array();	
	this.sendHeaders = function(){
		if(this.method == 'POST'){
			this.req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			this.req.setRequestHeader("Content-Length",this.send.length);
		}
	}
}
// Encode form, return URL.
FormAjax.prototype.encodeForm = function(){
	this.send = null;
	var buf = '';
	if(this.elements.length > 0){
		for(var t = 0; t < this.elements.length; ++t){
			buf += encodeURIComponent(this.elements[t].name);
			buf += '=';
			buf += encodeURIComponent(this.elements[t].value);
			buf += '&';
		}
		// Strip last '&'
		buf = buf.substr(0,(buf.length - 1));
	}
	if(this.method == 'POST'){
		this.send = buf;
		return(this.action);
	}else{
		if(this.action.indexOf("?") == -1){
			return(this.action + "?" + buf);
		}else{
			return(this.action + "&" + buf);
		}
	}	
}

FormAjax.prototype.toString = function(){
	return("FormAjax[" + this.method + " " + this.action + "]");	
}

/** 
* Submit AJAX request.
*/
FormAjax.prototype.submit = function(){
	// Normal browsers.
	var req = false;
	if (window.XMLHttpRequest) {
		this.req = new XMLHttpRequest();
	}else if(window.ActiveXObject){
		this.req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if(! this.req){
		var er = new Object();
		er.toString = function(){ return("Can't create XMLHttpRequest"); }
		throw er;
	}
	// We don't have access to 'this' within the function.
	var ajax = this;
	this.req.onreadystatechange = function(){
		if(ajax.req.readyState == 4){
			if(ajax.req.status == 200){
				ajax.handleResponse(ajax.req);
			}else{
				ajax.handleProblem(ajax.req);
			}
			ajax.req = null;
		}
	}
	var url = this.encodeForm();
	this.req.open(this.method,url,true);
	this.sendHeaders();
	this.req.send(this.send);
}
/**
* Add a FORM element. NOTE that this has some issues with
* dynamic forms..
* 
* @param el Object supporting a value and name property.
*/
FormAjax.prototype.addElement = function(el){
	l = this.elements.length;
	this.elements[l] = el;
}

/**
* Add a variable. 
*/
FormAjax.prototype.addVariable = function(name,value){
	var obj = new Object();
	obj.name = name;
	obj.value = value;
	this.addElement(obj);
}
/**
* Assign this to function for processing.	
*
* @param r the XMLHttpRequest object.
*/
FormAjax.prototype.handleResponse = function(r){
	alert(r.responseText);
}

/**
* Assign this for handling a problem.
*
* @param r XMLHttpRequest object.
*/
FormAjax.prototype.handleProblem = function(r){
	alert("Problem: " + r.status + " " + r.statusText);
}

function PlayList(){
	this.max_size = 5;
	this.action = '?';
	this.events = new Array();
	this.lastText = '';
}
PlayList.prototype.addEvent = function(cb) {
	this.events.push(cb);
}
PlayList.prototype.handleChange = function(){
	for( ix in this.events){
		this.events[ix]();
	}
}
PlayList.prototype.reload = function(){
	var ajx = new FormAjax(this.action);
	ajx.addVariable('load','js');
	pl = this;
	ajx.handleResponse = function(r){
		txt = 'var meta = {' + r.responseText + '};';
		eval(txt);
		var refresh = meta['refresh'] * 1000;
		var plist = meta['LIST'];
		var buf = '';
		for(x in plist){
			var ent = plist[x];
			buf +=  ent['Artist'] + ':' + ent['Title'];
		}
		if(buf != pl.lastText){
			pl.refresh_list(plist);
			pl.handleChange();
		}
		pl.lastText = buf;
		setTimeout('pl.reload()',refresh);
	}
	ajx.submit();
}

PlayList.prototype.refresh_list = function(pl) {
	var ul = document.createElement('ul');
	ul.className = 'l365_playlist';
	ul.setAttribute('class','l365_playlist');
	var mx = this.max_size;
	for(x in pl){
		if(--mx < 0){
			break;
		}
		var ent = pl[x];
		var tr_type = document.createElement('span');
		tr_type.className='l365_type';
		tr_type.setAttribute('class','l365_type');
		var tr_time = document.createElement('span');
		tr_time.className='l365_time';
		tr_time.setAttribute('class','l365_time');

		var tr_title = document.createElement('span');
		tr_title.className = 'l365_title';
		tr_title.setAttribute('class','l365_title');

		tr_time.appendChild(document.createTextNode(this.get_ent_time(ent['Seconds'])));
		var title = ' ' + ent['Title'];
		var li = document.createElement('li');
		switch(ent['trackType']){
			case 'ad':
				li.className = 'l365_ad';
				li.setAttribute('class','l365_ad');
				tr_type.innerHTML = '&#x20AC;';
				title = ' ' + ent['desc'];
				break;
			case 'psa':
				li.className = 'l365_psa';
				li.setAttribute('class','l365_psa');
				tr_type.innerHTML = '&#x2022;';
				break;
			default:
				li.className = 'l365_tune';
				li.setAttribute('class','l365_tune');
				tr_type.innerHTML = '&#9836';
				title = ' ' + ent['Artist'] + ' - ' + ent['Title'];
				break;
		}
		/* Max title length, 51 */
		if(title.length > 51){
			title = title.substring(0,51);
		}
		tr_title.appendChild(document.createTextNode(title));
		li.appendChild(tr_type);
		li.appendChild(tr_time);
		li.appendChild(tr_title);
		ul.appendChild(li);
	}
	var el = document.getElementById('playlist');
	if(el){
		el.innerHTML = '';
		el.appendChild(ul);
	}
}
PlayList.prototype.get_ent_time = function (seconds){
	var min = parseInt(seconds / 60);
	var sec = parseInt(seconds % 60);
	if(sec < 10){
		return(' [' + min + ':0' + sec + '] ');
	}
	return('[' + min + ':' + sec + ']');
}
