// some variables to save
var currentPosition;
var currentVolume;
var currentItem;

var theID; 
var p1playing;
var p2playing;
var p3playing;
var p4playing;
var p5playing;
var p6playing;
var p7playing;
var p8playing;
var p9playing;
var p10playing;

// these functions are caught by the JavascriptView object of the player.
function sendEvent(thePlayer, typ, prm) { thisMovie(thePlayer).sendEvent(typ,prm); };

function getUpdate(typ,pr1,pr2,pid) { 
	if ((pid != "null")&&(typ=="state")&&(pr1==2)) {

		if (p1playing && pid != "player1") { sendEvent("player1", 'stop');  p1playing=false; }
		if (p2playing && pid != "player2") { sendEvent("player2", 'stop');  p2playing=false; }
		if (p3playing && pid != "player3") { sendEvent("player3", 'stop');  p3playing=false; }
		if (p4playing && pid != "player4") { sendEvent("player4", 'stop');  p4playing=false; }
		if (p5playing && pid != "player5") { sendEvent("player5", 'stop');  p5playing=false; }
		if (p6playing && pid != "player6") { sendEvent("player6", 'stop');  p6playing=false; }
		if (p7playing && pid != "player7") { sendEvent("player7", 'stop');  p7playing=false; }
		if (p8playing && pid != "player8") { sendEvent("player8", 'stop');  p8playing=false; }
		if (p9playing && pid != "player9") { sendEvent("player9", 'stop');  p9playing=false; }
		if (p10playing && pid != "player10") { sendEvent("player10", 'stop');  p10playing=false; }

		if (pid=="player1") { p1playing=true; }
		if (pid=="player2") { p2playing=true; }
		if (pid=="player3") { p3playing=true; }
		if (pid=="player4") { p4playing=true; }
		if (pid=="player5") { p5playing=true; }
		if (pid=="player6") { p6playing=true; }
		if (pid=="player7") { p7playing=true; }
		if (pid=="player8") { p8playing=true; }
		if (pid=="player9") { p9playing=true; }
		if (pid=="player10") { p10playing=true; }

	}

	theID=pid; 
	if(typ == "time") { currentPosition = pr1; }
	else if(typ == "volume") { currentVolume = pr1; } 
	else if(typ == "item") { currentItem = pr1; setTimeout("getItemData(theID, currentItem)",100); }
	var id = document.getElementById(typ);
	id.innerHTML = typ+ ": "+Math.round(pr1);
	pr2 == undefined ? null: id.innerHTML += ", "+Math.round(pr2);
	if(pid != "null") {
		document.getElementById("pid").innerHTML = "(ID: <b><i>"+pid+"</i></b>)";
	}
}


function getItemData(pid, idx) {
	var nodes = "";
	if(pid != "null") {  
		var obj = thisMovie(pid).itemData(idx); 
		for(var i in obj) { 
			nodes += "<li>"+i+": "+obj[i]+"</li>"; 
		}
	} 
	document.getElementById("data").innerHTML = nodes;
}

// This is a javascript handler for the player and is always needed.
function thisMovie(movieName) {
	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}


function initialize() {
	createPlayer("placeholder31", "player1", "audio/playlists/vsm_cj1.xml", "", false, false);
	createPlayer("placeholder32", "player2", "audio/playlists/vsm_cj2.xml", "", false, false);
}


function createPlayer(thePlace, thePlayer, theFile, theImg, start, icons) {
	var s1 = new SWFObject("audio/mediaplayer.swf", thePlayer, "200","170","7");
	s1.addParam("allowfullscreen", "false");
	s1.addVariable("enablejs","true");
	s1.addVariable("repeat","false");
	s1.addVariable("shuffle","false");
	s1.addVariable("file", theFile);
	s1.addVariable("javascriptid",thePlayer);
	s1.addVariable("thumbsinplaylist","false");
	s1.addVariable("width", "180");
	s1.addVariable("height", "160");
	s1.addVariable("displaywidth", "180");
	s1.addVariable("displayheight", "90");
	if (start) s1.addVariable("autostart", "true");
	if (! icons) s1.addVariable("showicons", "false");
	if (theImg != "") s1.addVariable("image", theImg);
	s1.write(thePlace);
}