
	/*
	JS
	--------------------------------------------------------------------------------------------  
	@site			sho.com/series 
	@file			RelatedVideos.js
	@author			ncavanagh
	@modified		05.06.09
	@desc			Loads video lists from Brightcove API by tag
	@depend			prototype, jsr_class


	/* =:RelatedVideos
	-------------------------------------------------------------------------------------------- */  
	RelatedVideos = function()
	{	 
		var tags = '';
		var playerID = '14033849001';
		var numberOfVideos = 4; // default, overwritten by tile attribute
		var videoFeed;
		var token = escape("FE3OTYO94Tzdw42TKsz9IAXXqEf8ERJqPPPD22wq364.");
		var req = "http://api.brightcove.com/services/library?";
		req += "token=" + encodeURIComponent(token);
		req += "&sort_by=MODIFIED_DATE";
		
		var linkBase = 'http://www.sho.com/site/video/brightcove/series/title.do?bcpid=1772825635&bctid=';
	
		function init()
		{	
			if ($('related-videos')) { initVideoFeed(); }
		}
		
		function initVideoFeed()
		{	
			videoFeed = $('related-videos-feed');
			tags = $('tags').innerHTML;
			req += "&command=find_videos_by_tags&and_tags=" + tags;

			var getLimit = videoFeed.select('ul')[0]; 
			var limit = getLimit.readAttribute('id');  
			limit = limit.split("limit-")[1]; 
			numberOfVideos = limit;
			
			videoFeed.innerHTML = '';
			videoFeed.addClassName('loading');
			req += "&callback=RelatedVideos.responseFeed"; 
			bObj = new JSONscriptRequest(req); 
			bObj.buildScriptTag(); 
			bObj.addScriptTag(); 
		}	

		function responseFeed(jsonData) 
		{   
			var videos = jsonData["items"];
			videoFeed.removeClassName('loading');
			if (!videos) { videoFeed.innerHTML = 'None found.'; return; }
			if (videos.length < 1) { videoFeed.innerHTML = 'None found.'; return; }
			var titleList = new Element('ul', { 'id':'title-list' });
			var i=0; 
			while (i<videos.length && i<numberOfVideos) {  
				var str = "";
				str += '<li class="clearfix"><a rel="brightcove" href="#player:' + videos[i].id + '">';
				str += '<span class="video-thumb"><img src="' + videos[i].thumbnailURL + '"/></span></a>';
				str += '<h3 class="small"><a rel="brightcove" href="#player:' + videos[i].id + '">' + videos[i].name.truncate(41) + '</a></h3>';
				str += '<p>' + videos[i].shortDescription; //.truncate(80);
				str += '</p></li>';
				titleList.innerHTML += str;
				i++;
			}  
			$('related-videos-feed').appendChild(titleList);
			
			Series.OverlayVideo.init(iconPath,seriesName);
		}
		
		// time is stored in milliseconds; we need to convert to a mm:ss display format
		function formatTime(time) 
		{	
			var t_secs = Math.round(time/1000);
			var mins = Math.floor(t_secs/60);
			var secs = t_secs - (mins*60);
			return mins + ":" + secs;
		}	
		
	/* =:Reveal Public Methods
	  ---------------------------------------------------------------------------------------- */   
	return {
		init:init,
		responseFeed:responseFeed
	}
	
	}();
	
	// Add to onload stack
	document.observe("dom:loaded", function() { 
		RelatedVideos.init();
	});	