function sizeToFit() {
	var width = 0;
	$('#gallery > ul > li:visible').each(function() {
		width += $(this).width() + 10;
		if ($(this).is('.text')) width += 20;
	});
	$('#gallery > ul').width(width);
}

function resize() {
	var extra = 0;
	if ($(window).height() > 830) {
		extra = 200;
	} else if ($(window).height() > 730) {
		extra = 100;
	}
	
	var windowHeight = $(window).height();
	$('#gallery').height(470 + windowHeight - 630);
	$('#gallery img').height(400 + windowHeight - 630);
	$('#gallery ul').height(450 + windowHeight - 630);
	$('#gallery li.text div').height(400 + windowHeight - 630);
	
	if (windowHeight > 730) {
		$('#gallery li.img').each(function() {
		    var index = $('#gallery li.img').index(this);
		    $(this).find('img').attr('src', images.large[index]);
		});
	} else if (windowHeight > 630) {
		$('#gallery li.img').each(function() {
		    var index = $('#gallery li.img').index(this);
		    $(this).find('img').attr('src', images.medium[index]);
		});
	} else {
		$('#gallery li.img').each(function() {
		    var index = $('#gallery li.img').index(this);
		    $(this).find('img').attr('src', images.small[index]);
		});
	}
	sizeToFit();
}

$(function() {
	if (! ($.browser.msie && $.browser.version.substr(0,1)<7) ) {
		$('#gallery li.img').hide();
	}
	
	$('#gallery li img').bind('load', function() {
		$(this).closest('li').show();
		sizeToFit();
	});
	
	$(window).resize(resize);
	resize();
});
