(function($) {
	if (typeof g_use_multiple_image != "undefined" && g_use_multiple_image) {
		$(document).ready(function() {
			//
			// Does a multiple image selector exist already?
			// If so, then add images to it instead.
			// All multiple image scripts put the following nodes into the DOM:
			// <div id="imageholder"><div id="mainholder"><a><img id="mainimg">
			// If that node structure exists, then it's fair to say that there's
			// an existing multiple image.
			if ($('#imageholder > #mainholder > a[rel] > img#mainimg').length > 0) {
				var multiple_images = $('.multiple_image-additional');
				if (multiple_images.length > 0) {
					//
					// The container for multiple images (#floats) won't show if
					// there are no images. Display it if there are any.
					$('#floats').show();
					//
					// Set the size of multiple image thumbnails to be the same
					// as the existing thumbnail size, or the size in config
					// if there are none.
					var new_image_size = g_additional_image_size;
					var existing_image = $('#floats img:first');
					if (existing_image.length) {
						var url_parts   = existing_image.attr('src').split('?');
				        var query_parts = $.query.load('?'+url_parts[1]);
				        if (query_parts.get('w')) {
				        	new_image_size[0] = query_parts.get('w');
				        }
				        if (query_parts.get('h')) {
				        	new_image_size[1] = query_parts.get('h');
				        }
					}
					//
					// Make sure this image shows at exactly w and h pixels.
					// To achieve this, show it as a PNG (unless IE6), and give
					// it a background color.
					var multiple_image_attrs = {bg: 'FFFFFF', f: ($.browser.msie && $.browser.version <= 6 ? 'gif' : 'png')};
					existing_image.attr('src', $.replaceUrlParts(existing_image.attr('src'), multiple_image_attrs));
					//
					// Add each new image to the container, and set up its click
					// event.
					multiple_images.each(function() {
						var new_url     = $.replaceUrlParts(this.src, {w: new_image_size[0], h: new_image_size[1]});
				        var new_image   = $('<img />');
				        new_image.attr('src', $.replaceUrlParts(new_url, multiple_image_attrs));
				        new_image.click(function() {
				        	var main_image      = $('#mainimg');
				        	var main_image_link = main_image.parent();
				        	main_image.attr('src', $.replaceUrlPartsFrom(main_image.attr('src'), new_url, ['src', 'f']));
				        	main_image_link.attr('href', $.replaceUrlPartsFrom(main_image_link.attr('href'), new_url, ['src', 'f']));
				        });
				        $('#floats').append(new_image);
					});
				}
				$('.multiple_images').remove();
			}
			else {
				var g_multiple_image_selector = $('#multiple_image_selector').multipleImageSelector({
					main_image_size:       g_main_image_size,
					additional_image_size: g_additional_image_size,
					expanded_image_size:   g_expanded_image_size
				});
			}
		});
	}
})(jQuery);