$(function()
{
	function path()
	{
		var path = window.location.href.split("#", 2);
		return path[1] ? path[1] : false;
	}
	
	$('.tabs').each(function(i)
	{
		var $tabs = $(this).children('thead').find('td');
		var $container = $(this).children('tbody').find('div');
		
		$tabs.each(function(n)
		{
			var name = "Tab_" + i + '-' + n;
			
			// Give the container a unique id
			$container.eq(n).attr('id', name);
			
			// And this its name and click handler
			$(this)
				.attr('name', name)
				.click(function()
				{
					$(this).addClass('sel').siblings('.sel').removeClass('sel');
					$('#' + $(this).attr('name')).show().siblings().hide();
				});
		});
		
		// Hide based on incoming path requested
		if (path() && $tabs.filter('[name='+path()+']').length) 
		{
			$tabs.filter('[name='+path()+']').click();
		} 
		else 
		{
			$tabs.eq(0).click();
		}
		
	});
	
	// Set up expandable DLs
	$('.dl_exp').each(function(i)
	{
		$(this).find('dt').each(function(n)
		{
			var name = "dd_" + i + '-' + n;
			
			$(this).attr('name', name).click(function()
			{
				$('#' + $(this).attr('name')).toggle();
			})
			.next().hide().attr('id', name);
		});
	});
	
	// Facybox
	$('[rel*=facybox]').facybox();
	
	// Fixes bugs where the video stays displayed or
	// the sound keeps playing in certain browsers
	$(document).bind('close.facybox', function(e)
	{
		$('#facybox .content').html('');
	});
	
	// Fold up videos
	$('#pagination a').click(function()
	{
		var number = $(this).attr('data-page');
		
		$('#videos .video')
			.hide()
			.slice((number - 1) * 5, number * 5)
			.show();
			
		$(this).addClass('selected').siblings().removeClass('selected');
	})
	
	// Show the first set
	.eq(0).click();
});

function descToggle(i) 
{
	if ($(i).html() == 'Show all') {
		$('.dl_exp dd').show();
		$(i).html('Hide all');
		$(i).prev().attr('src','images/layout/minus.png');
	} else {
		$('.dl_exp dd').hide();
		$(i).html('Show all');
		$(i).prev().attr('src','images/layout/plus.png');
	}
}


