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

//define player config
tourCVPMain = new CVP({
	id: 'cvpMain',
	width: '640',
	height: '360',
	flashVars: cvpFlashVars,
	
	embed : {
		containerSwf : cvpContainerSwf,
		options: {
			wmode : 'transparent'
		}
	},
	
	onPlayerReady: function() {
		var siteSection = 'wgc.com_';
		var location = window.location.href;	//get url so we can tell which wgc event we're in
		
		if(location.indexOf('r470') != -1) {
			siteSection += 'matchplaychamp';
		} else if(location.indexOf('r473') != -1) {
			siteSection += 'cachamp';
		} else if(location.indexOf('r476') != -1) {
			siteSection += 'bridgestoneinvitational';
		} else if(location.indexOf('r489') != -1) {
			siteSection += 'hsbcchamp';
		}
		
		this.setAdSection(siteSection);
		cvp_initWGCPage();
	},
	
	onContentPlay: function() {
		cvp_updateVideoHeadlineDescription(this.getContentId());
	},
	
	onAdStarted: function(token) {
		$('tourVidDetailBlurb').innerHTML = '';	//clear title and description during ad playback
		$('tourVidDetailURL').innerHTML = '';

		isAdPlaying = true;
	},
	
	onAdFinished: function(token) {
		isAdPlaying = false;
	},
	
	onContentTrackingAdCountdown: function(secs) {
		var secondWord = (secs == "1")?"second":"seconds";
		
		$('tourVidDetailBlurb').innerHTML = '<h4>Next video will start in  ' + secs + ' ' + secondWord + '</h4>';
	}
});

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

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

// Video Play function
function loadVideo(video, override) {
	if(typeof video == 'string') {	//if video is a string, it is a single video
		cvp_playVideo(video);
	} else {	//else it's a playlist of videos size 1 to n
		var len = video.length;
		if(len == 0) {	//no playlist, just return
			return;
		}
		
		var currId = video[0].url;
		
		cvp_playVideo(currId);
		if(len > 1) {
			cvp_enqueueVideos(video, currId);
		}
	}
}

function cvp_initWGCPage() {
	var q = '';
	
	// Process what is being called
	var regex = new RegExp( "[?](.*)$" );
	var results = regex.exec( window.location.href );
	
	if (results){
		if (results.length == 2) q = results[1];
	}
	
	if(q=='') {
		loadVideo(playlist);
		return;
	} else if (q.match(/[a-z]+\/[0-9][0-9][0-9][0-9]\/[0-9][0-9]\/[0-9][0-9]/)) {	// q is a video
		if (!q.match(/^\/video\/video/) && !q.match(/^\/video\/audio/)) {
			q = '/video'+q;
		}
		loadVideo(q);
		return;
	}
}

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

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