function NBAVideoPlayer() {
	this.playerDiv = '';
	this.context = 'nba_standalone_player';
	this.playlist = [];
	this.cvp = {};
	this.backgroundImg = '';
	this.currentlyPlayingID = '';
	this.adKeyStr = '';
	this.useAdKey = false;
	this.useHTML5 = /\b(iPad|iPod|iPhone)\b/.test(window.navigator.userAgent);
	this.outputDebug = false;
	
	String.prototype.startsWith = function(str) 
{return (this.match("^"+str)==str)}
	
	path = window.location.pathname;
	
	teamPathURL = path ;
	
if (teamPathURL.startsWith("/video/games/blazers")) {
this.context = 'nba_blazers_highlights' ;
} else if (teamPathURL.startsWith("/video/games/bobcats")) {
this.context = 'nba_bobcats_highlights' ;
} else if (teamPathURL.startsWith("/video/games/bucks")) {
this.context = 'nba_bucks_highlights' ;
} else if (teamPathURL.startsWith("/video/games/bulls")) {
this.context = 'nba_bulls_highlights' ;
} else if (teamPathURL.startsWith("/video/games/cavaliers")) {
this.context = 'nba_cavaliers_highlights' ;
} else if (teamPathURL.startsWith("/video/games/celtics")) {
this.context = 'nba_celtics_highlights' ;
} else if (teamPathURL.startsWith("/video/games/clippers")) {
this.context = 'nba_clippers_highlights' ;
} else if (teamPathURL.startsWith("/video/games/grizzlies")) {
this.context = 'nba_grizzlies_highlights' ;
} else if (teamPathURL.startsWith("/video/games/hawks")) {
this.context = 'nba_hawks_highlights' ;
} else if (teamPathURL.startsWith("/video/games/heat")) {
this.context = 'nba_heat_highlights' ;
} else if (teamPathURL.startsWith("/video/games/hornets")) {
this.context = 'nba_hornets_highlights' ;
} else if (teamPathURL.startsWith("/video/games/jazz")) {
this.context = 'nba_jazz_highlights' ;
} else if (teamPathURL.startsWith("/video/games/kings")) {
this.context = 'nba_kings_highlights' ;
} else if (teamPathURL.startsWith("/video/games/knicks")) {
this.context = 'nba_knicks_highlights' ;
} else if (teamPathURL.startsWith("/video/games/lakers")) {
this.context = 'nba_lakers_highlights' ;
} else if (teamPathURL.startsWith("/video/games/magic")) {
this.context = 'nba_magic_highlights' ;
} else if (teamPathURL.startsWith("/video/games/mavericks")) {
this.context = 'nba_mavericks_highlights' ;
} else if (teamPathURL.startsWith("/video/games/nets")) {
this.context = 'nba_nets_highlights' ;
} else if (teamPathURL.startsWith("/video/games/nuggets")) {
this.context = 'nba_nuggets_highlights' ;
} else if (teamPathURL.startsWith("/video/games/pacers")) {
this.context = 'nba_pacers_highlights' ;
} else if (teamPathURL.startsWith("/video/games/pistons")) {
this.context = 'nba_pistons_highlights' ;
} else if (teamPathURL.startsWith("/video/games/raptors")) {
this.context = 'nba_raptors_highlights' ;
} else if (teamPathURL.startsWith("/video/games/rockets")) {
this.context = 'nba_rockets_highlights' ;
} else if (teamPathURL.startsWith("/video/games/sixers")) {
this.context = 'nba_sixers_highlights' ;
} else if (teamPathURL.startsWith("/video/games/spurs")) {
this.context = 'nba_spurs_highlights' ;
} else if (teamPathURL.startsWith("/video/games/suns")) {
this.context = 'nba_suns_highlights' ;
} else if (teamPathURL.startsWith("/video/games/thunder")) {
this.context = 'nba_thunder_highlights' ;
} else if (teamPathURL.startsWith("/video/games/timberwolves")) {
this.context = 'nba_timberwolves_highlights' ;
} else if (teamPathURL.startsWith("/video/games/warriors")) {
this.context = 'nba_warriors_highlights' ;
} else if (teamPathURL.startsWith("/video/games/wizards")) {
this.context = 'nba_wizards_highlights' ;
} else if (teamPathURL.startsWith("/video/teams/blazers") || teamPathURL.startsWith("/blazers/video/originals")) {
this.context = 'nba_blazers_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/bobcats") || teamPathURL.startsWith("/bobcats/video/originals")) {
this.context = 'nba_bobcats_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/bucks") || teamPathURL.startsWith("/bucks/video/originals")) {
this.context = 'nba_bucks_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/bulls") || teamPathURL.startsWith("/bulls/video/originals")) {
this.context = 'nba_bulls_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/cavaliers")|| teamPathURL.startsWith("/cavaliers/video/originals")) {
this.context = 'nba_cavaliers_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/celtics") || teamPathURL.startsWith("/celtics/video/originals")) {
this.context = 'nba_celtics_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/clippers") || teamPathURL.startsWith("/clippers/video/originals")) {
this.context = 'nba_clippers_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/grizzlies") || teamPathURL.startsWith("/grizzlies/video/originals")) {
this.context = 'nba_grizzlies_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/hawks") || teamPathURL.startsWith("/hawks/video/originals")) {
this.context = 'nba_hawks_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/heat") || teamPathURL.startsWith("/heat/video/originals")) {
this.context = 'nba_heat_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/hornets") || teamPathURL.startsWith("/hornets/video/originals")) {
this.context = 'nba_hornets_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/jazz") || teamPathURL.startsWith("/jazz/video/originals")) {
this.context = 'nba_jazz_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/kings") || teamPathURL.startsWith("/kings/video/originals")) {
this.context = 'nba_kings_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/knicks") || teamPathURL.startsWith("/knicks/video/originals")) {
this.context = 'nba_knicks_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/lakers") || teamPathURL.startsWith("/lakers/video/originals")) {
this.context = 'nba_lakers_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/magic") || teamPathURL.startsWith("/magic/video/originals")) {
this.context = 'nba_magic_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/mavericks") || teamPathURL.startsWith("/mavericks/video/originals")) {
this.context = 'nba_mavericks_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/nets") || teamPathURL.startsWith("/nets/video/originals")) {
this.context = 'nba_nets_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/nuggets") || teamPathURL.startsWith("/nuggets/video/originals")) {
this.context = 'nba_nuggets_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/pacers") || teamPathURL.startsWith("/pacers/video/originals")) {
this.context = 'nba_pacers_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/pistons") || teamPathURL.startsWith("/pistons/video/originals")) {
this.context = 'nba_pistons_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/raptors") || teamPathURL.startsWith("/raptors/video/originals")) {
this.context = 'nba_raptors_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/rockets") || teamPathURL.startsWith("/rockets/video/originals")) {
this.context = 'nba_rockets_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/sixers") || teamPathURL.startsWith("/sixers/video/originals")) {
this.context = 'nba_sixers_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/spurs") || teamPathURL.startsWith("/spurs/video/originals")) {
this.context = 'nba_spurs_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/suns") || teamPathURL.startsWith("/suns/video/originals")) {
this.context = 'nba_suns_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/thunder") || teamPathURL.startsWith("/thunder/video/originals")) {
this.context = 'nba_thunder_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/timberwolves")|| teamPathURL.startsWith("/timberwolves/video/originals")) {
this.context = 'nba_timberwolves_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/warriors") || teamPathURL.startsWith("/warriors/video/originals")) {
this.context = 'nba_warriors_teamoriginals' ;
} else if (teamPathURL.startsWith("/video/teams/wizards") || teamPathURL.startsWith("/wizards/video/originals")) {
this.context = 'nba_wizards_teamoriginals' ;

}



	
	
	this.initialize = function(vidPlayerDiv, vidPlaylist, height, width, context) {
		// check for flash
		if (new NBA_FlashDetect().getVersion() == 0 && !(this.useHTML5)) {
			$(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;
		}
		if(CVP.swfobject.getQueryParamValue('from_espn') == 'yes') {
			this.adKeyStr = 'from_espn=yes';
			this.useAdKey = true;
		} 

		// Only show the html5 video player for ipad if US or CA.  Not setting 
		// the configUrl and containerUrl will result in a 'null' video player being output.
		// isUSorCA is set in the header using ESI.
		if (this.useHTML5 && typeof isUSorCA != 'undefined' && isUSorCA == 'yes') {
			// Need to make the video area 30 pixels shorter to eliminate letter boxing on the ipad:
			height = height-30;
			// teams, dleague and wnba use a different fallback file name from the default set in config.xml
			CVP_config.perVideoFallbacks = [
			                                {criteria : '${video.category}**${video.dateCreated}',
			                                evaluate : function(criteria) {
			                                	var dateCreated = criteria.split('**')[1];
			                                	var dateObj = new Date(dateCreated);
			                                	var split = criteria.split('**')[0].split('/');
			                                	// utc 1296777600000 (Feb 4, 2011) date after which mp4 encodings are available for team videos.
			                                	return ((split[0] == 'teams' || split[0] == 'dleague' || split[0] == 'wnba') && (dateObj.getTime() < 1296777600000)) ? true : false;
			                                	},
			                                filter : 'http://nba.cdn.turner.com/nba/big/${video.id}_nba_android_high.mp4'
			                                }];

			CVP_config.flashVars.configUrl = "http://www.nba.com/.element/swf/1.1/cvp/config.xml";
			CVP_config.flashVars.containerUrl ="http://www.nba.com/.element/swf/1.1/cvp/container.xml";
		}
		CVP_config.flashVars.html5_js_url = "http://i.cdn.turner.com/xslo/cvp/js/cvp.html5/cvp.html5_0.9.5.min.js";
		CVP_config.flashVars.context = this.context;
		CVP_config.height = height;
		CVP_config.width = width;
		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();
		// Override default backgroundImg with ESPN/fantasy skin:
		if (nbaVideo.mainPlaylist.key == 'Fantasy' && this.adKeyStr == 'from_espn=yes') {
			this.backgroundImg = 'url(http://i.cdn.turner.com/nba/nba/.element/img/2.0/sect/video/nba.fantasy.video.bg.jpg)'; 
		}
		this.clearBrandedBackground();
		if (this.useAdKey) {
			this.setAdKeyValue();
		}
		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.setAdKeyValue = function() {
		var arrKeyValue = this.adKeyStr.split('=');
		var adKey = arrKeyValue[0];
		var adValue = arrKeyValue[1];
		this.log('set ad key/value: ' + adKey + '=' + adValue);

		if (this.channel != nbaVideo.mainPlaylist.key) {
			this.adKeyStr = arrKeyValue[0] + '=no';
		} else {
			this.cvp.setAdKeyValue(adKey, adValue);
		}
	}; 

	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);
		
		
		if (this.nbaVideoPlayer.useAdKey) {
			this.nbaVideoPlayer.setAdKeyValue();
		}

		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;
		var videoMetaData = this.getContentEntry(videoId).evalJSON(true);
		nbaVideo.socialTools(videoMetaData);
	},
	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() {
		nbaOmEvent.onStreamEnd();
	},
	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) {}
}

