$(document).ready(function() {
	$('.ThumbList li a img').fadeTo(0,0.8);
	$('.ThumbList li a.activeThumb img').fadeTo(0,1.0);
	
	$('.ThumbList li a img').hover(
		function () { $(this).fadeTo("slow",1.0); },
		function () { if (!$(this).parent().hasClass('activeThumb')) { $(this).fadeTo("slow",0.8); } }
	);
	
	/* Thumbnail Animation */
	var TotalImage = $('.ThumbList li').length;
	var ImageWidth = $('.ThumbList li').width() + 22;
	$('.ThumbList').width(ImageWidth * TotalImage);
	var TotalWidth = $('.ThumbList').width();
	var LastPos = (TotalWidth * -1) + (ImageWidth * 3);
	var MoveTo = 0;
	
	$('.arRight a').click(function() {
		goScroll(1);
		return false;
	});
	$('.arLeft a').click(function() {
		goScroll(0);
		return false;
	});
	
	function goScroll(prev)
	{
		MoveTo = (prev) ? ((MoveTo - ImageWidth) >= LastPos) ? (MoveTo - ImageWidth) : 0 : ((MoveTo + ImageWidth) <= 0) ? (MoveTo + ImageWidth) : ((ImageWidth * TotalImage * -1) + (ImageWidth * 3));
		$('.ThumbList').animate({'left' : MoveTo},600);
	}
	$(".ThumbList li a").click(function() {
		if (!$(this).hasClass('activeThumb')) {
			$('.ThumbList li a.activeThumb img').fadeTo("slow",0.8);
			$('.ThumbList li a').removeClass('activeThumb');
			$(this).addClass('activeThumb');
			
			var img = new Image();
			$(img).load(function () {
				$(this).hide();
				$('#ImageContainer').html(this);
				// fade our image
				$('#ImageContainer img').fadeIn("slow");
			})
			.error(function () {
				alert('Something has happened. We are unable to load this image.');
			})
			.attr('src', $(this).attr('href'));
		}
		return false;
	});
});
