// some variables to save
var currentPosition;
var currentVolume;
var currentItem;

var theID; 
var p1playing;
var p2playing;

// 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 (pid=="player2") { 
			p2playing=true;
			if (p1playing) { sendEvent("player1", 'stop');  p1playing=false; }
		} else { 
			p1playing=true;
			if (p2playing) { sendEvent("player2", 'stop');  p2playing=false; }
		}
	}

	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("placeholder1", "player1", "../media/video/therightway.flv", "media/images/therightway.jpg", false, false);
	createPlayer("placeholder2", "player2", "../media/video/pictures.flv", "media/images/pictures.jpg", false, false);
	createPlayer("placeholder3", "player3", "../media/video/friendship.flv", "media/images/friendship1.jpg", false, false);
}


function createPlayer(thePlace, thePlayer, theFile, theImg, start, icons) {
	var s1 = new SWFObject("embed/player.swf", thePlayer, "320","240","7");
	s1.addParam("allowfullscreen", "true");
	s1.addVariable("enablejs","true");
	s1.addVariable("repeat","true");
	s1.addVariable("shuffle","false");
	s1.addVariable("file", theFile);
	s1.addVariable("javascriptid",thePlayer);
	s1.addVariable("width", "320");
	s1.addVariable("height", "240");
	s1.addVariable("backcolor", "FFFFFF");
	s1.addVariable("controlbar", "over");
	s1.addVariable("displayclick", "fullscreen");
	s1.addVariable("displaywidth", "280");
	s1.addVariable("displayheight", "176");
	if (start) s1.addVariable("autostart", "true");
	if (! icons) s1.addVariable("showicons", "false");
	if (theImg != "") s1.addVariable("image", theImg);
	s1.write(thePlace);
}


