// JavaScript Document


var player = null; 

/* Provides a reference to the player, so that scripts can access the player's API */
/* Called automatically by JW Player when it's fully loaded */
function playerReady(obj) { 
	player = document.getElementById('videoplayer'); 

	
	/* Ensures a tab is selected at page load. NOTE: startUrl & startTab are global vars expected to be defined on calling page
	startUrl = URL of playlist file
	startTab = CSS id of the tab to be highlighted above the video player
	*/
	if (typeof(startUrl) != 'undefined' && typeof(startTab) != 'undefined') {
		videoclick(startUrl, startTab);
	} else {
		videoclick(newsurl, 'videotab-news');
	}
	/* make the video autoplay after every playlist load */
	/* player.addControllerListener('PLAYLIST', 'autoPlayAfterLoad'); */
}

/* 
Handles the pressing of the video tabs by highlighting the tab and loading corresponding playlist 
playlisturl - url of the videoplaylist
tabid - CSS id of the tab that will be highlighted (by changing its background image)
*/
function videoclick(playlisturl, tabid){
	/* grab all link elements inside the videotab div and change their bkgrd image to unselected tab */	
	var thevideotabs = document.getElementById('videotab').getElementsByTagName("a");
	for (i=0; i < thevideotabs.length; i++) {
		thevideotabs[i].style.backgroundImage = "url(/images/videoplayer/videotab_gen.jpg)";
	}
	/* now highlight the tab that was pressed */
	document.getElementById(tabid).style.backgroundImage = "url(/images/videoplayer/videotab_curr.jpg)";
	/* load the playlist corresponding to the pressed tab */
	player.sendEvent('LOAD', playlisturl);
}

/* 
This fxn should be hooked up as a Listener to player (via JW player API fxn for hooking up listeners) to make it autoplay after 
loading new playlist. (We perform the hook up in playerReady())
If we had instead put a play statement in playerReady after the load stmt, it would not work since playlist loading not finished by time play is called.
We could put a timeout infront of play, but that's hacky.  This is proper way.
*/
function autoPlayAfterLoad(obj) {
	// player.sendEvent('PLAY');
	player.sendEvent('ITEM', '0'); // play first item in playlist
}


/* Loads a playlist, then moves whole page to the location of the videoplayer.  
Useful for links that start a video on same page as player but in totally diff area of page.
NOTE: Requires videoclick() function. First 2 params are passed directly to videoclick().
pageanchor - anchor tag of the videoplayer
see videoclick() for desc of other params 
*/
function gotoandrunvideo(playlisturl,tabid,pageanchor) {
	videoclick(playlisturl, tabid);
	window.location.hash = pageanchor;
}
