/** tournament page cvp **/
cvpFlashVars.player = 'main'+cvpPlayerTest;

//define player config
tourCVPMain = new CVP({
	id: 'cvpMain',
	width: '640',
	height: '360',
	flashVars: cvpFlashVars,
	
	embed : {
		containerSwf : cvpContainerSwf
	},
	
	onPlayerReady: function() {
		var siteSection = 'pgatour.com_';
		
		if(tourn_id.indexOf('r') != -1) {	//pgatour
			siteSection += 'pgatour_tournaments_'+tourn_id;
		} else if(tourn_id.indexOf('h') != -1) {	//nationwide
			siteSection += 'nationwide_tournaments_'+tourn_id;
		} else if(tourn_id.indexOf('s') != -1) {	//champions
			siteSection += 'champions_tournaments_'+tourn_id;
		} else {	//default
			siteSection += 'pgatour_tournaments';
		}
		
		this.setAdSection(siteSection);
		cvp_playerReady();
	},
	
	onContentPlay: function() {
		cvp_updateVideoHeadlineDescription(this.getContentId());
		/* omniture video start */
		try {
			var vdata = tourCVPMain.getContentEntry(tourCVPMain.getContentId());
			var currVidObj = _w.JSON.parse(vdata);
			trackMetrics({
				type: "video-start",
				data: {
					video : {
						title: currVidObj.headline,
						id: currVidObj.id
					}
				}
			});
		} catch(e){}
	},
	
	onAdStarted: function(token) {
		/* omniture ad pre-roll */
		try {
			var vdata = tourCVPMain.getContentEntry(tourCVPMain.getContentId());
			var currVidObj = _w.JSON.parse(vdata);
			trackMetrics({
				type: "video-preroll",
				data: {
					video : {
						title: currVidObj.headline,
						id: currVidObj.id
					}
				}
			});
		} catch(e){};
		$('tourVidDetailBlurb').innerHTML = '';	//clear title and description during ad playback
		$('tourVidDetailURL').innerHTML = '';

		isAdPlaying = true;
	},
	
	onContentCompleted : function() { 
		/* omniture video complete */
		try {
			var vdata = tourCVPMain.getContentEntry(tourCVPMain.getContentId());
			var currVidObj = _w.JSON.parse(vdata);
			trackMetrics({
				type: "video-complete",
				data: {
					video : {
						title: currVidObj.headline,
						id: currVidObj.id
					}
				}
			});
		} catch(e){}
	},
	
	onContentPause : function(videoId, paused) {
				/* omniture video pause */
				 try {
					 trackMetrics({
 						type: "video-pause",
						 data: { }
					 });
				 } catch(e){}
	 },
	
	onAdFinished: function(token) {
		isAdPlaying = false;
	}
});

//setup a playlist to play
function cvp_playerReady() {
	var len = playlist.length;
	var currId = cvp_prepareVideoId(playlist[0].url);	//vId of the first video to play
	var vId;
	
	tourCVPMain.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
			tourCVPMain.queue(vId);
		}
	}
}

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;
}

function cvp_updateVideoHeadlineDescription(vId) {
	var vidDiv = document.getElementById('tourVidDetailBlurb');
	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 = tourCVPMain.getContentEntry(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);
		}
	}
}

//play a video with vId
function cvp_playVideo(vId) {
	tourCVPMain.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
			tourCVPMain.queue(queueId);
		}
	}
}

Event.observe(window, 'load', tourCVPMain.embedSWF('tourCVP'));

