(function($) {
    $.fn.gallery = function(options) {

	var settings = {
	    'selection'        : 'gallery-selection',
	    'category'         : null,
	    'initial'          : null
	};
	var current_product;
	var current_image;

	function loadImage(path, load_details) {
	    var category = settings['category'];
	    current_product = category.getProduct(path);
	    if (!current_product) {
		// No product matching path so display the first product.
		current_product = category.getFirstProduct();
		path = current_product.thumb;
	    }
	    current_image = category.getImage(current_product, path);
	    var picture = $('.gallery_big_picture',
			    settings['selection']);
	    if (current_image.path === picture.attr('src')) {
		return;
	    };
	    picture.fadeOut('slow', function() {
		picture.attr('src', current_image.path);
	    });
	    if (load_details) {
		loadDetails();
	    } else {
		var caption = current_product.name;
		if (current_image.caption) {
		    caption = caption + ' - ' +
			current_image.caption;
		}
		var description = current_image.description ||
		    current_product.description ||
		    '[description]';
		var price;
		if (current_product.price &&
		    current_product.price != '$0.00') {
		    price = current_product.price;
		} else {
		    price = '';
		}
		$('.gallery_caption', settings['selection'])
		    .html(caption);
		$('.gallery_description', settings['selection'])
		    .html(description);
		$('.gallery_price', settings['selection'])
		    .html(price);
	    }
	};

	function loadDetails() {
	    var details_html = '';
	    if (current_product.images.length > 1) {
		for (var i in current_product.images) {
		    var image = current_product.images[i];
		    details_html += '<img src="' + image.detail_thumb +
			'" class="gallery_detail_thumbnail" />';
		}
	    }
	    $('.gallery_details', settings['selection'])
		.html(details_html);
	    var details = $('.gallery_details', settings['selection']);
	    $('img', details).click(function(e) {
		loadImage(e.target.src, false);
	    });
	    var caption = current_product.name;
	    if (current_product.images[0].caption) {
		caption = caption + ' - ' +
		    current_product.images[0].caption;
	    }
	    var description = current_product.description ||
		'[description]';
	    var price;
	    if (current_product.price &&
		current_product.price != '$0.00') {
		price = current_product.price;
	    } else {
		price = '';
	    }
	    $('.gallery_caption', settings['selection'])
		.html(caption);
	    $('.gallery_description', settings['selection'])
		.html(description);
	    $('.gallery_price', settings['selection'])
		.html(price);
	};

	return this.each(function() {
	    if (options) {
		$.extend(settings, options);
	    }
	    var $this = $(this);
	    $('img', this)
	        .css('cursor', 'pointer')
		.click(function(e) {
		    loadImage(e.target.src, true);
		});
	    $('.gallery_big_picture',
	      settings['selection']).load(function() {
		  $(this).fadeIn('slow');
	      });
	    if (settings['initial']) {
		loadImage(settings['initial'], true);
	    } else {
		loadImage('', true);
	    }
	});
    }
})(jQuery);
