// Globals
// Major version of Flash required
var requiredMajorVersion = 9;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Minor version of Flash required
var requiredRevision = 124;
var tourCVPRR;
var isRefresh = false;
var rrPlaylist;
var plRefreshId;
var contentHeight = 0;
var oldHighlight = 'tourRRVideo0';

//populate div with video list
function populateVideoPlaylist(elId, pl) {
	var content = '';
	var len = pl.length;
	for (var i=0; i<len; i++) {
		var title = pl[i].title.replace(/amp;/g,'');
		
		if (pl[i].url != "") {
			content += '<div id="tourRRVideo'+i+'" class="tourRRVideoItem" url="'+pl[i].url+'" onclick="cvp_highlightVideoInList('+i+'); tourCVPRR.play(\''+pl[i].url+'\');" title="'+title+'"><span>'+title.truncate(41)+'</span></div>'+"\n";
		}
	}
	document.getElementById(elId).innerHTML = content;
	getContentHeight();
}

//change the class for the currently playing video
function cvp_highlightVideoInList(vIndex) {
	document.getElementById(oldHighlight).className = 'tourRRVideoItem';
	document.getElementById('tourRRVideo'+vIndex).className = 'tourRRVideoItem tourRRVideoSelected';
	
	oldHighlight = 'tourRRVideo'+vIndex;
	
	updateHeadlineAndDescription(vIndex);	//update the headline/description
}

//update the headline and description
function updateHeadlineAndDescription(vIndex) {
	document.getElementById('tourRRVideoHeadline').innerHTML = rrPlaylist[vIndex].title;
	document.getElementById('tourRRVideoDesc').innerHTML = rrPlaylist[vIndex].description;
}

//ajax call to get the latest playlist file
function fetchPlaylist() {
	var plFile = '/.element/ssi/auto/3.0/aps/sect/video/tournament/'+tId+'/leaderboard/assets_max_15.json';	//tId is a global set in the lb's index.html
	
	new Ajax.Request(plFile, {
			method:'get',
			onFailure: function(oXHR, oJson) {	//mainly to catch playlist json file 404s
				document.getElementById('tourRRVideoListContent').innerHTML = '<div id="tourRRVideoErrorMsg">Video playlist currently unavailable</div>';
				getTimeNow();	//output last updated time
				//if we wanted to hide the video player, we could use the below line
				//document.getElementById('tourRRVideoContainer').setStyle({display: 'none'});
			},
			onSuccess: function(transport) {
				rrPlaylist = transport.responseText.evalJSON(true);

				populateVideoPlaylist('tourRRVideoListContent', rrPlaylist);
				cvp_highlightVideoInList('0');	//highlight first video in list
				
				document.getElementById('tourRRVideoHeadline').innerHTML = rrPlaylist[0].title;
				document.getElementById('tourRRVideoDesc').innerHTML = rrPlaylist[0].description;
				
				getTimeNow();	//output last updated time
				
				if(!isRefresh) {	//only on initial page load
					tourCVPRR.play(rrPlaylist[0].url);
				}
			}
	});
}

//retrieve the client time and do some 12-hour logic
function getTimeNow() {
	var now = new Date();
	var hours = now.getHours();
	var minutes = now.getMinutes();
	var date = now.getDate();
	var month = now.getMonth();
	var year = now.getFullYear();
	var ampm = 'am';
	var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
	
	if(hours > 11) {
		ampm = 'pm';
	}
	
	if(hours > 12) {
		hours = hours-12;
		
	} else if (hours == 0) {
		hours = 12;
	}
	
	if(minutes < 10) {
		minutes = '0'+minutes;
	}
	document.getElementById('tourRRVideoLastUpdate').innerHTML = 'Updated: '+monthNames[month]+' '+date+', '+year+'&nbsp;'+hours+':'+minutes+'&nbsp;'+ampm;
}

//remove the passed in element from the DOM
function removeEl(id) {
	var el = document.getElementById(id);
	el.parentNode.removeChild(el);
}

//things to do on playlist refresh
function refreshPlaylist() {
	isRefresh = true;
	removeEl('cvpRR');
	
	var container = document.getElementById('tourCVPPlaceholder');
	var newDiv = document.createElement('div');
	newDiv.id = 'cvpRRDiv';
	container.appendChild(newDiv);
	tourCVPRR.embedSWF('cvpRRDiv');
	
	fetchPlaylist();
}

//scroll up
function moveUp() {
	var speed = 3;
	var vListContainer = document.getElementById('tourRRVideoListContent');
	
	if (parseInt(vListContainer.style.top) <= 0) {
		vListContainer.style.top = parseInt(vListContainer.style.top)+speed+"px";
	}
	moveUpVar=setTimeout("moveUp()", 20);
}

//scroll down
function moveDown() {
	var speed = 3;
	var vListContainer = document.getElementById('tourRRVideoListContent');
	
	if(parseInt(vListContainer.style.top) >= (contentHeight*(-1)+210)) {
		vListContainer.style.top = parseInt(vListContainer.style.top)-speed+"px";
	}
	moveDownVar=setTimeout("moveDown()", 20);
}

//get the height of the playlist div
function getContentHeight() {
	contentHeight = document.getElementById('tourRRVideoListContent').offsetHeight;
}

cvpFlashVars.player = 'inline'+cvpPlayerTest;

//after page load, embed the video player
Event.observe(window, 'load', function() {
	tourCVPRR = new CVP({
		id: 'cvpRR',
		width: '288',
		height: '162',
		flashVars: cvpFlashVars,
		
		embed : {
			containerSwf : cvpContainerSwf
		},
		
		onPlayerReady: function() {
			if(!isRefresh) {	//on page load
				fetchPlaylist();
			} else {	//playlist auto-refresh
				this.play(rrPlaylist[0].url);
			}
			
			this.setAdSection('pgatour.com_embed_rightrail');
		},
		
		onContentPlay: function() {
		/*
			clearInterval(plRefreshId);	//when a video starts, cancel the playlist refresh
			plRefreshId = setInterval(refreshPlaylist, 600000);	//then reset the refresh
		*/
		}
	});
	tourCVPRR.embedSWF('cvpRRDiv');
});

