nfl.namespace('nfl.media.video');
nfl.media.video.player	= Class.create();
nfl.media.video.player.prototype = {
	initialize: function(refname,container,host,path,file,contentid,format,bgcolor,timeout){
		this._initArgs		= arguments;
		this._playerName	= "liveplayer.swf";
		this._contentid		= contentid;
		this._referenceName	= refname;
		this._uicontainer	= $(container);
		this._protocol		= '';
		this._host			= host;
		this._path			= path;
		this._file			= file;
		this._flashpath		= "/flash/video/";
		try{
			this._xmlpath		= xmlpath || "";
		}catch(e){
			this._xmlpath		= false;
		}
		this._format		= format || "progVideo";
		this._bgcolor		= bgcolor || "#000000";
		this._customVars	= null;
		this._activeTimeout	= timeout; //null for no inactivity timeout, or the number of minutes if active
		this._activeTimeoutInitialized	= false;
		this._limelight		= false;
		this._showinterstitial		= true;
		this._interstitialfrequency	= 4;
		
		this.setup();
	},
	setup: function(){
		if(this._host && this._host.indexOf("://") >= 0 && !this._limelight){
			//strip out the protocol
			this._protocol	= this._host.substring(0,(this._host.indexOf("://") + 3));
			this._host		= this._host.substring((this._host.indexOf("://") + 3));
		}
		if(this._protocol && this._protocol.indexOf("rtmp") && !this._limelight){
			this._format	= "videoStream";
			this._autoplay	= true;
		}
		if(this._path && this._path.lastIndexOf("/") == (this._path.length -1)){
			this._path		= this._path.substring(0,(this._path.length - 1));
		}
		
		if(this._initArgs && this._initArgs.length > 0){
			for(i=0;i < this._initArgs.length; i++){
				if(typeof this._initArgs[i] == "string" && this._initArgs[i].indexOf('=') > 0){
					nfl.log("nfl.media.video.player: encountered flashvars object notation. "+ this._initArgs[i]);
					var cArray	= this._initArgs[i].split(',');
					if(this._customVars && this._customVars.length > 0){
						this._customVars	= this._customVars.concat(cArray);
					}else{
						this._customVars	= cArray;
					}
				}
			}
		}
		
		Event.observe(window,'load',function(){
			//get the width and height of the flash container
			this._dimensions	= this._uicontainer.getDimensions();
			this.width			= this._dimensions.width;
			this.height			= this._dimensions.height;
			//write out flash content to container
			this.write();
		}.bindAsEventListener(this));
	},
	loadVideo: function(json){
		var params	= json;
		if(typeof params.host != "undefined"){
			params.path		= (params.host + params.path)
		}
		if($('videoplayer')){
			nfl.log("playVideo: "+ params.format +", "+ params.file +", "+ params.path +", "+ params.imgUrl +", "+ params.dartUrl +", "+ params.id);
			//try{
				$('videoplayer').playVideo(params.format,params.file,params.path,params.imgUrl,params.dartUrl,params.id);
			//}catch(e){}
		}
	},
	loadVideoById: function(videoId){
		if($('videoplayer')){
			alert('call video players exposed interface to load video "'+ videoId +'"');
		}
	},
	reset: function(){
		this._uicontainer.update('');
		this.write();
	},
	loadVideoFromJSON: function(json){
		this._protocol		= '';
		this._format		= json.format;
		this._contentid		= json.id;
		this._host			= json.host;
		this._path			= json.path;
		this._file			= json.file;
		if(this._initArgs && this._initArgs.length > 0){
			for(i=5;i < this._initArgs.length; i++){
				if(this._initArgs[i].indexOf('=') > 0){
					nfl.log("nfl.media.video.player: encountered flashvars object notation. "+ this._initArgs[i]);
					var cArray	= this._initArgs[i].split(',');
					this._customVars	= cArray;
				}
			}
		}
		this._customVars[this._customVars.length]	= "cdnId="+ json.type;
		this.reset();
	},
	write: function(){
		this.flashobj = new SWFObject(nfl.global.flashpath + this._flashpath + this._playerName, "videoplayer", this.width, this.height, "8", this._bgcolor, true);
		this.flashobj.addParam("allowScriptAccess", "always");
		this.flashobj.addParam("allowFullScreen", "true");
		this.flashobj.addVariable("format", this._format);
		if(this._protocol){	this.flashobj.addVariable("mediaProtocol", this._protocol); }
		if(this._host){ this.flashobj.addVariable("mediaHost", this._host); }
		if(this._host && this._path){ this.flashobj.addVariable("mediaPath", (this._host + this._path)); }
		if(this._file){ this.flashobj.addVariable("mediaFile", this._file); }
		if(this._contentid){ this.flashobj.addVariable("id", this._contentid); }
		if(this._xmlpath){ this.flashobj.addVariable("playerXmlUrl", this._xmlpath); }
		if(this._referenceName){ this.flashobj.addVariable("jsRef", this._referenceName); }
		
		if(this._customVars != null){
			for(fv=0; fv < this._customVars.length; fv++){
				var fvKey	= this._customVars[fv].split('=')[0];
				var fvVal	= this._customVars[fv].substring(this._customVars[fv].indexOf("=")+1);
				this.flashobj.addVariable(fvKey, fvVal);
			}
		}
		this.flashobj.write(this._uicontainer.id);
		this.setActiveTimeout();
	},
	setActiveTimeout: function(){
		if(this._activeTimeout != null && !this._activeTimeoutInitialized){
			Event.observe(window,'focus',function(e){
				try{ $('videoplayer').resetTimeout(); }catch(e){} //reset flash timer
			}.bind(this));					
			Event.observe(document,'mousemove',function(e){
				try{ $('videoplayer').resetTimeout(); }catch(e){} //reset flash timer
			}.bind(this));
			Event.observe(document,'keypress',function(e){
				try{ $('videoplayer').resetTimeout(); }catch(e){} //reset flash timer
			}.bind(this));
			this._activeTimeoutInitialized	= true;
		}
	}
}
nfl.media.video.VideoGalleryPlayer = Class.create();
nfl.media.video.VideoGalleryPlayer.prototype = {
	getFlashID: function() { return this.flashID },
	getFlashVars: function(obj) {
		return $H({
				format: "progVideo",
				emailAFriendBaseUrl: "/email-a-friend?id=",
				playerbar: nfl.global.imagepath + "/img/flash/video_landing/player_bar_base.jpg",
				playerVolume: "50",
				previewImage: "",
				autoplay: 'true',
				mediaPath: '', 
				mediaFile: '',
				fullscreen: "false"
		}).merge($H(obj));
	},
	setFlashVars: function(obj) {
		return this.flashVars = this.flashVars.merge($H(obj));
	},
	getParameters: function(obj) {
		return $H({ allowScriptAccess: "always", allowFullScreen: "false", wmode: "transparent" }).merge($H(obj));
	},
	setParameters: function(obj) {
		return this.parameters = this.parameters.merge($H(obj));
	},
	initialize: function(id, adURL, flashVars, parameters) {
		this.id			= id;
		this.adURL		= adURL;
		this.flashVars	= this.getFlashVars(flashVars);
		this.parameters		= this.getParameters(parameters);
		this.flashID		= id.replace(/-/g, "") + "Flash";
	},
	write: function() {
		// add the video SWF
		if (document.getElementById(this.flashID)) { throw('Could not write VideoGalleryPlayer: element #' + this.flashID + ' already exists.') }
		so = new SWFObject(nfl.global.imagepath + "/flash/videogallery.swf",this.flashID,"646","400","8", "#FFFFFF");
		this.parameters.each(function(pair) { so.addParam(pair.key, pair.value) });
		this.flashVars.each(function(pair) { so.addVariable(pair.key, encodeURIComponent(pair.value)) });
		so.write(this.id);
		so = null;
	}
};