
// image loader
$.fn.image = function(src){
	return this.each(function(){
		var i = new Image();
		i.src = src;
		this.appendChild(i);
	});
}


// entry point for main image swapping
function startImageLoad(e)
{
	// hide caption
	$('.caption').hide();
	
	// hide main image
	$('#main_image').fadeOut('slow',function(){
		
		// replace main image
		$('#main_image').empty();
		$('#loading').show();
		getImage(e);
     });
}

function getImage(e)
{
	// replace main image
	var href = $(e).attr('href');
	$('#main_image').image(href);
	
	// hide loader
	$('#loading').hide();
	
	// reveal main image
	$('#main_image').fadeIn('fast');

	// set caption
	var caption = $(e).find(":nth-child(1)").attr('title');
    $('.caption').html(caption).show();
}





/*   ########      DOCUMENT  READY      ######      */


$(document).ready( function(){

     // Set up carousel
     $('#thumbs').jcarousel({
          easing:'easeInOutQuad',
          scroll: 5,
          visible: 9,
          wrap: null,
          animation: 2000
     });

     // load the first big image
     startImageLoad($('#thumbs a:first'));

     $('#thumbs a').preload({ threshold:2 });

     // Bind image loading to each thumbnail's click event
     // and return false
     $('#thumbs a').click(function(){
          startImageLoad(this);
          return false;
     });
});