
// 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
     $('#mycarousel').jcarousel({
          easing:   'easeInOutQuad',
          scroll:    9,
          visible:   9,
          wrap:      null,
          animation: 1500
     });

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

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

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