var keep_fading = true;

$(function()
{
	thumb_switcher();
	// setTimeout('random_quote()',2000);
	// 
	// $('#quote blockquote').append('<a href="#">Tyst!</a>').find('a')
	// .toggle(function() {
	// 	keep_fading = false;
	// 	$(this).text("Speak up!")
	// },
	// function()
	// {
	// 	keep_fading = true;
	// 	random_quote();
	// 	$(this).text("Tyst!")
	// });
	portfolio_switcher();
});

function portfolio_switcher()
{
	$('#nav-sub a').click(function()
	{
		$this = $(this);
		$this.blur();
		
		href = $this.attr('href');
		
		$('#portfolio').fadeOut('normal',function() {
			$(this).empty().addClass('loading').show();
			$.get('/includes/' + href, function(data)
			{	
				$("#portfolio").animate({height: '200px'},500,function()
				{
					$('#portfolio').removeClass('loading').hide().append(data).fadeIn();
					thumb_switcher();
					tb_init('a.thickbox');
				});
			});
		});
		
		// Make switcher show the currently selected thumb
		$this.parents('ul#nav-sub').find('li').removeClass('sel');	
		$this.parent().addClass('sel');
		return false;
	});
}

function random_quote()
{
	if (keep_fading == true)
	{
		$.get("/lib/random-quote-get.php", function(data)
		{
	 		$quote = $('#quote blockquote p');
			$quote.fadeOut('normal',function() {
				$quote.text(data).fadeIn();
				setTimeout('random_quote()',3000);
			});
		});
	}
}

function thumb_switcher()
{
	$("ul.switcher a").click(function() {
			$this 				= $(this);
			$case_div 		= $this.parents('li.case');
			$this.blur();
			
			$thumb_parent = $case_div.find('a.thumb'); 		// img's parent - <a>
			$thumb	 			= $thumb_parent.find('img');		// img inside the anchor
			
			$deflist_values = $case_div.find('dl dd');
			$case_header		= $case_div.find('h4');
			
			img_src 	= $this.attr('href'); 										// Link to the big image
			thumb_src = img_src.replace(/.jpg/, "-thumb.jpg");	// add -thumb before .jpg to make it the source to the thumb instead
			
			$thumb_parent.addClass('loading');		// Add loader
			$thumb.fadeOut('normal',function()		// Fade out thumb
			{	
				$thumb.attr('src',thumb_src);
				$(this).hide().fadeIn('slow',function() // Fade in new thumb
				{	
					$thumb_parent.attr('href',img_src);
					$thumb_parent.removeClass('loading');		// Remove loader
				});	
			});
			
			// Make switcher show the currently selected thumb
			$this.parents('li.case').find('a').removeClass('sel');	
			$this.addClass('sel');
			return false;
	});
}