/** should be used wherever the "main" player instance is needed **/
cvpFlashVars.player = 'main';

//define player config
tourCVPMain = new CVP({
	id: 'cvpMain',
	width: '640',
	height: '360',
	flashVars: cvpFlashVars,
	
	embed : {
		containerSwf : cvpContainerSwf
	},

	onPlayerReady: function() {
		if(tourCVPPage == 'video') {
			initialize_video_page();
		} else {
			cvp_playerReady();
		}
	},

	onContentPlay: function() {
		//cvp_getRelatedUrl(this.getContentId());
		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){};
		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;
	},

	onContentTrackingAdCountdown: function(secs) {
		var secondWord = (secs == "1")?"second":"seconds";
		$('video_headline').innerHTML = '';
		$('video_description').innerHTML = '';
		//$('tourVidDetailBlurb').innerHTML = '<h4>Next video will start in  ' + secs + ' ' + secondWord + '</h4>';
	},
	onAdPrerollUrl: function(videoId, adUrl) {
		var videoSkinDiv = $('tourFullTop');	//set skin back to default
		var videoSkinClick = $('tourVideoAd');
		
		document.getElementById('video_headline').innerHTML = '';
		document.getElementById('video_description').innerHTML = '';
		
		if(videoSkinDiv != null) {
			videoSkinDiv.style.backgroundImage = 'url(http://i.cdn.turner.com/pgatour/.element/img/5.0/sect/video/skin_bkg_generic_v1.gif)';
			videoSkinDiv.style.height = '596px';
		}
		
		if(videoSkinClick != null) {
			videoSkinClick.innerHTML = '';
		}
	}
});

//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(/\/$/, "");
	}
	
	if(/&adTest=1$/.test(vId)) {	//remove &adTest=1 from end
		vId = vId.replace(/&adTest=1$/, "");
	}
	
	return vId;
}

//update video source or related link
function cvp_getRelatedUrl(vId) {
	var rDiv = document.getElementById('tourCVPRelatedLink');
	rDiv.innerHTML = '';	//clear div contents, for replays
	
	var jsonText = tourCVPMain.getContentEntry(vId);
	var videoObj = jsonText.evalJSON();	//create a video object
	
	if(videoObj && videoObj.urls.relateds != '') {
		var relatedP = document.createElement('p');
		rDiv.appendChild(relatedP);
		relatedP.id = 'tourRelatedUrl';
		
		var relatedA = document.createElement('a');
		relatedP.appendChild(relatedA);
		
		if(videoObj.urls.relateds.length == null) {	//only one related item
			relatedA.setAttribute('href', videoObj.urls.relateds.story.url.html);
			relatedA.appendChild(document.createTextNode(videoObj.urls.relateds.story.headline));
		} else {	//2 or more related items
			relatedA.setAttribute('href', videoObj.urls.relateds[0].story.url.html);	//we will assume that the first related url is always what we are looking for
			relatedA.appendChild(document.createTextNode(videoObj.urls.relateds[0].story.headline));
		}
	} else {
		//something happened
	}
}

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

function cvp_updateVideoHeadlineDescription(vId) {
	var vidDiv = document.getElementById('tourVidDetailBlurb');
	//var vidDescDiv = document.getElementById('tourCVPDescription');
	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('div');
	vidDiv.appendChild(descript);

	headline.id = "video_headline";
	descript.id = "video_description";
	
	//document.getElementById('video_description').style.height = '50px';

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

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

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

