
	var BC_TKN = '01pbc-c2zfoyxQJbVGZiQYX6nJsLAu83vA-ynPiEWKA.';

	/* Player Interface */
	/*
	var vcPlayer;
	var vcContent;
	var vcVideo;  
	
	var _brightcove_initialized = false;
	
	// called when template loads, we use this to store a reference to the player and modules
	// and add event listeners for the video load (when the user clicks on a video)
	function onTemplateLoaded(player) {
		vcPlayer = brightcove.getExperience("sptVideoPlayer");
		vcVideo = vcPlayer.getModule(APIModules.VIDEO_PLAYER);
		vcContent = vcPlayer.getModule(APIModules.CONTENT);
		_brightcove_initialized = true;
	}
  
	// handles click event from list items 
	function playTitleFromList(id) {
		alert("CALLED: playTitleFromList(" + id + ");");	
		if (!_brightcove_initialized) {
			alert("Brightcove isn't initialized - waiting 1/4 second");
			setTimeout("playTitleFromList(" + id + ")", 250);			
		} else {		
			alert("Brightcove is initialized.  Loading video.");
			vcContent.getVideoAsynch(id);
		}
	}
	*/

var _brightcove_initialized = false;	
var _play_video_automatically = true;

var bcExp;
var modVP;
var modExp;
var modContent;

// called when template loads, this function stores a reference to the player and modules.
// Then event listeners will be added for when the template is ready and when a user 
// clicks on a video.
function onTemplateLoaded(experienceID) {
    bcExp = brightcove.getExperience(experienceID);
     
    modVP = bcExp.getModule(APIModules.VIDEO_PLAYER);
    modExp = bcExp.getModule(APIModules.EXPERIENCE);
    modContent = bcExp.getModule(APIModules.CONTENT);
  
    modExp.addEventListener(BCExperienceEvent.TEMPLATE_READY, onTemplateReady);
    modExp.addEventListener(BCExperienceEvent.CONTENT_LOAD, onContentLoad);
    modContent.addEventListener(BCContentEvent.VIDEO_LOAD, onVideoLoad);  
}

function onTemplateReady(evt) {
    //alert("EVENT: TEMPLATE_READY"); 
    _brightcove_initialized = true;
}
function onContentLoad(evt) {
	//modContent.getVideoAsynch(1898343320);
    //alert("EVENT: CONTENT_LOAD");
}
function onVideoLoad(evt) {  
    // Play video that was just loaded
    if (_play_video_automatically) {
	    modVP.loadVideo(evt.video.id);
    } else {
    	modVP.cueVideo(evt.video.id);
    }
}

function playVideo(vid) {
	if (!_brightcove_initialized) {
		setTimeout("playVideo(" + vid + ")", 250);			
	} else {		
		modContent.getVideoAsynch(vid);
	}
}	

function loadPlaylistIntoPlayer(pid) {
	if (!_brightcove_initialized) {
		setTimeout("loadPlaylistIntoPlayer(" + pid + ")", 250);
	} else {
		modContent.getPlaylistAsynch(pid);
	}
}
	
	
	
function vcInitCall() {
	/*
	// Create a new request object
	bObj = new JSONscriptRequest(vcReq); 
	// Build the dynamic script tag
	bObj.buildScriptTag(); 
	// Add the script tag to the page
	bObj.addScriptTag();
	*/
}

function sendRequest(cmd, callback) {
	var _request = 'http://api.brightcove.com/services/library?' + cmd + '&token=' + getEncodedToken() + '&callback=' + callback
	jsonObj = new JSONscriptRequest(_request);
	jsonObj.buildScriptTag();
	jsonObj.addScriptTag();	
}

var _currentVideoChannelResultsPage = 1;
var _VIDEOS_PER_PAGE = 15;

function loadPlaylistVideos(plId, pg) {
 	_currentPlaylistId = plId;
	_currentVideoChannelResultsPage = pg;
	document.getElementById('videoList').innerHTML = "<div class=\"playlistLoader\">Loading<br /><img src=\"http://www.tampabay.com/universal/images/bc-loading-video.gif\" alt=\"\" border=\"\" /></div>";
	var cmd = 'command=find_playlist_by_id&playlist_id=' + plId
	sendRequest(cmd, 'handlePlaylistRequest'); 	
}

function loadDefaultVideoFromPlaylist(plId) {
	_currentPlaylistId = plId;
//	loadPlaylistIntoPlayer(plId);
	var cmd = 'command=find_playlist_by_id&playlist_id=' + plId;
	sendRequest(cmd, 'handleDefaultVideoRequest');
}

var _currentPlaylistId = '';
var _currentFeaturePlaylistId = '';

function loadPlaylistSidebarVideos(plId, pg) {
	_currentPlaylistId = plId;
	var cmd = 'command=find_playlist_by_id&playlist_id=' + plId;
	sendRequest(cmd, 'handleSidebarVideoRequest');
}

function loadFeaturedVideos(featuredPlaylistId) {
	_currentFeaturePlaylistId = featuredPlaylistId;
	var cmd = 'command=find_playlist_by_id&playlist_id=' + featuredPlaylistId;
	sendRequest(cmd, 'handleFeaturedVideosRequest');
}

function handleDefaultVideoRequest(jsonData) {
	_play_video_automatically = false;
	var videos = jsonData['videos'];
	playVideo(videos[0].id);
}

function handleFeaturedVideosRequest(jsonData) {
	var videos = jsonData['videos'];
	
	var s = '<div class="belt">';
	var videoLink = '';
	var videoCount = 1;
	
	var _videoParameterId = getRequestParameter('v');
	var _featuredVideoPosition = -1;
	
	for (var i = 0; i < videos.length; i++) {
		videoLink = '/video/?v=' + videos[i].id + '&p=' + _currentFeaturePlaylistId + '&n=' + videoCount;
		
		if (videos[i].id == _videoParameterId) {
			s += '<div class="panel featuredplaying">';
			_featuredVideoPosition = videoCount;		
		} else
			s += '<div class="panel">';
			
		s += '<a href="' + videoLink +'"><img src="' + videos[i].thumbnailURL + '" width="80" height="60" border="0" /><br /></a>';
		s += '<a href="' + videoLink +'" class="bcTitleLinkText">' + videos[i].name + '</a>';
		s += '</div>';
		videoCount++;
	}
	
	s += '</div>';
	
	$('#videoChannelRightContainer').prepend('<div class="featured-video-title">Featured Videos<p></p></div>');
	document.getElementById('featuredVideos').innerHTML = s;
	
	setupFeaturedSlider();	
	gotoFeaturedVideo(_featuredVideoPosition);
}

//this controls appearance of the slider on the video channel page
function setupFeaturedSlider() {
	stepcarousel.setup({
		galleryid: 'featuredVideos', //id of carousel DIV
		beltclass: 'belt', //class of inner "belt" DIV containing all the panel DIVs
		panelclass: 'panel', //class of panel DIVs each holding content
		autostep: {enable:false, moveby:1, pause:3000},
		panelbehavior: {speed:500, wraparound:true, persist:false},
		defaultbuttons: {enable: true, moveby: 1, leftnav: ['http://www.tampabay.com/universal/images/video-list-left.png', 0, 30], rightnav: ['http://www.tampabay.com/universal/images/video-list-right.png', -24, 30]},
		statusvars: ['BCstatusA', 'BCstatusB', 'BCstatusC'], //register 3 variables that contain current panel (start), current panel (last), and total panels
		contenttype: ['inline'] //content setting ['inline'] or ['external', 'path_to_external_file']
	});			
}

function gotoFeaturedVideo(_position) {
	if (_position > 0)
		stepcarousel.stepTo('featuredVideos', _position);
}

function handleSidebarVideoRequest(jsonData) {
	var videos = jsonData['videos'];
	
	document.getElementById('bcSidebarTitle').innerHTML = jsonData['name'];
	
	var s = '<div class="belt">';
	var videoLink = '';
	var videoCount = 1;
	for (var i = 0; i < videos.length; i++) {
		videoLink = '/video/?v=' + videos[i].id + '&p=' + _currentPlaylistId + '&n=' + videoCount;
	
		s += '<div class="panel">';
		s += '<a href="' + videoLink +'"><img src="' + videos[i].thumbnailURL + '" width="80" height="60" border="0" /><br /></a>';
		s += '<a href="' + videoLink +'" class="bcTitleLinkText">' + videos[i].name + '</a>';
		s += '</div>';
		videoCount++;
	}
	
	s += '</div>';
	
	document.getElementById('bcSidebarPlaylist').innerHTML = s;	
	
	setupVideoSlider();
	
}

//this controls appearance of the slider on the other channels, I think...
function setupVideoSlider() {
	stepcarousel.setup({
		galleryid: 'bcSidebarPlaylist', //id of carousel DIV
		beltclass: 'belt', //class of inner "belt" DIV containing all the panel DIVs
		panelclass: 'panel', //class of panel DIVs each holding content
		autostep: {enable:true, moveby:1, pause:3000},
		panelbehavior: {speed:500, wraparound:false, persist:true},
		defaultbuttons: {enable: true, moveby: 1, leftnav: ['http://www.tampabay.com/universal/images/video-list-left.png', 0, 30], rightnav: ['http://www.tampabay.com/universal/images/video-list-right.png', -24, 30]},
		statusvars: ['BCstatusA', 'BCstatusB', 'BCstatusC'], //register 3 variables that contain current panel (start), current panel (last), and total panels
		contenttype: ['inline'] //content setting ['inline'] or ['external', 'path_to_external_file']
	});			
}

var _initialPlaylistLoad = true;

function handlePlaylistRequest(jsonData) {
	var videos = jsonData["videos"];
	
	if (videos.length == 0) {
		document.getElementById('videoList').innerHTML = "<div class=\"videos-none\">No videos found!</div>";
	} else {
		var videoCount = 1;
		var resultList = '';
		
		var totalVideos = videos.length;
		var totalPages  = Math.floor(totalVideos / _VIDEOS_PER_PAGE);
		if (totalVideos % 15 > 0) totalPages++;
		
		if (_initialPlaylistLoad) {
			// if "n" is set as a parameter, we'll want to change the current page
			var _idx = getRequestParameter('n');
			if (_idx > 0) 
				_currentVideoChannelResultsPage = Math.floor(_idx / _VIDEOS_PER_PAGE);
			
			_initialPlaylistLoad = false;
		}
		
		var startAt = (_currentVideoChannelResultsPage * _VIDEOS_PER_PAGE);
		var thisPage = _currentVideoChannelResultsPage + 1;
		
		var _videoParameterId = getRequestParameter('v');
		
		var videoLink = '';
		
		resultList += '<div class="videoResultTitleBar">';
		resultList += '<div class="videoResultTitleBarText">' + jsonData['name'] + '</div>';
		resultList += '<div class="videoResultTitleBarPagination">';
		
		// previous page
		if (thisPage > 1) {
			var _previousPage = thisPage - 2;
			resultList += '<a href="javascript:loadPlaylistVideos(' + _currentPlaylistId + ',' + _previousPage + ');">Previous</a> | ';
		}
		
		resultList += thisPage + ' of ' + totalPages;
		
		// next page
		if (thisPage < totalPages) {
			var _nextPage = thisPage;
			resultList += ' | <a href="javascript:loadPlaylistVideos(' +  _currentPlaylistId + ',' + _nextPage + ');">Next</a>';
		}
		
		resultList += '</div><div class="clear"></div>';
		resultList += '</div><div class="clear"></div>';
		
		var videosDisplayed = 0;
		for (var i = startAt; i < videos.length; i++) {
			videoLink = '/video/?v=' + videos[i].id + '&p=' + _currentPlaylistId + '&n=' + (i + 1);
		
			resultList += '<div class="';
			if (videos[i].id == _videoParameterId)
				resultList += 'nowplaying ';
			resultList += 'videoElement">';
			resultList += '<a href="' + videoLink + '"><img width="120" height="90" src="' + videos[i].thumbnailURL + '" /></a><br />';
			resultList += '<a href="' + videoLink + '">' + videos[i].name + ' (' + vcFormatTime(videos[i].length) + ')</a>';
			resultList += '</div>';
			videosDisplayed++;
			
			if (videosDisplayed == _VIDEOS_PER_PAGE)
				break;
		}
		
		document.getElementById('videoList').innerHTML = '<div class="video-container-outer"><div class="video-container-inner">' + resultList + '<div class="clear"></div></div></div>';	
	}

}

function loadVideos(tags, pg) {
	var pageSize = 4;
	var getItemCount = true;
	var cmd = 'find_videos_by_tags';
	var loadVideosByTagRequest = 'http://api.brightcove.com/services/library?command=' + cmd + '&get_item_count=true&and_tags=' + tags + '&page_number=' + pg + '&pageSize=' + pageSize + '&token=' + getEncodedToken() + '&callback=handleVideoListResponse';

	lvObj = new JSONscriptRequest(loadVideosByTagRequest);
	lvObj.buildScriptTag();
	lvObj.addScriptTag();
}

function getEncodedToken() {
	return encodeURIComponent(BC_TKN);
}

function handleVideoListResponse(jsonData) {
	var items = jsonData["items"];
	
	if (items.length == 0) {
		document.getElementById('videoList').innerHTML = '<div class="videos-none">No videos found!</div>';
	} else {		
		var resultList = '<ul class="videoResultsUL">';
		for (var i = 0; i < items.length; i++) {
			resultList += '<li>';
			resultList += '<div class="videoResultContainer" onClick="playVideo(' + items[i].id + ');">';
			resultList += '<div class="videoResultThumb"><img src="' + items[i].thumbnailURL + '" /></div>';
			resultList += '<p class="videoResultDisplayName">' + items[i].name + ' (' + vcFormatTime(items[i].length) + ')</p>';
			resultList += '<p class="videoResultDesc">' + items[i].shortDescription + '</p>';
			resultList += '</div>';
			resultList += '</li>';
		}
		resultList += "</ul>";
				
		document.getElementById('videoList').innerHTML = resultList;
	}
}

function vcFormatTime(time) {
	var t_secs = Math.round(time/1000);
	var mins = Math.floor(t_secs/60);
	var secs = t_secs - (mins*60);
	//return (secs < secs) ? mins + ":0" + secs : mins + ":" + secs;
	if (secs == 0) return mins + ':' + secs + "0";  
	else if (secs < 10) return mins + ':0' + secs; 
	else return mins + ':' + secs;
}	

function getRequestParameter(key) {
	key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  	var regexS = "[\\?&]"+key+"=([^&#]*)";
  	var regex = new RegExp( regexS );
  	var results = regex.exec( window.location.href );
  	if( results == null )
    	return "";
  	else
		return results[1];
}


