function txtReplace() { 
  FLIR.init(new FLIRStyle({ mode:'wrap' }));
  $('#featured-right h3').each( function() { FLIR.replace(this); } );
}

function loadFeatured() {
  $.getJSON("/json-featured",
	function(feed) {
	  var feat = feed.items;
	  var cat = $('#featured-right-header a.selected').text();
		  
	  if (cat == 'Artists') {
	   feat = $.map(feat, function(n){
	   if(n.category == "Artists" )
		 return n;
	   });
	  } else if (cat == 'Companies') {  
		feat = $.map(feat, function(n){
		if(n.category == "Companies")
		 return n;
		});
	  } else {  
		feat = $.map(feat, function(n){
		if(n.category == "Guests")
		 return n;
		});
	  };
    writeFeatured(feat);
  });
}

function writeFeatured(a) {
  var feat = a;
  for(i=0; i < 3; i++)
  {
    var featured = '';
	var image = feat[i].image;
	var description = feat[i].description;
	
	if(i == 0)
	  featured += '<div class="feat" id="0">'
	else
	  featured += '<div class="feat">'
	
	if(image != '')
		featured += '<a href="' + feat[i].link + '">' + image + '</a>';
	else
		featured += '<a href="' + feat[i].link + '"><img src="/images/error_episode-thumbnail.jpg" alt="" /></a>';
	
	featured += '<h3><a href="' + feat[i].link + '">' + feat[i].title + '</a></h3>';
	featured += description.slice(0, 200) + '... [<a href="' + feat[i].link + '">more</a>]';
	featured += '</div>'
		
	if(i == 0)
		$('#featured-right').html(featured);						//if loading first new content erase others
	else
		$('#featured-right').append(featured);					//if current record not first content append to container
		
	if(i == 0)
	  $('#featured-right-more a.prev').hide();					//if end of entries hide previoust links
	  
	if(i == feat.length-1)
      $('#featured-right-more a.next').hide();					//if end of entries hide next links
	else
	  $('#featured-right-more a.next').show();					//if not end of entries show next links
	  
	txtReplace();
  };
  
};

function previousFeatured(a) { 
  var feat = a;
  var featuredIndex = parseInt($('.feat:first').attr('id'));
  var j = featuredIndex-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 featured = '';
	var image = feat[i].image;
	var description = feat[i].description;
	
	if(i == j )
	  featured += '<div class="feat" id="' + j + '">'
	else
	  featured += '<div class="feat">'
	  
	if(image != '')
		featured += '<a href="' + feat[i].link + '">' + image + '</a>';
	else
		featured += '<a href="' + feat[i].link + '"><img src="/images/error_episode-thumbnail.jpg" alt="" /></a>';
		
	featured += '<h3><a href="' + feat[i].link + '">' + feat[i].title + '</a></h3>';
	featured += description.slice(0, 200) + '... [<a href="' + feat[i].link + '">more</a>]';
	featured += '</div>'
	
	if(i == j)
		$('#featured-right').html(featured);						//if loading first new featured content erase others
	else
		$('#featured-right').append(featured);					//if current record not first featured content append to container
	
	if(i == 0)
	  $('#featured-right-more a.prev').hide();					//if end of entries hide next links
	  
	txtReplace();
  };	
};

function nextFeatured(a) {  
  var feat = a;
  var featuredIndex = parseInt($('.feat:first').attr('id'));
  var j = featuredIndex+3;										//load content starting after previous entries
  var k = j+3;												//load content up to 3 entries
		  
  for(i=j; i < k; i++)
  {
    var featured = '';
	var image = feat[i].image;
	var description = feat[i].description;
				
    if(i == j )
	  featured += '<div class="feat" id="' + j + '">'
	else
      featured += '<div class="feat">'
	
	if(image != '')
		featured += '<a href="' + feat[i].link + '">' + image + '</a>';
	else
		featured += '<a href="' + feat[i].link + '"><img src="/images/error_episode-thumbnail.jpg" alt="" /></a>';
		
    featured += '<h3><a href="' + feat[i].link + '">' + feat[i].title + '</a></h3>';
    featured += description.slice(0, 200) + '... [<a href="' + feat[i].link + '">more</a>]';
    featured += '</div>'
				
    if(i == j) 
      $('#featured-right').html(featured);						//if loading first new content erase others
	else
      $('#featured-right').append(featured);		   				//if current record not first content append to container
				
    if(i == feat.length-1)
      $('#featured-right-more a.next').hide();	
	  txtReplace();	//if end of entries hide next links
    };	
	txtReplace();
};

$(document).ready(function(){
						   
	//hide prev link
	$('#featured-right-more a.prev').hide();
	
	//switch between featured catagories
	$('#featured-right-header a').click(function()
	{
		$('#featured-right-header a').removeClass('selected');
  		$(this).addClass('selected');
		loadFeatured();
		return false;
	});
	
	//activate 'more' link
	$('#featured-right-more a.next').click(function()
	{
		var featuredHeight = $('#featured-right').height();		//capture height of featured container
		$('#featured-right').css('min-height', featuredHeight);  	//apply min-height css to container 
		
		$.getJSON("/json-featured",
			function(feed) {
			  var feat = feed.items;
			  var cat = $('#featured-right-header a.selected').text();
		  
			if (cat == 'Artists') {
			  feat = $.map(feat, function(n){
			   if(n.category == "Artists" )
				 return n;
			  });
			} else if (cat == 'Guests') {  
			  feat = $.map(feat, function(n){
			   if(n.category == "Guests")
				 return n;
			  });
			} else {  
			  feat = $.map(feat, function(n){
			   if(n.category == "Companies")
				 return n;
			  });
			};
			nextFeatured(feat);
		   }
	   	);
		$('#featured-right').css('min-height', 0);  			//remove min-height restriction on featured container
		
		if(!$('#featured-right-more a.prev').is(':visible'))	//if previous link not visible then show
			$('#featured-right-more a.prev').show();
	
		return false;
	});
	
	//activate 'previous' link
	$('#featured-right-more a.prev').click(function()
	{
		var featuredHeight = $('#featured-right').height();		//capture height of featured container
		$('#featured-right').css('min-height', featuredHeight);  	//apply min-height css to container
		
		if(!$('#featured-right-more a.next').is(':visible'))	//if more link not visible then show
			$('#featured-right-more a.next').show();
		
		$.getJSON("/json-featured",
		  function(feed) {
			var feat = feed.items;
			var cat = $('#featured-right-header a.selected').text();
		  
			if (cat == 'Artists') {
			  feat = $.map(feat, function(n){
			   if(n.category == "Artists" )
				 return n;
			  });
			} else if (cat == 'Guests') {  
			  feat = $.map(feat, function(n){
			   if(n.category == "Guests")
				 return n;
			  });
			} else {  
			  feat = $.map(feat, function(n){
			   if(n.category == "Companies")
				 return n;
			  });
			};
			previousFeatured(feat);
		   }
	   	);
		if(!$('#featured-right-more a.prev').is(':visible'))	
			$('#featured-right-more a.prev').show();			//if previous link not visible then show
			
		$('#featured-right').css('min-height', 0);  			//remove min-height restriction on container
		return false;
	});
	
	//load initial featured content into sidebar container
	loadFeatured();
});