var small_width = 148;
var medium_width = 444;
var current_image = 1;

function RedrawStage(first_run)
{
	$('#contents').css('left', Math.round($(window).width()/2-(medium_width/2)));
	$('#slider_b').css('left', Math.round($(window).width()/2-(medium_width/2))-(small_width*(current_image-1)));
	$('#stage_a,#stage_b').css('left', Math.round($(window).width()/2-(medium_width/2)));
	$('#viewport_a').css("left", Math.round($(window).width()/2+(medium_width/2))-small_width);
	$('#viewport_b').css('right', Math.round($(window).width()/2+(medium_width/2))-small_width);
	if (first_run)
	{
		$('#contents').css('display', 'block');
		$('#stage_a,#stage_b').css('display', 'block');
		$('#viewport_a,#viewport_b').css('display', 'block');
	}
}

$(window).resize(function()
{
	RedrawStage(false);
});

$(window).load(function()
{
	RedrawStage(true);
	
	//$('html.home').css('overflow', 'hidden');
	
	$('.nav>a').bind('mouseover', function(){ $(this).addClass('hover'); });
	$('.nav>a').bind('mouseout', function(){ $(this).removeClass('hover'); });
	
	$('#stage_a>a,#stage_b>a').bind('click', function(event)
	{
		tmp = $('#slider_a>a');
		$(tmp[current_image]).trigger('click');
		document.location.hash = '#photo' + current_image;
		return false;
	});
	
	$('#slider_a>a,#slider_b>a').bind('click', function(event)
	{
		index = parseFloat($(this).attr('href').substr(6));
		if (isNaN(index)){ index = 0; }

		move_by = (Math.abs(current_image - index)*small_width);
		$('#slider_a,#slider_b').animate({left: [(current_image < index?'-=':'+=') + move_by, 'swing']}, {duration: 500});
		
		current_image = index;
		if ($('#stage_a').css('display') == 'block')
		{
			$('#stage_b>a>img').load(function ()
			{
				$('#stage_a').fadeOut(250);
        	}).attr('src', $('img', this).attr('src').replace('data.small/', 'data/'));
		}
		else
		{
			$('#stage_a>a>img').load(function ()
			{
				$('#stage_a').fadeIn(250);
        	}).attr('src', $('img', this).attr('src').replace('data.small/', 'data/'));
		}
	});
	
	onload_index = parseFloat(document.location.hash.substr(6));
	if (!isNaN(onload_index))
	{
		tmp = $('#slider_a>a');
		$(tmp[onload_index-1]).trigger('click');
		document.location.hash = '#photo' + onload_index;
	}
	
});
