var UniqueID = "musicpopup" // Make each link open in a new window 
var newWinOffset = 0 // Position of first pop-up
var lastClicked = null;

function PlayerStop(){
	document.getElementById('playalaya').innerHTML = "";
}

function initPlayer(){
	anchors = document.getElementsByTagName("a");
	//alert(anchors.length);
	for ( var i=anchors.length-1; i>=0; --i ){
		thisLink = anchors[i].href.split(".");
		if(thisLink.pop() == "mp3"){
			//alert("got one");
			anchors[i].onclick = function() { PlayerOpen('',this); return false };
		}
	}	
}

function PlayerOpen(soundfiledesc,obj) { 
	soundfilepath = obj.href;
	PlayWin = document.getElementById('playalaya');
	
	//alert(lastClicked);
	
	if ((lastClicked) && (lastClicked.href == soundfilepath) && (lastClicked.className != "")) {
		PlayerStop();
		obj.className = "";
		obj.innerHTML = "listen";
	} else {
		//alert("in else");
		if(lastClicked){
			lastClicked.className = "";
			lastClicked.innerHTML = "listen";
		}
		obj.className = "playing";
		obj.innerHTML = "stop";
		//PlayWin = window.open('',UniqueID,'width=320,height=190,top=' + newWinOffset +',left=0,resizable=0,scrollbars=0,titlebar=0,toolbar=0,menubar=0,status=0,directories=0,personalbar=0');
		//PlayWin.focus(); 
		//PlayWin.blur(); 
		
		//alert(soundfilepath);
		
		var winContent = "<B style='font-size:18px;font-family:Verdana,sans-serif;line-height:1.5'>" + soundfiledesc + "</B>";
		
		winContent += "<OBJECT width='300' height='42'>"; 
		winContent += "<param name='SRC' value='" + soundfilepath + "'>";
		winContent += "<param name='AUTOPLAY' VALUE='true'>"; 
		winContent += "<param name='CONTROLLER' VALUE='true'>";
		winContent += "<param name='BGCOLOR' VALUE='#FFFFFF'>"; 
		winContent += "<EMBED SRC='" + soundfilepath + "' AUTOSTART='TRUE' LOOP='FALSE' WIDTH='300' HEIGHT='42' CONTROLLER='TRUE' BGCOLOR='#FFFFFF'></EMBED>";
		winContent += "</OBJECT>"; 
		
		//winContent += "<p style='font-size:12px;font-family:Verdana,sans-serif;text-align:center'><a href='" + soundfilepath +"'>Download this file</a> <SPAN style='font-size:10px'>(right-click or Option-click)</SPAN></p>";
		
		PlayWin.innerHTML = winContent; 
		//PlayWin.document.close(); // "Finalizes" new window 
		//UniqueID = UniqueID + 1 // newWinOffset = newWinOffset + 20 // subsequent pop-ups will be this many pixels lower 
		lastClicked = obj;
	}
}