/**
* the id is the string form of the var name
* it will be called by flash to communicate with the instance
* var sw = SwfPlayerObject("sw");
*/ 
function SWFPlayerObject(instanceId,id,width,height,version,bgcolor) {

// required. for flash to reference the page.
if(!instanceId)
return;

this.wmp = null;
this.coad = null;

version = version ? version : "8";
width = width ? width : 300;
height = height ? height : 300;
bgcolor = !isNaN(bgcolor) ? bgcolor : "#000000";

var so = new SWFObject("/player/embed/", id, width, height, version, bgcolor);

if(location.search.indexOf("jsDebugOn")!=-1)
so.addVariable("jsDebuggerFunction",instanceId+".log");

if(location.search.indexOf("lcDebugOn=true")!=-1)
so.addVariable("lcDebugOn","true");

this.setPath = function(path) {
so.setAttribute('swf', path);
}

// a javascript reference to this by string.
so.addVariable("jsInstanceId",instanceId)

// a reference to the embedded plug in
so.addVariable("elementId",id);

this.addParam = function(name,value) {
so.addParam(name,value);
so.addVariable(name,value);
}

this.addVariable = function(name,value) {
so.addVariable(name,value);
}

this.setAttribute = function(name,value) {
so.setAttribute(name,value);
}

this.addWMPSupport = function(target) {
this.wmp = new WMPBrowser(target,id);
}

this.addCoAd = function(target) {
this.coad = new CoAdBrowser(target);
}

this.write = function(target) {
so.write(target);
}

this.openAndFocus = function(url,name,features){
var o = window.open(url, name, features);
o.focus();
}

this.log = function(statement){

if(document.getElementById("ta") && statement){
document.getElementById("ta").value += ('\n'+statement);	
}else{
var d = document.createElement('div');
var width = "100%";
var height = 200;
var left = 10;
var top = screen.availHeight - height - ((window.ActiveXObject) ? 60 : 90);
var newStyle = 'display:inline;position:absolute;z-index:300;left:' + left + 'px;top:' + top + 'px;width:' + width + 'px;height:' + height + 'px;';	
d.style.cssText = newStyle;
document.body.appendChild(d);	

// textarea
var ta= document.createElement('textarea');
ta.setAttribute("name","ta");
ta.setAttribute("id","ta");
ta.setAttribute("cols","130");
ta.setAttribute("rows","8");
ta.setAttribute("wrap","off");
d.appendChild(ta);	
var db= document.createElement('button');
db.setAttribute("name","db");
db.setAttribute("id","db");
var buttext = document.createTextNode('clear');
db.appendChild(buttext);
db.onclick = function () {
document.getElementById("ta").value = "";
};

d.appendChild(db);	
if(statement){
ta.value+=statement;
}
}
}
};

/**
* 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) {}
}
};


