//load the cvp
function cvp_loadVideoPlayer(player, width, height, divId, wmodeBool) {
	/************************
		Insert the player
	 ************************/
	var flashvars = {};
	flashvars.player = player;

	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/pgatour/.element/apps/cvp/3.0/swf/pgatour_video.swf", divId, width, height, "http://i.cdn.turner.com/pgatour/.element/swf/3.0/expressInstall.swf", flashvars, params);
}

//update the headline and description for the currently playing video
function cvp_updateVideoHeadlineDescription(divId, vId) {
	var vidDiv = document.getElementById(divId);
	vidDiv.innerHTML = '';	//clear div contents, for replays
	
	var videoUrl = document.getElementById('tourVidDetailURL');
	videoUrl.innerHTML = '';
	
	var headline = document.createElement('h3');
	vidDiv.appendChild(headline);
	var descript = document.createElement('p');
	vidDiv.appendChild(descript);
	
	headline.id = "video_headline";
	descript.id = "video_description";
	
	var vidUrlInput = document.createElement('input');
	videoUrl.appendChild(vidUrlInput);
	
	vidUrlInput.setAttribute('id', 'staticlink');
	vidUrlInput.setAttribute('name', 'staticlink');
	
	var jsonText = TURNERPLAYER.getVideoEntry(vId);
	var videoObj = jsonText.evalJSON();	//create a video object

        if(!videoObj) {	//if the video object is null for some reason, update headline and desc with blank strings

		headline.appendChild(document.createTextNode(''));
		descript.appendChild(document.createTextNode(''));
		vidUrlInput.setAttribute('style', 'display:none');
	} else {
		headline.appendChild(document.createTextNode(videoObj.headline));
		descript.appendChild(document.createTextNode(videoObj.description));
		
		var objVId = videoObj.id;
		if(/\/video\/video/.test(objVId)) {	//remove /video.json from end
			objVId = objVId.replace(/\/video\/video/, "/video");
		}
		
                var vidPBaseUrl = "http://www.pgatour.com/video/";
                var endIndex = location.href.indexOf('?');
                if (endIndex == -1) { endIndex = location.href.lastIndexOf('/') + 1; }
                if (endIndex > 0) { vidPBaseUrl = location.href.substring(0,endIndex) + '?'; }

		vidUrlInput.setAttribute('style', 'display:block');
		vidUrlInput.setAttribute('onclick', 'this.focus();this.select();');
		vidUrlInput.setAttribute('value', vidPBaseUrl+objVId);
		
		if(videoObj.urls.relateds != '') {
			cvp_updateSourceLink(videoObj, vidDiv);
		}
	}
}

//update the video source link (ex: Courtesy of blah.com)
function cvp_updateSourceLink(videoObj, vidDiv) {
	var sourceP = document.createElement('p');
	vidDiv.appendChild(sourceP);
	sourceP.id = 'video_source';
	
	var sourceA = document.createElement('a');
	sourceP.appendChild(sourceA);
	
	if(videoObj.urls.relateds.length == null) {	//only one related item
		sourceA.setAttribute('href', videoObj.urls.relateds.story.url.html);
		sourceA.appendChild(document.createTextNode(videoObj.urls.relateds.story.headline));
	} else {	//2 or more related items
		sourceA.setAttribute('href', videoObj.urls.relateds[0].story.url.html);	//we will assume that the first related url is always what we are looking for.
		sourceA.appendChild(document.createTextNode(videoObj.urls.relateds[0].story.headline));
	}
}

//populate div with video list
function cvp_populateVideoList() {
	var content = '';
	for (var i=0; i<playlist.length; i++) {
		var title = playlist[i].title.replace(/amp;/g,'');
		
		if (playlist[i].url != "") {
			content += '<div class="tourVideoChoice" url="'+playlist[i].url+'"><a href="javascript:tourOpen(\''+playlist[i].url+'\');">'+title+' <img src="/.element/img/3.0/sect/r/video_sm_icon.gif" /></a></div>'+"\n";
		}
	}	

	$('video_list').innerHTML = content;
}

//setup the hp playlist to play
function cvp_playerReadyHP() {	
	var len = playlist.length;
	var vId;
	
	for(var i=0; i<=len-1; i++) {
		vId = cvp_prepareVideoId(playlist[i].url);
		
		if(i==0) {
			TURNERPLAYER.play(vId, {prerollURL:playlist[i].preroll});
		} else {
			TURNERPLAYER.queue(vId, {disablePreroll:true});	//we only want to play a preroll for the first video
		}
	}
	
	cvp_populateVideoList();	//update the video list in the page
	
	TURNERPLAYER.mute();	//default the player to muted
}

//setup a playlist to play, not on the homepage.  see cvp_playerReadyHP for that
function cvp_playerReady() {
	var len = playlist.length;
	var currId = cvp_prepareVideoId(playlist[0].url);	//vId of the first video to play
	var vId;
	
	TURNERPLAYER.play(currId);	//play first video
	
	for(var i=0; i<=len-1; i++) {
		vId = cvp_prepareVideoId(playlist[i].url);
		
		if(vId.indexOf(currId) == -1) {	//we don't want to queue the currently playing video
			TURNERPLAYER.queue(vId);
		}
	}
}

//make sure the vId is in a format that we expect
function cvp_prepareVideoId(vId) {
	if(/\/video.json$/.test(vId)) {	//remove /video.json from end
		vId = vId.replace(/\/video.json$/, "");
	}
	
	if(/\/$/.test(vId)) {	//remove trailing slash
		vId = vId.replace(/\/$/, "");
	}
	return vId;
}

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


//play a video with vId
function cvp_playVideo(vId) {
	TURNERPLAYER.play(cvp_prepareVideoId(vId));
}

//enqueue videos from a playlist
function cvp_enqueueVideos(playlist, currId) {
	var len = playlist.length;
	var queueId;
	
	for(var i=0; i<=len-1; i++) {
		queueId = cvp_prepareVideoId(playlist[i].url);
		
		if(queueId.indexOf(currId) == -1) {	//we don't want to queue the currently playing video
			TURNERPLAYER.queue(queueId);
		}
	}
}

//change the class for the currently playing video
function cvp_highlightVideoInList(vId) {
	var divList = $('video_list').getElementsByTagName('div');
	var len = divList.length;
	for(var i=0; i<=len-1; i++) {
		if(divList[i].getAttribute('url').indexOf(vId) != -1) {
			divList[i].className = 'tourVideoChoice vidselected';
		} else {
			divList[i].className = 'tourVideoChoice';
		}
	}
}

//reset the ad frequency count to zero
function cvp_resetAdFrequency() {
	TURNERPLAYER.resetAdFrequency();
}

//when in a channel, follow 3:1 rule for all videos, including 100% sponsored
function cvp_channelOverrideAlwaysSponsored(playlist, currId) {
	var len = playlist.length;
	var queueId;
	
	cvp_playVideo(currId);	//play the first video
	
	for(var i=1; i<=len-1; i++) {	//loop through second to end videos
		queueId = cvp_prepareVideoId(playlist[i].url);
		
		if(isChannel && (i%3 != 0)) {	//if the iterator isn't divisible by three...
			TURNERPLAYER.queue(queueId, {disablePreroll:true});	//override 100% sponsored rule
		} else {
			TURNERPLAYER.queue(queueId);	//otherwise, just queue as normal with 100% sponsored ad
		}
	}
	isChannel = false;
}