var player = null;
var currentPosition = 0;
var curPL = '/Audio/playlist_em.php';
var oriPL = curPL;
document.ptitle ='';
function playerReady(thePlayer) {
	player = window.document[thePlayer.id];
	addListeners();
}
function addListeners() {
	if (player) { 
		player.addControllerListener("ITEM", "itemListener");
		player.addModelListener("TIME", "positionListener");

	} else {
		setTimeout("addListeners()",100);
	}
}
function itemListener(obj) { 
		var tmp = document.getElementById("bsound");
		if (tmp) { tmp.title = (player.getPlaylist()[obj.index].title) ? player.getPlaylist()[obj.index].title : document.ptitle; }
}
function positionListener(obj) { 
	currentPosition = obj.position; 
}

function loadFile(theFile) { 
	if (player) {
		player.sendEvent('LOAD', theFile);
		setTimeout("player.sendEvent('NEXT')",999);
		if (theFile.indexOf(".flv") > 0) {
			document.getElementById("wrap").style.top= "40px";
		} else { document.getElementById("wrap").style.top= "-450px"; }
	} else setTimeout("loadFile('"+theFile+"')",999);
}
function createPlayer() {
	var flashvars = {
//			file:"/Audio/playlist_em.xml", 
			autostart:"false",
			shuffle:"true", 
			repeat:"always"
	}
	var params = {
			allowfullscreen:"true", 
			allowscriptaccess:"always"
	}
	var attributes = {
			id:"aplayer",  
			name:"aplayer"
	}
	swfobject.embedSWF("/Audio/player.swf", "Audioplr", "480", "400", "9.0.115", false, flashvars, params, attributes);
}
function pl_control(f){
	var tmp = document.getElementById("bsound").title;
	switch (f) {
		case 'hide':
			if (document.getElementById("wrap").style.top!="-450px" ) {document.getElementById("wrap").style.top = "-380px";} 
		break;
		case 'show':
			document.getElementById("wrap").style.top= "40px";
		break;
		case 'stop':
			player.sendEvent('STOP');
		break;
		case 'prev':
			if (tmp == document.ptitle) {player.sendEvent('SEEK', currentPosition-15);} else {player.sendEvent('PREV');}
		break;
		case 'next':
			if (tmp == document.ptitle) {player.sendEvent('SEEK', currentPosition+15);} else {player.sendEvent('NEXT');}
		break;
		case 'play':
			player.sendEvent('PLAY');
		break;
		case 'sound':
			snd = document.getElementById("bsound");
			if (snd.src.indexOf("nosound.gif") > 0) {
				snd.src="img/sound.gif";
				document.getElementById("plr").style.display='block';
				document.getElementById("plr").style.visibility='visible';
				loadFile(curPL);
//				player.sendEvent('PLAY');
			} else {
				snd.src="img/nosound.gif";
				document.getElementById("wrap").style.top= "-450px";
				document.getElementById("plr").style.display='none';
				document.getElementById("plr").style.visibility='hidden';
				player.sendEvent('STOP');
				curPL = oriPL;
//				snd.title=snd.alt
			}
		break;
		default:
			if (curPL != f ){
				curPL = f;
				if (document.getElementById("plr").style.display=='none') {pl_control('sound');} else {loadFile(curPL);}
			}
	}
}
