
	function galleryPreviewerInit()
	{
		var galleryPreviewer = this;
		$(galleryPreviewer)
			.wrapInner('<div class="previous button"><div class="next button"><div class="container" /></div></div>')

		var overFunction = function(event) { $(this).addClass('over'); event.stopPropagation()};
		var outFunction = function() {$(this).removeClass('over')};
		var clickFunction = function(event) {			
			var isNext = $(this).is('.next');
			var isPrevious = $(this).is('.previous');

			var next = $(galleryPreviewer).find(".next");
			var previous = $(galleryPreviewer).find(".previous");

			var list = $(galleryPreviewer).find("ul");
			var left = list.data('left');
			var width = list.data('width');
			var scroll = list.data('scroll');
			var containerWidth = $(galleryPreviewer).find("div.container").width();
			left += isNext ? -scroll : scroll;
			if(left >= 0 )
			{
				previous.each(disableButton);
				left = 0;
			}
			else previous.each(enableButton);

			if(width + left < containerWidth)
			{
				next.each(disableButton);
				left = containerWidth - width;
			}
			else next.each(enableButton);

			list.animate({left: left})
			list.data('left', left);

			event.stopPropagation();
			return false;
		};

		var stopEvent = function(event)
		{
			event.preventDefault();
			event.stopImmediatePropagation();
			return false;
		}
		
		var disableButton = function()
		{
			$(this)
				.addClass('disabled')
				.removeClass('over')
				.unbind('mouseover', overFunction).mouseover(stopEvent)
				.unbind('mouseout', outFunction).mouseout(stopEvent)
				.unbind('click', clickFunction).click(stopEvent)
		}

		var enableButton = function()
		{
			$(this)
				.removeClass('disabled')
				.unbind('mouseover', stopEvent).mouseover(overFunction)
				.unbind('mouseout', stopEvent).mouseout(outFunction)
				.unbind('click', stopEvent).click(clickFunction)
		}

		var list = $(galleryPreviewer).find("ul");

		list.data('left', 0)
			.mouseover(stopEvent)
			.click(stopEvent)
			.css('width', (list.width() * $(list).find("li").length) + 'px')

		$(galleryPreviewer).find(".button").each(disableButton).mousedown(stopEvent);

		list.find("a").click(ajaxLinkHandler);

		var itemWidthReady = function()
		{
			var img = $(list).find("li:first img");
			var itemWidth = list.find("li:first").width();

			list.data('scroll', itemWidth * 3)
			list.data('width',  itemWidth * list.find("li").length)

			if(list.data('width') > $(galleryPreviewer).find(".container").width())
				$(galleryPreviewer).find(".next").each(enableButton);
		}
		
		var img = $(list).find("li:first img");
		if(img.attr('complete')) itemWidthReady();
		else img.load(itemWidthReady);
	}
	
	$("div.galleryPreviewer").ready(function() {
		$("div.galleryPreviewer").each(galleryPreviewerInit);
	})

