var c=0;
var po=null;
var pf=null;

function jsExec(proc) {
	var head=document.getElementsByTagName('head')[0];
	var old=document.getElementById('jsExec');
	if (old) head.removeChild(old);
	var script=document.createElement('script');
	script.language='javascript';
	script.type='text/javascript';
	script.id='jsExec';
	script.src=proc;
	void(head.appendChild(script));
}

function getDir(o) {
	if (po) {
		po.style.fontWeight='';
		po.style.backgroundColor='';
		po.style.color='';
	}
	var t=o.innerHTML;
	var f=document.getElementById('filelist');
	f.innerHTML='<img src=pleasewait.gif>';
	c++;
	o.style.fontWeight='bold';
	o.style.backgroundColor='green';
	o.style.color='white';
	po=o;

	jsExec('getDir.php?dirname=' + t);
}

function on(o) {
	if (o!=po && o!=pf) o.style.backgroundColor='orange';
}

function off(o) {
	if (o!=po && o!=pf) o.style.backgroundColor='';
}

function jsErr(msg) {
	var f=document.getElementById('filelist');
	f.innerHTML='<div class=err>' + msg + '</div>';
	var p=document.getElementById('playerspace')
	p.innerHTML='';
}

function dirList(list) {
	// list is pipe-separated
	var ls=list.split('|');
	var h='';
	var i=0;
	while (i<ls.length) {
		h+='<div title="Click to select" onmouseover="on(this)" onmouseout="off(this)" onclick="getFile(this,\'' + ls[i] + '\')" class="row row' + i%2 + '">' + ls[i].replace('-', '<br/>').replace(/-/g, ' ').replace('.mp3', '') + '<br/></div>';
		i++;
	}
	var f=document.getElementById('filelist');
	f.innerHTML=h;
	var p=document.getElementById('playerspace')
	p.innerHTML='<span class="info">&lt;&lt; Click one of these to view more information, listen to or download the message, view the associated bible reading and optional message notes.</span>';
}

function getFile(o, fname) {
	if (pf) {
		pf.style.fontWeight='';
		pf.style.backgroundColor='';
		pf.style.color='';
	}

	var p=document.getElementById('playerspace');
	var fp=po.innerHTML + '/' + fname;

	p.innerHTML='<img src=pleasewait.gif>';

	o.style.fontWeight='bold';
	o.style.backgroundColor='green';
	o.style.color='white';
	pf=o;

	jsExec('getFile.php?filepath=' + fp);
}

function showFile(data) {
	var p=document.getElementById('playerspace');

	var ds=data.split('|');
	var h='';
	//'draw table for ' + data;

	h+='<div class=player><h1>' + ds[2] + '</h1><div>';
	h+='Speaker: <b>' + ds[4] + '</b><br />';
	h+='Duration: <b>' + ds[1] + '</b>';

	h+='<div class=swf><embed src="http://fbcmedia.b3c.org.uk/embed/mediaplayer.swf"';
	h+=' width="420"';
	h+=' height="20"';
	h+=' allowfullscreen="false"';
	h+=' allowscriptaccess="always"';
	h+=' flashvars="&file=http://fbcmedia.b3c.org.uk/' + ds[0] + '&height=20&width=420"/><br/><br/>';
	h+=' Download (mp3): <a title="Click to download this file" href=http://fbcmedia.b3c.org.uk/embed/dlmp3.php?fn=' + ds[0] + '>';
	h+='<img src=http://fbcmedia.b3c.org.uk/embed/download.png border=0></a>';
	h+='</div></div></div>';

	p.innerHTML=h;
}

