//load the turner video player
function cvp_loadVideoPlayer(player, width, height, divId, wmodeBool) {
	var isAdTest = TURNERPLAYER.swfobject.getQueryParamValue('adTest');
	var cvpPlayerTest = '';
	
	if(isAdTest) {
		cvpPlayerTest = '_adTest';
	}
	
	var flashvars = {};
	flashvars.player = player+cvpPlayerTest;	//ex: 'main', 'hp'
	
	var params = {};
	params.quality = "high";
	params.bgcolor = "#000000";
	params.allowFullScreen = "true"; 
	params.allowScriptAccess = "always";
	if(wmodeBool) {
		params.wmode = "transparent";
	}
	
	TURNERPLAYER.embedSWF("http://i.cdn.turner.com/pga/.element/apps/cvp/1.0/swf/pga_video.swf", divId, width, height, "http://i.cdn.turner.com/pga/.element/swf/1.0/global/expressInstall.swf", flashvars, params);
}

//play a video with vId
function cvp_playVideo(vId, override) {
	vId = cvp_prepareVideoId(vId);
	
	if(!override) {
		TURNERPLAYER.play(vId);
	} else {
		TURNERPLAYER.play(vId, override);
	}
}

//enqueue videos from playlist var
function cvp_enqueueVideos() {
	if(!playlist) {
		return;
	}
	
	var len = playlist.length;
	
	for(var i=0; i<=len-1; i++) {
		if(i==0) {
			TURNERPLAYER.play(cvp_prepareVideoId(playlist[i].url));
		} else {
			TURNERPLAYER.queue(cvp_prepareVideoId(playlist[i].url));
		}
	}
}

//enqueue videos from playlist var - with the option to take parameters
function cvp_enqueueVideos2(override) {
	if(!playlist) {
		return;
	}
	
	var len = playlist.length;
	
	for(var i=0; i<=len-1; i++) {
		if(i==0) {
			TURNERPLAYER.play(cvp_prepareVideoId(playlist[i].url),override);
		} else {
			TURNERPLAYER.queue(cvp_prepareVideoId(playlist[i].url),override);
		}
	}
}
//make sure the vId is in a format that we expect
function cvp_prepareVideoId(vId) {
	if (vId.indexOf('/video/') < 0) {	//sometimes /video is in the url, sometimes it is not.  make sure it is.
		vId = '/video'+vId;
	}
	
	if(/\/$/.test(vId)) {	//remove trailing slash
		vId=vId.replace(/\/$/,"");
	}
	return vId;
}

//populate the headline and description for a given video
function cvp_updateHeadlineAndDescription(vId, headlinEl, descEl) {
	var jsonText = TURNERPLAYER.getVideoEntry(vId);
	var videoObj = jsonText.evalJSON();
	
	if(!videoObj) {	//if the video object is null for some reason, update headline and desc with blank strings
		document.getElementById(headlinEl).innerHTML = '';
		document.getElementById(descEl).innerHTML = '';
	} else {
		document.getElementById(headlinEl).innerHTML = videoObj.headline;
		document.getElementById(descEl).innerHTML = videoObj.description;
	}
}

//populate the headline and description for HP video
function cvp_updateHeadlineAndDescriptionHP() {
	$('videoTitle').innerHTML = playlist[0].title;
	$('videoDescription').innerHTML = playlist[0].description;
}

//show countdown for ad playback
function cvp_adCountDown(secs, headlineDiv, descDiv) {
	var secondWord = (secs == "1")?"second":"seconds";
	
	$(descDiv).innerHTML = '';
	$(headlineDiv).innerHTML = 'Next video will start in  ' + secs + ' ' + secondWord;
}