$(document).ready(function(){
	
	var images = $('#imageContainer img').hide();
	var thumbs = $('#thumbContainer img').hide();
	var total = images.length + thumbs.length;
	
	function set()
	{
		var real = this, img = new Image();
		
		$(img).load(function(){
			if(!--total)
				setTimeout(init, 50);
		}).error(function(){
			if(/^a$/i.test(this.parentNode.nodeName))
				this.parentNode.parentNode.removeChild(this.parentNode);
			else
				this.parentNode.removeChild(this);
			if(!--total)
				setTimeout(init, 50);
		}).attr({
			id	: real.id,
			alt	: real.alt,
			src : real.src,
			width: $(real).width(),
			height: $(real).height()
		}).css("display", "none");
		
		$(real).replaceWith(img);
	}
	
	images.each(set);
	thumbs.each(set);
	
	function init()
	{
		var images = $('#imageContainer img');
		var thumbs = $('#thumbContainer a');

		if(images.length === 1)
		{
			$(images).show();
		}
		else if(images.length > 1)
		{
			thumbs.mouseover(function(){
				$('#imageContainer img').hide().filter(this.hash).show();
			});
			
			$(images[0]).show();
			$('#thumbContainer img').show();
		}
	}
	
	/*
	// Hide all large images except the first one
	$('#imageContainer img').hide().filter(':first').show().error(function(e){
		this.parentNode.removeChild(this);
	});

	// Select all thumb links
	$('#thumbContainer a').mouseover(function(event) {
		// Hide all large images except for the one with the same hash as our thumb link
		$('#imageContainer img').hide().filter(this.hash).show();
	});
	*/
});