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 ) {
			pl_control("show");
		} else { document.getElementById("wrap").style.top= "-9000px"; }
	} else setTimeout("loadFile('"+theFile+"')",999);
}
function createPlayer() {
	var flashvars = {
			autostart:"false",
			shuffle:"true", 
			repeat:"always"
	}
	var params = {
			allowfullscreen:"true", 
			allowscriptaccess:"always"
	}
	var attributes = {
			id:"aplayer",  
			name:"aplayer"
	}
	swfobject.embedSWF("/Audio/player.swf", "Audioplr", "100%", "90%", "9.0.115", false, flashvars, params, attributes);
}
function pl_control(f){
	if (player) {
		var tmp = document.getElementById("bsound").title;
		wrp=document.getElementById("wrap");
		switch (f) {
			case 'hide':
				if (wrp.style.top!="-9000px" ) {wrp.style.top = -90;wrp.style.height = 120;wrp.style.width = 250;} 
			break;
			case 'show':
				wrp.style.top= "40px";
				wrp.style.left= "1%";
				wrp.style.width= "98%";
				wrp.style.height= parseInt(document.getElementById("mfr").width/1.33)+"px";
			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("wrap").style.top= "-9000px";
					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);}
				}
		}
	}
}

