var current_slide;
var slideshow_interval;

function activate (obj)
{
	current_slide = $(obj).index();
	deactivateAll();
	
	$(obj).addClass ("active");	
	Cufon.replace('ul#fa_articles li h1 a');

	$("ul#fa_pictures li:eq(" + current_slide + ")").addClass("active");	
}

function deactivateAll ()
{
	//$("ul#fa_articles").find('div').remove();
	$("ul#fa_articles li").stop(true, true).removeClass ("active");
	$("ul#fa_pictures li").stop(true, true).removeClass ("active");

	Cufon.replace('ul#fa_articles li h1 a');
}

function switch_slideshow()
{
  var next_slide = (current_slide + 1) % 5;

  var next_slide_obj = $("ul#fa_articles li:eq(" + next_slide + ")");
  
  activate (next_slide_obj);
}



$(document).ready(function() {
	
	/* SLIDESHOW */
	
	current_slide = 0;
	
	activate ($("ul#fa_articles li").first());
	
	slideshow_interval = setInterval ("switch_slideshow()", 6000);	// switch slideshow every 6 seconds;

	$("ul#fa_articles li").hover(
	  function () {
	    clearInterval (slideshow_interval);
		activate (this);
	  }, 
	  function () {
	    
	  }
	);


	
	/* TABS */

	$('#recommended_reading').tabs({ fx: { opacity: 'toggle' } });

	/* RECOMMENDED SITES */

	$('div#recommended_sites').tabs({ fx: { opacity: 'toggle' } });	
	$('div#recommended_sites').tabs( "select" , 4 )

	
	/* BOOKS */ 
	
	$('#right_arrow_recommended').click(function()
	{
		var $tabs = $('#recommended_reading').tabs();
		var selected = $tabs.tabs('option', 'selected');
		
		$tabs.tabs('select', selected+1);
	});

	$('#left_arrow_recommended').click(function()
	{
		var $tabs = $('#recommended_reading').tabs();
		var selected = $tabs.tabs('option', 'selected');
		
		$tabs.tabs('select', selected-1);
	});

	/* VIDEOS */ 

	$('ul#top_video_links li a').click(function ()
	{
		var vid;
		var results;
		
		if (this.href.match("youtube.com/"))
		{
			//http://www.youtube.com/watch?v=GItD10Joaa0
			results = this.href.match("[\\?&]v=([^&#]*)");

			vid = ( results === null ) ? url : results[1];
		}
		else if (this.href.match("youtu.be/"))
		{
			// http://youtu.be/5uxd-521uus?hd=1
			results = this.href.match("^http://youtu.be/(.*)(?=hd=1)");

			vid = ( results === null ) ? url : results[0];
		}

		var $iframe = $('#video_iframe');
		if ($iframe.length)
		{
			$iframe.attr('src', 'http://www.youtube.com/embed/' + vid);   
		}
		
		$('ul#top_video_links li').removeClass ('active');
		$(this).parent().addClass ('active');

		return false;
	});

	$('ul#top_video_links li a').first().click();	
	
	
		var h1=$('#h1_item_1');
		var height = h1.height();
		$('li#slider_item_1').addClass("ss"+height);		
	
});

