/**
* 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(meta){
	for( ix in this.events){
		this.events[ix](meta);
	}
}
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(meta);
		}
		pl.lastText = buf;
		setTimeout('pl.reload()',refresh);
	}
	ajx.submit();
}

PlayList.prototype.createBuyButtons = function(ent){
	var bl = document.createElement('a');
	var url = "/detail.php?src=pl";
	url += "&title=" + escape(ent['Title']);
	url += "&artist=" + escape(ent['Artist']);
	url += "&album=" + escape(ent['Album']);
	if(ent['FileName']){
		url += "&key=" + escape(ent['FileName']);
	}
	bl.appendChild(document.createTextNode(' [info]'));
	bl.setAttribute('href',url);
	return(bl);
}

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');
		var bl = null;
		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:
				bl = this.createBuyButtons(ent);
				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);
		if(bl){
			li.appendChild(bl);
		}else{
			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 + ']');
}
function ChangeImg(id){
	this.id = id;
	this.images = new Array();
	this.ix = 0;
	/* Serial ID */
	this.event_id = -1;
	var an = document.getElementById( this.id + '_A');
	if(an){
		this.orig_a = new Array();
		this.orig_a['href'] = an.href;
		this.orig_a['target'] = an.target;
		this.orig_a['title'] = an.getAttribute('title');
	}
}
ChangeImg.prototype.addImage = function (url){
	this.images.push(url);
}
ChangeImg.prototype.rollover = function(){
	if(this.ix >= this.images.length){
		this.ix = 0;
	}
	var url = this.images[this.ix];
	++this.ix;
	this.setImage(url,null);
}
ChangeImg.prototype.changeImage = function(ev){
	this.restoreAnchor();
	if(! ev){
		this.rollover();
		return(0);
	}
	if(ev['SerialId'] == this.event_id){
		this.rollover();
		return(-1);
	}
	this.event_id = ev['SerialId'];
	var url = ev['Url'];
	if(url){
		this.setImage(url,ev);
	}else{
		this.rollover();
	}
	/* Set an anchor link */
	if(ev['Link']){
		this.setAnchor(ev['Link'],ev['Target'],ev['Title']);
	}
}
ChangeImg.prototype.setAnchor = function(href,target,title){
	var la = document.getElementById( this.id + '_A');
	if(! la){
		return;
	}
	la.href=href;
	la.setAttribute('href',href);
	if(title){
		la.setAttribute('title',title);
		la.title = title;
	}
	if(target){
		la.target = target;
		la.setAttribute('target',target);
	}
}
ChangeImg.prototype.restoreAnchor = function(){
	if(! this.orig_a){
		return(0);
	}
	var la = document.getElementById( this.id + '_A');
	if(! la){
		return;
	}
	la.setAttribute('href',this.orig_a['href']);
	la.setAttribute('target',this.orig_a['target']);
	la.setAttribute('title',this.orig_a['title']);
	la.target = this.orig_a['target'];
	la.href = this.orig_a['href'];
}
ChangeImg.prototype.setImage = function(url,ev){
	var img = document.getElementById(this.id);
	if(! img){
		return(0);
	}
	img.src = url;
	if(! ev){
		return(1);
	}
	if(ev['Title']){
		img.longDesc = ev['Title'];
		img.setAttribute('title',ev['Title']);
	}
}
ChangeImg.prototype.preload = function(){
	for(i = 0; i < this.images.length; ++i){
		var m = new Image();
		m.src = this.images[i];
	}
}
function showHide(id,show_clazz){
	var el = document.getElementById(id);
	if(! el){
		return;
	}
	if(el.getAttribute('class') == 'hide'){
		el.setAttribute('class',show_clazz);
		el.className = show_clazz;
	}else{
		el.setAttribute('class','hide');
		el.className = 'hide';
	}
}
function initialize(){
		pl = new PlayList();
		pl.action="/cgi/playlist.cgi?format=js";
		pl.max_size=5;
		pl.reload();
		ri = new ChangeImg('right_img');
		ri.addImage('/img/dyn_logo/00004.jpg');
ri.addImage('/img/dyn_logo/00007.jpg');
ri.addImage('/img/dyn_logo/00013.jpg');
ri.addImage('/img/dyn_logo/00003.jpg');
ri.addImage('/img/dyn_logo/00014.jpg');
ri.addImage('/img/dyn_logo/00009.jpg');
ri.addImage('/img/dyn_logo/00011.jpg');
ri.addImage('/img/dyn_logo/00006.jpg');
ri.addImage('/img/dyn_logo/00012.jpg');
ri.addImage('/img/dyn_logo/00005.jpg');
ri.addImage('/img/dyn_logo/00008.jpg');
ri.addImage('/img/dyn_logo/00002.jpg');
ri.addImage('/img/dyn_logo/00001.jpg');
ri.addImage('/img/dyn_logo/00010.jpg');
		var first_time = true;
		var cb_roll = function(md){
			if(first_time){
				ri.preload();
				first_time = false;
			}
			var ev = md['EVENTS'];
			ev = ev['PictureImage'];
			if(ev){
				ri.changeImage(ev);
			}else{
				ri.rollover();
			}
		}
		pl.addEvent(cb_roll);
}


