function NBAVideoPlayer() {
	this.playerDiv = '';
	this.context = 'nba_standalone_player';
	this.playlist = [];
	this.cvp = {};
	this.backgroundImg = '';
	this.currentlyPlayingID = '';
	this.outputDebug = false;
	
	this.initialize = function(vidPlayerDiv, vidPlaylist, height, width, context) {
		// check for flash
		if (new NBA_FlashDetect().getVersion() == 0) {
			$(vidPlayerDiv).innerHTML = '<a target="_blank" href="http://get.adobe.com/flashplayer/"><img height="' + height + '" width="' + width + '" alt="Flash not detected. Click here to install Adobe Flash Player" src="http://i.cdn.turner.com/nba/nba/.element/img/2.0/global/640x360_no_flash.jpg" /></a>';
			return false;
		};

		if (context) {
			this.context = context;
		}
		this.playerDiv = vidPlayerDiv;
		var ct=vidPlaylist.length;
		for (var i=0; i < ct; i++) {
			var thisVid = vidPlaylist[i];
			thisVid = thisVid.replace(/\.json$/i, '');
			this.playlist[i] = thisVid;
		}

		if(CVP.swfobject.getQueryParamValue('adTest')) {
			document.title = 'AD STAGING: ' + document.title;
			this.context = 'nba_adtest_player';
		} 
		if(CVP.swfobject.getQueryParamValue('nbaDebug')) {
			this.outputDebug = true;
		} 

		CVP_config.height = height;
		CVP_config.width = width;
		CVP_config.flashVars.context = this.context;
		this.cvp = new CVP( CVP_config );
		this.cvp.embedSWF(this.playerDiv);
		// set link in CVP player back to this class
		this.cvp.nbaVideoPlayer = this;
		// first video will be called with this.cvp.onPlayerReady
		// but we need to set a link to it here, for playlisting
		this.currentlyPlayingID = this.playlist[0];

	}
	this.setPlaylist = function(arrPlaylist) {
		this.playlist = arrPlaylist;
	}

	// this.currentlyPlayingID is set in the init above - 
	// in conditions where loadThisVideo variable is within playlist
	this.start = function() {
		this.playVideo(this.currentlyPlayingID);
	}

	/* removed check if video is in this.playlist -
	 * better to have video play regardless */
	this.playVideo = function(videoID) {
		this.log('playVideo(' + videoID + ')');
		this.backgroundImg = nbaVideo.getDefaultBackground();
		this.clearBrandedBackground();
		this.setAdSection();
		this.cvp.play(videoID);

	}

	/* called from this.cvp.onContentPlay */
	this.queueNext = function(videoId) {
		this.log('NBAVideoPlayer.queueNext(' + videoId + ')');
		var marker = this.playlist.indexOf('/video/' + videoId);
		var ct = this.playlist.length - 1;
		if ((marker > -1) && (marker < ct)) {
			marker++;
			this.log('queueing next, marker: ' + marker + ' id: ' + this.playlist[marker]);
			this.cvp.queue(this.playlist[marker]);
		}		
	}

	/* called from this.cvp.onContentPlay */
	this.playNext = function(videoId) {
		this.log('NBAVideoPlayer.playNext(' + videoId + ')');
		var marker = this.playlist.indexOf('/video/' + videoId);
		var ct = this.playlist.length - 1;
		if ((marker > -1) && (marker < ct)) {
			marker++;
			this.log('playing next, marker: ' + marker + ' id: ' + this.playlist[marker]);
			this.cvp.play(this.playlist[marker]);
		}		
	}

	this.setAdSection = function() {
		var newAdSection = nbaVideo.getAdSection();		
		this.log('set ad section: ' + newAdSection);
		this.cvp.setAdSection(newAdSection);
	};

	this.clearBrandedBackground = function() {
		if (this.backgroundDiv != null) {
			this.backgroundDiv.style.backgroundImage = this.backgroundImg;
		}
	}

	this.log = function(message) {
		if ((this.outputDebug) && (typeof(console) != 'undefined')) {
			console.log('[CVP] ' + message)
		}
	}
	this.error = function(message) {
		if ((this.outputDebug) && (typeof(console) != 'undefined')) {
			console.error('[CVP] ' + message)
		}
	}	
};

var CVP_config = {
	id : 'cvp_player',
	width : '576',
	height : '354',
	flashVars : {
		player : 'main',
		context :(typeof pageType !== 'undefined' && pageType === 'teamsite') ? 'team' : 'nba'
	},
	embed : {
		containerSwf : 'http://i.cdn.turner.com/nba/nba/.element/swf/1.1/cvp/nba_container.swf',
		expressInstallSwf : 'http://i.cdn.turner.com/v5cache/turnerplayer/flash/expressInstall.swf',
		//containerSwf : '/.element/swf/1.1/cvp/nba_staging.swf',
		//expressInstallSwf : 'http://i.cdn.turner.com/v5cache/turnerplayer/flash/expressInstall.swf',
		options : {
			quality : 'high',
			bgcolor : '#000000',
			allowFullScreen : 'true',
			allowScriptAccess : 'always',
			wmode: 'transparent'
		}
	},
	onContentPlay : function() {
		var videoId = this.getContentId();
		this.nbaVideoPlayer.queueNext(videoId);
		nbaVideo.onVideoPlay(videoId);

		var videoMetaData = this.getContentEntry(videoId).evalJSON(true);
		nbaVideo.omnitureTracking(videoMetaData);
	},
	onContentDataLoaded : function(videoId) {	
		this.nbaVideoPlayer.onContentDataLoaded(videoId);
	},
	onPlayerReady : function() {
		this.nbaVideoPlayer.log('Player Ready');
		this.nbaVideoPlayer.start();
	},
	onAdPrerollUrl : function(videoId, adUrl) {
		this.nbaVideoPlayer.clearBrandedBackground();
		this.nbaVideoPlayer.currentlyPlayingID = videoId;
	},
	onAdPostrollUrl : function(videoId, adUrl) {},
	onAdStarted : function(token) {
		var videoId = this.getContentId();
		nbaVideo.onVideoPlay(videoId);
	},
	onAdFinished : function(token) {},
	onAdSensitive : function (videoId) {	},
	onVideoPlayerBegin : function(videoId, json) {},
	onVideoPlayerPlay : function(videoId) {	},
	onVideoPlayerStop : function(videoId) {},
	onAdError : function(message) {
		this.nbaVideoPlayer.clearBrandedBackground();
	},
	onVideoPlayerError : function(message) {},
	onContentError : function(errorMessage) {},
	onContentPlayHead : function() {},
	onContentStop : function(videoId) {},
	onContentPause : function(videoId, paused) {},
	onContentCompleted : function() {},
	onVideoTrackingAdClick : function(videoId, url) {},
	onVideoTrackingProgress : function(videoId, percent) {},
	onVideoTrackingLifetime : function(seconds) {},
	onVideoTrackingFullscreen : function(videoId, fullscreen) {},
	onVideoTrackingMuted : function(videoId, muted) {},
	onVideoTrackingPaused : function(videoId, paused) {},
	onVideoTrackingCompleted : function(videoId) {},
	onVideoTrackingReplay : function(videoId) {},
	onVideoTrackingPlay : function(videoId) {},
	onContentTrackingAdCountdown : function(secs) {}
}
