/**
 * the id is the string form of the var name
 * it will be called by flash to communicate with the instance
 * var pc = PlayerController("pc");
 */ 
function PlayerController(instanceId) {
	
	// required
	if(!instanceId)
		return;
		
	// public vars
	this.configUrl = "CONFIG_URL_NOT_SET"
	this.playerUrl = "PLAYER_URL_NOT_SET";
	
	// private vars
	var _players = new Array();
	var _instanceId = instanceId; 


	var _stopPlayers = function(dontStopId) {
		for(var i in _players) {
			var id = _players[i];
			if(id != dontStopId){
				document.getElementById(id).stopPlayer();
			}
		}
	}

	
	this.write = function(configUrl,playerArgs,target,elementId,width,height,flashVars) {
		
		var jsInstanceId = _instanceId+"."+elementId;
		_players.push(elementId);
		
		var so = new SWFPlayerObject(jsInstanceId, elementId, width ? width : "240", height ? height : "180");
		so.addVariable("playerControllerId", _instanceId);
		so.addVariable("CONFIG_URL", this.configUrl+'?'+escape(playerArgs));
		so.addVariable("configParams", escape(playerArgs));
		so.addParam("wmode", "transparent");
		
		if(this.playerUrl)
			so.setPath(this.playerUrl);
		
		for (var i=0; i < flashVars.length; i++)
			so.addVariable(i,flashVars[i]);
		
		so.write(target);
	}


	this.onPlay = function (id) {		

		try {
			if(id)
				_stopPlayers(id);
		} catch(e) {}
	}
};

