$(document).ready(function() {
	$('#scroll').scrollingCarousel({
		scrollSpeed: 'slow',
		looped: false
	});
	
	// Preload the images	
	$('#scroll li img').each(function() {
		var img_src = $(this).attr('src').split('/thumbs');
		img_src = img_src[0] + img_src[1];
		
		//$('<img/>')[0].src = img_src;
		var newImg = new Image();
		newImg.src = img_src;
	});
	
	
	// Setup for photo pop-up
	var thePhoto 		= $('#thePhoto');
	var photoWrapper	= $('#photoWrapper');
	
	photoWrapper.hide();
	thePhoto.attr('src', '');
	
	
	// thumbOnClick
	$('#scroll img').css('cursor', 'pointer').click(function() {
		
		var img_src = $(this).attr('src').split('/thumbs');
		img_src = img_src[0] + img_src[1];
		
		//var new_img	= new Image();
		//new_img.src	= img_src;
		
		// photoSwitch
		function photoSwitch() { 
			thePhoto.fadeOut('slow', function(){
				$(this).attr('src', img_src);
				
				if(thePhoto[0].complete) {
					photoWrapper.removeClass('loading');
					thePhoto.fadeIn('slow');
				} else {
					thePhoto.load(function() {
						photoWrapper.removeClass('loading');
						thePhoto.fadeIn('slow');
					});
				}
			});
		}
		
		// photoPopUp
		function photoInit() {
			var newPhoto = thePhoto.attr('src', img_src);
			if(newPhoto[0].complete) {
				photoWrapper.removeClass('loading');
				photoWrapper.fadeIn();
			} else {
				newPhoto.load(function() {
					photoWrapper.removeClass('loading');
					photoWrapper.fadeIn();
				});
			}
		}
		
		
		if (photoWrapper.is(':hidden')) { photoInit(); } else { photoSwitch(); }
		
		$('#photoClose').fadeIn();
	}); // end thumbOnClick
	
	
	$('#photoClose').click(function(){
		photoWrapper.fadeOut(function() {
			thePhoto.attr('src', '');
		});
		$(this).fadeOut();
		return false;
	});
});
