function txtReplace() { 
  FLIR.init(new FLIRStyle({ mode:'wrap' }));
  $('#video-right h3').each( function() { FLIR.replace(this); } );
  $('.overlay a').each( function() { FLIR.replace(this); });
}

function loadVideos() {
  $.getJSON("/json-video",
	function(feed) {
	  var videos = feed.items;
	  var cat = $('#video-right-header a.selected span').text();
		  
	  if (cat == './shutdown') {
	    videos = $.map(videos, function(n){
		  if(n.category == 'shutdown-Extras' || n.category == 'shutdown-tv' )
		  return n;
	    });
	  } else {  
	  videos = $.map(videos, function(n){
	    if(n.category == 'Boomers-Extras' || n.category == 'Boomers')
	    return n;
	  });
    };
    writeVideos(videos);
  });
}

function writeVideos(a) {
  var videos = a;
  for(i=0; i < 3; i++)
  {
    var video = '',
	    image = videos[i].image,
	    description = videos[i].description;
	
	if(i == 0)
	  video += '<div class="video" id="0">'
	else
	  video += '<div class="video">'
	
	if(image != null)
		video += '<a href="' + videos[i].link + '">' + image + '</a>';
	else
		video += '<a href="' + videos[i].link + '"><img src="/images/error_episode-thumbnail.jpg" alt="" /></a>';
	
	video += '<h3><a href="' + videos[i].link + '">' + videos[i].title.replace('Episode', 'Ep.') + '</a></h3>';
	video += description.slice(0, 100) + '... [<a href="' + videos[i].link + '">more</a>]';
	video += '</div>'
		
	if(i == 0)
		$('#video-right').html(video);						//if loading first new video erase others
	else
		$('#video-right').append(video);					//if current record not first video append to container
		
	if(i == 0)
	  $('#video-right-more a.prev').hide();					//if end of entries hide previoust links
	  
	if(i == videos.length-1)
      $('#video-right-more a.next').hide();					//if end of entries hide next links
	else
	  $('#video-right-more a.next').show();					//if not end of entries show next links
	  
	txtReplace();
  };
  
};

function previousVideos(a) { 
  var videos = a;
  var videoIndex = parseInt($('.video:first').attr('id'));
  var j = videoIndex-3;										//load content starting after previous entries
  
  if(j == 0 || j < 0) j = 0;
	
  var k = j+3;												//load content up to 3 entries
	
  for(i=j; i < k; i++)
  {
	var video = '';
	var image = videos[i].image;
	var description = videos[i].description;
	
	if(i == j )
	  video += '<div class="video" id="' + j + '">'
	else
	  video += '<div class="video">'
	  
	if(image != '')
		video += '<a href="' + videos[i].link + '">' + image + '</a>';
	else
		video += '<a href="' + videos[i].link + '"><img src="/images/error_episode-thumbnail.jpg" alt="" /></a>';
		
	video += '<h3><a href="' + videos[i].link + '">' + videos[i].title.replace('Episode', 'Ep.') + '</a></h3>';
	video += description.slice(0, 100) + '... [<a href="' + videos[i].link + '">more</a>]';
	video += '</div>'
	
	if(i == j)
		$('#video-right').html(video);						//if loading first new video erase others
	else
		$('#video-right').append(video);					//if current record not first video append to container
	
	if(i == 0)
	  $('#video-right-more a.prev').hide();					//if end of entries hide next links
	  
	txtReplace();
  };	
};

function nextVideos(a) {  
  var videos = a;
  var videoIndex = parseInt($('.video:first').attr('id'));
  var j = videoIndex+3;										//load content starting after previous entries
  var k = j+3;												//load content up to 3 entries
		  
  for(i=j; i < k; i++)
  {
    var video = '';
	var image = videos[i].image;
	var description = videos[i].description;
				
    if(i == j )
	  video += '<div class="video" id="' + j + '">'
	else
      video += '<div class="video">'
	
	if(image != '')
		video += '<a href="' + videos[i].link + '">' + image + '</a>';
	else
		video += '<a href="' + videos[i].link + '"><img src="/images/error_episode-thumbnail.jpg" alt="" /></a>';
		
    video += '<h3><a href="' + videos[i].link + '">' + videos[i].title.replace('Episode', 'Ep.') + '</a></h3>';
    video += description.slice(0, 100) + '... [<a href="' + videos[i].link + '">more</a>]';
    video += '</div>'
				
    if(i == j) 
      $('#video-right').html(video);						//if loading first new video erase others
	else
      $('#video-right').append(video);		   				//if current record not first video append to container
				
    if(i == videos.length-1)
      $('#video-right-more a.next').hide();	
	  txtReplace();	//if end of entries hide next links
    };	
	txtReplace();
};

function replacePlayer(p)
{
	var player = p;
	
	$.getJSON("/json-video",
			function(feed) 
			{
	  			var video = feed.items;
				var url = window.location.href;
				var id = (url == 'http://kill9.skimbee.com/videos' || url == 'http://kill9.skimbee.com/videos/' || url == 'http://www.kill9studios.com/videos' || url == 'http://www.kill9studios.com/videos/') ? 0 : '';
				var veoh = '';
				var divx = '';
				var youtube = '';
				
				for(i=0; i < video.length; i++)
				{
					if(url == video[i].link)
					{
						id = i;
						break;
					}
				}
				
				//Veoh video player HTML code
				veoh += '<object type="application/x-shockwave-flash" data="http://www.veoh.com/videodetails2.swf?permalinkId=' + video[id].players[0].veoh + '&id=anonymous&player=videodetailsembedded&videoAutoPlay=0" width="540" height="438">';
				veoh += '<param name="movie" value="http://www.veoh.com/videodetails2.swf?permalinkId=' + video[id].players[0].veoh + '&id=anonymous&player=videodetailsembedded&videoAutoPlay=0"></param>';
				veoh += '<param name="allowFullScreen" value="true"></param>';
				veoh += '<param name="wmode" value="transparent"></param>';
				veoh += '</object>';
				
				//YouTube video player HTML code
				youtube += '<div align="center">';
				youtube += '<object width="540" height="329" data="http://www.youtube.com/v/' + video[id].players[0].youtube + '&ap=%2526fmt%3D18">';
				youtube += '<param name="movie" value="http://www.youtube.com/v/' + video[id].players[0].youtube + '&ap=%2526fmt%3D18"></param>';
				youtube += '<param name="allowFullScreen" value="true"></param>';
				youtube += '<param name="wmode" value="transparent"></param>';
				youtube += '<embed src="http://www.youtube.com/v/' + video[id].players[0].youtube + '&ap=%2526fmt%3D18" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="540" height="329"></embed>';
				youtube += '</object>';
				youtube += '</div>';
									
				divx += '<embed src="http://www.veoh.com/videodetails2.swf?permalinkId=' + video[id].players[0].divx + '&id=anonymous&player=videodetailsembedded&videoAutoPlay=0" allowFullScreen="true" width="540" height="337" bgcolor="#000000" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>';
									
				if(player == 'veoh')
					$('#player').html(veoh);
				
				if (player == 'youtube')
					$('#player').html(youtube);
				
				if(player == 'divx')
					$('#player').html(divx);
		});		
}

$(document).ready(function(){
	//change video-right header 0 to selected					   
	$('#video-right-header #shutdown').css('background', 'url(/images/buttons/button_shutdown-on.png) no-repeat center');
	
	//ie6 fixes
	if($.browser.msie && $.browser.version == '6.0') $('.overlay ul li:eq(0)').css('border', 'none');
	
	//determine which video player to use
	$('.overlay a').click(function() 
	{ 
		var player = $(this).attr('id');
		
		$('.overlay').fadeOut('slow');
		replacePlayer(player);
				
		return false;
	});
						   
	//hide video prev link
	$('#video-right-more a.prev').hide();
	
	//switch between episodes and production videos
	$('#video-right-header a').click(function()
	{
		$('#video-right-header #shutdown').css('background', 'url(/images/buttons/button_shutdown.png) no-repeat center');
		$('#video-right-header #boomers').css('background', 'url(/images/buttons/button_boomers.png) no-repeat center');
		
		$('#video-right-header a').removeClass('selected');
  		$(this).addClass('selected');
		
		($(this).attr('id') == 'shutdown')? $(this).css('background', 'url(/images/buttons/button_shutdown-on.png) no-repeat center') : $(this).css('background', 'url(/images/buttons/button_boomers-on.png) no-repeat center');
		
		loadVideos();
		return false;
	});
	
	//activate 'more videos' link
	$('#video-right-more a.next').click(function()
	{
		var videoHeight = $('#video-right').height();		//capture height of video container
		$('#video-right').css('min-height', videoHeight);  	//apply min-height css to container 
		
		$.getJSON("/json-video",
			function(feed) {
			  var videos = feed.items;
			  var cat = $('#video-right-header a.selected').text();
		  
				if (cat == './shutdown') {
				  videos = $.map(videos, function(n){
				   if(n.category == 'shutdown-Extras' || n.category == 'shutdown-tv' )
					 return n;
				  });
				} else {  
				  videos = $.map(videos, function(n){
				   if(n.category == 'Boomers-Extras' || n.category == 'Boomers')
					 return n;
				  });
				};
				nextVideos(videos);
		   }
	   	);
		$('#video-right').css('min-height', 0);  			//remove min-height restriction on video container
		
		if(!$('#video-right-more a.prev').is(':visible'))	//if previous link not visible then show
			$('#video-right-more a.prev').show();
	
		return false;
	});
	
	//activate 'previous videos' link
	$('#video-right-more a.prev').click(function()
	{
		var videoHeight = $('#video-right').height();		//capture height of video container
		$('#video-right').css('min-height', videoHeight);  	//apply min-height css to container
		
		if(!$('#video-right-more a.next').is(':visible'))	//if more link not visible then show
			$('#video-right-more a.next').show();
		
		$.getJSON("/json-video",
		  function(feed) {
			var videos = feed.items;
			var cat = $('#video-right-header a.selected').text();
		  
			if (cat == './shutdown') {
			  videos = $.map(videos, function(n){
			   if(n.category == 'shutdown-Extras' || n.category == 'shutdown-tv' )
				 return n;
			  });
			} else {  
			  videos = $.map(videos, function(n){
			   if(n.category == 'Boomers-Extras' || n.category == 'Boomers')
				 return n;
			  });
			};
			previousVideos(videos);
		   }
	   	);
		if(!$('#video-right-more a.prev').is(':visible'))	
			$('#video-right-more a.prev').show();			//if previous link not visible then show
			
		$('#video-right').css('min-height', 0);  			//remove min-height restriction on video container
		return false;
	});
	
	//load initial video content into episode list container
	loadVideos();
	
	//fix height and width on player images
	$('#player img:first').attr('width','540').attr('height','336');
});