jQuery.fn.gallery = function(opts){
	// set default options
	var defaults = {
		target      : '.galleria_container',
		caption     : '.galleria_container .caption',
		activeThumbClass : 'active',
		inactiveThumbClass : 'inactive',
		beforeLoad : function(target,caption){
			$('.loadingImage',target).remove();
			target
				.append('<div class="loadingImage">Loading Image</div>')
				.height( (target.innerHeight() > 400) ?target.innerHeight() : 420 );
				
			var image = $(this)
			image.fadeOut(200,function(){  image.remove();window.location = '#gallery' });
			
			caption.html('');
		},
		onLoad : function(target,caption,text){// called when new image is ready to diplay
			$('.loadingImage',target).remove();
			
			target.height('auto');
			
			window.location = '#gallery'
			var image = $(this);
			var insert = setInterval(function(){
				if( $('img',target).length == 0) {
					clearInterval(insert);
					image
						.insertBefore(caption)
						.hide()
						.fadeIn(200);
					caption.html(text);
					window.location = '#gallery'
				}
			},100);
			
		}
	};
	
	// extend the options
	var settings = $.extend(defaults, opts);
	
	return this.each(function(){
		var $this = $(this),
			target = $(settings.target),
			caption = $(settings.caption),
			activeThumbClass = settings.activeThumbClass,
			beforeLoad = settings.beforeLoad,
			onLoad = settings.onLoad;
		
		$this
			.unbind('click.gallery')
			.bind('click.gallery',function(e,element){
				var current = (element) ? element : e.target,
					gallery = $(this);
								
				if(current.nodeName != 'IMG'
				   ||gallery.attr('data-gallery-processing') == 't'  )
					{ e.stopPropagation(); e.preventDefault(); return false; }
				
				current = $(current).parents('a:first');
								
				gallery.attr('data-gallery-processing','t');
				
			// Remove active class from current active image
				$('.'+activeThumbClass,this)
					.removeClass(activeThumbClass);
								
				var	currentLI = current.parents('li:first'),
					nextSrc = $('a', currentLI.next('li') ).attr('href'),
					largeSrc = current.attr('href'),
					largeCaption = current.attr('title'),
					large = new Image(),
					next = new Image();
				
				large.src = largeSrc;
				next.src = nextSrc;
				beforeLoad.apply($('img',target),[target,caption]);
				
			// Add active class to selected image
				currentLI.addClass(activeThumbClass);
				
				$(large)
					.load(function(){
						onLoad.apply(this,[target,caption,largeCaption]);
						gallery.attr('data-gallery-processing','f');
						
					})
				
				return false;
			})
			.trigger('click.gallery', $('img:first', $this) );
	});
};

$(document).ready(function(){
	$('.columns_2 .section').equalizeCols();
	$('.columns_3 .section').equalizeCols();


	$('ul.gallery')
		.gallery({
			target      : '.galleria_container',
			caption     : '.galleria_container .caption'
		})
	
	$('#gallery_thumbs').serialScroll({
		items:'li',
		prev:'#gallery_prev',
		next:'#gallery_next',
		axis:'x',
		offset:-230, //when scrolling to photo, stop 230 before reaching it (from the left)
		start:0, //as we are centering it, start at the 2nd
		duration:500,
		force:true,
		stop:false,
		step: 3,
		event:'click',
		lock:false,
		cycle:false, //don't pull back once you reach the end
		easing:false,//'easeOutQuart', //use this easing equation for a funny effect
		jump: true //click on the images to scroll to them,
	});
});