$(document).ready(function()
{
	$("#slideshow-menu ul li:first").addClass('active'); 
	$("#slideshow-menu ul li:first h2").truncate(38);
	$("#slideshow-menu ul").show();
	
	$("#slideshow-menu ul li").click(function()
	{ 
		if (!$(this).hasClass('active'))
		{
			//Set Variables
			var imgUrl = $(this).find('a').attr("href"); //Get Image URL
			
			if ($(this).is(".active"))   //If it's already active, then...
			{
				return false; // Don't click through
			} 
			else 
			{
				$("#slideshow-menu ul li").removeClass('active'); //Remove class of 'active' on all lists
				$(this).addClass('active');  //add class of 'active' on this list only
				$(this).hide();
				
				$("#slideshow-menu ul li h2 span").parent().html($("#slideshow-menu ul li h2 span").html()); //Remove span on all h2 titles
				$(this).find('h2').truncate(38);
				$(this).removeClass('hover');
				$(this).fadeIn();
				
				$(".hero img#loader").show();
				$(".hero img#hero").hide();
				$(".hero img#hero").load(function () { $(".hero img#loader").hide(); $(this).fadeIn(); })
				$(".hero img#hero").attr({ src: imgUrl });
			}
			
			return false;
		}
		else
		{
			return true;
		}
		
	}) .hover(function(){
		if (!$(this).hasClass('active'))
			$(this).addClass('hover');
			}, function() {
			$(this).removeClass('hover');
	});
	
});

$.fn.truncate = function(len)
{
	var element = $(this);
	if (element) 
	{
		if (element.html().length > len)
		{
			var wordArr = element.html().split(' ');
			var newText = '';
			var n = 0; 
			while (newText.length < len)
			{
				newText += wordArr[n] + ' ';
				n++;
			} 
			newText += '...<span class="hide">' + element.html() + '</span>';
			element.html(newText);
		}
	}
};
