jQuery(document).ready(function()
{
	jQuery('#footer').tabs({fxAutoHeight: true, fxFade: true});
	
	init_blog_page(); // Only executes if on blog page
	
	if (jQuery('#gallery-page').size())
	{		
		jQuery("ul.thumbs a").fancybox(
		{
			'hideOnContentClick': true,
			'overlayShow':true,
			'overlayOpacity':0.8,
			'zoomSpeedIn':200,
			'zoomSpeedOut':200
		});

	}
});

// Setup the blog page functionality
function init_blog_page()
{
	if (!jQuery("#blog-entries").size()) return false;

	jQuery("#blog-entries li").eq(0).addClass("visible").end() 	// Set visible class to the first one
	.slice(1).addClass('hidden') 							// Set hidden class to the rest
	.find(".content").hide();								// And also hide the rest
 	
	// Make headers clickable for toggling text (wraps all h3 and dates in <a>)
	jQuery("#blog-entries h3").each(function(i)
	{
		var $this = jQuery(this);
		var $date = $this.next();
		
		$this.wrap('<a href="#" class="toggler"></a>').parent().append($date);
		$this.parent().next().addClass('c' + i);	// This class is used to determine if we click an open or a closed one.
	});
	
	// Assign toggling to click
	jQuery("#blog-entries a.toggler").each(function() {
		jQuery(this).click(function() {
			var $this = jQuery(this);
			var $content = $this.parents('li').find(".content");
			$this.blur();
			toggle_blog_content($content);
			return false;
		});
	});
}

// Toggles entries.
function toggle_blog_content($obj)
{	
	var $vis_content = jQuery("#blog-entries .content:visible");
	if ($vis_content.size()) // If anyone is open - close that one
	{
		$vis_content.slideUp("fast",function() // Close it
		{ 
			jQuery("#blog-entries li").removeClass('visible').addClass('hidden');
			// If a closed one is clicked - show it
			if ($vis_content.attr("class") != $obj.attr("class"))
				show_blog_content($obj)
		});
	}
	else // If all are closed - just show the wanted one
	{
		show_blog_content($obj)
	}
}

// Shows the clicked entry	
function show_blog_content($obj)
{
	$obj.slideDown("normal",function() {
		$obj.parent().removeClass("hidden").addClass('visible');
	});
}

