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

		// Add top-margin to landscape thumbs to get them vertically centered
		$("ul.thumbs img").each(function()
		{
			var $this = $(this);
						
			var img_height = $this.height();

			if (img_height < 78)
			{
				img_top_margin = (78 - Math.floor(img_height)) / 2;
				//$this.css('margin-top',img_top_margin);
			}
		});
	}
});

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

	$("#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>)
	$("#blog-entries h3").each(function(i)
	{
		var $this = $(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
	$("#blog-entries a.toggler").each(function() {
		$(this).click(function() {
			var $this = $(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 = $("#blog-entries .content:visible");
	if ($vis_content.size()) // If anyone is open - close that one
	{
		$vis_content.slideUp("fast",function() // Close it
		{ 
			$("#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');
	});
}

