(function($){

	$.fn.gallery = function(options)
	{
		var settings = $.extend({
			fullImage: '#gallery-full-image',
			autoShow: true
		}, options);
		$.gallery.settings = settings;

		var firstItem = null;
		this.find('li').each(function(){
			var $li  = $(this);
			var $a   = $li.find('a');
			var $img = $a.find('img');
			var data = {
				thumbURL: $img.attr('src'),
				imageURL: $a.attr('href')
			};
			$li.data('gallery', data);
			$li.empty();
			$li.css({
				cursor: 'pointer',
				background: 'transparent url("'+data.thumbURL+'") 50% 50% no-repeat'
			});
			$li.click($.gallery.onClick);
			if (!firstItem) {
				firstItem = $li;
			}
		});

		if (settings.autoShow && firstItem) {
			$.gallery.show(firstItem.data('gallery').imageURL);
		}

		var fullImage = $($.gallery.settings.full_image);
		fullImage.css(
			{
				'backgroundColor': 'transparent',
				'backgroundPosition': '50% 50%',
				'backgroundRepeat': 'no-repeat'
			}
		).height(550); //this.parents('div.span-19').height());

		return this;
	};

	$.gallery = {
		settings: {},
		show: function(url)
		{
			var div = $($.gallery.settings.full_image);
			var img = document.createElement('IMG');
			img.src = url;
			$(img).load(function() {
				if (img.height > 550) {
					div.height(img.height);
				}
			});
			div.css('backgroundImage', 'url("'+url+'")');
		},
		onClick: function()
		{
			var data = $(this).data('gallery');
			$.gallery.show(data.imageURL);
			return false;
		}
	};

})(jQuery);
