var mtvnPlayers = [];

function MTVNPlayerController(id,onLoadFunctionName){
  mtvnPlayers[id] = this;
  this.onLoadFunctionName = onLoadFunctionName;
  this.playerLoaded = false;
  this.playerId = id;
  this.player = null;

  this._onPlayerLoaded = function(){
    this.playerLoaded = true;
    this.player = (navigator.appName.indexOf("Microsoft") != -1) ? window[this.playerId] : document[this.playerId];
    var f = eval(onLoadFunctionName);
    f(this);
  }

}

CMT = new Object();

// global vars
var controller;
var coad = new CoAdBrowser("coadTarget");

CMT.Player = new function(){
	
	this.configParams;
	this.playerMediaHost;
	this.videoUri;
	this.continuousPlay = true;
	this.fullEpisode = false;
	
	var coAdInterval;
	var onNoAdObject;
	
	
	this.init = function() {}
	
	this.addPlayer = function(host,uri,config,fullepisode,sid) {
		var flashVersion = "9.0.115.0";		
		var configParams = config;
		var extendedConfigParams = config;
		this.configParams = config;
		this.playerMediaHost = host;
		this.videoUri = uri;
		this.fullEpisode = fullepisode;
	
		var so = new SWFObject(CMT.Player.playerMediaHost + "/" + CMT.Player.videoUri + "/cp~" + encodeURIComponent(extendedConfigParams), "embeddedPlayer", "100%", "100%", flashVersion, "#cccccc");
		//so.addVariable("configParams", encodeURIComponent(configParams));
		so.addVariable("autoPlay", "true");

		if (sid)
			so.addVariable("sid", sid);
		
		so.addParam("allowFullScreen",true);
		so.addParam("allowScriptAccess","always");
		so.addParam("wmode", "transparent");
		so.addParam("swliveconnect",true);
		so.addParam("bgcolor", "#000000");
		so.write("videoPlayer");

		controller = new MTVNPlayerController("embeddedPlayer","CMT.Player.onPlayerLoaded");
		
		// set the share link
		document.getElementById("shareLink").value=location.href;
	}
	
	this.onPlayerLoaded = function(controller){
		controller.player.addEventListener('NO_AD','CMT.Player.onNoAd');
	}
	
	this.onNoAd = function(metadata) {
		var adObject = {};
		adObject.url = metadata.adUrl.replace(/pfadx/, "adi").replace(/640x480/,"300x250").replace(/text\/xml/,"text/html");
		adObject.width = 300;
		adObject.height = 250;
		onNoAdObject = adObject;
		CMT.Player.noAdSetCoad();
		coAdInterval = setInterval('CMT.Player.noAdSetCoad()', 60000);
	}
	
	this.noAdSetCoad = function() {
		CMT.Player.setCoad(onNoAdObject);
	}
	
	this.setCoad = function(adObject) {
		var src = adObject.url;
		var w = adObject.width;
		var h = adObject.height;
		coad.updateWindow(src, w, h);
	}
	
	this.isContinuousPlay = function() {
		return this.continuousPlay;
	}
}

CMT.Utils = new function(){
	this.init = function(){}
	
	this.select = function(obj){
		obj.focus();
		obj.select();
	}
	
	this.pageUrl = function() {
		return location.href;
	}
}


var mtvnSetCoad = CMT.Player.setCoad;

