function ImageScroller(maxNumImages) 
{
	var iNumImages = 0;
	var iMinOffset = 0;
	var iMaxOffset = 0;
	var iCurOffset = 0;
	var iNumOfThumbsShown = 5;
	var curImage = 0;
	var t_height = 0;
	
	// HACK
	var iInitialPadding = 5;
	
	var iScrollSpeed = 600;
		
	this.replaceGallery = function(images)
	{
		var tag = "_sm_grey.jpg";
		
		$('#gallery').html('');
		
		iNumImages = images.length;
		
		for (var i = 0; i < iNumImages; i++)
		{
			var img_src = images[i] + tag;
			var href_src = images[i].replace('_sm_grey','');
			$('#gallery').append('<a href=' + href_src + '><img src=' + img_src + ' class="thumbnail_landscape"/><id="image_' + i + '/></a>');
		}
		
		// clip the images
		
		var thumbHeight = parseInt($('.thumbnail_landscape').css('height'));
		var thumbHeight1 = parseInt($('#gallery a').css('padding-top'));
		var thumbHeight2 = parseInt($('#gallery a').css('padding-bottom'));
		var thumbHeight3 = parseInt($('#gallery a').css('margin-top'));
		var thumbHeight4 = parseInt($('#gallery a').css('margin-bottom'));
		
		thumbHeight += thumbHeight1 = thumbHeight2 + thumbHeight3 + thumbHeight4 + 7;
		t_height = thumbHeight;
		
		var maxHeight = (thumbHeight * iNumOfThumbsShown);
	
		$('#gallery_frame').css('height', (maxHeight - iInitialPadding) + "px");
		
		var totalHeight = thumbHeight * iNumImages;

		iMinOffset = iInitialPadding;//parseInt($('#gallery').css('top'));
		iCurOffset = 0;
		iMaxOffset = totalHeight - maxHeight;
		
		if (iMaxOffset < 0)
		{
			iMaxOffset = 0;
		}
		
		if (iNumImages <= iNumOfThumbsShown)
		{
			$('#up_arrow').css('opacity', 0);
			$('#down_arrow').css('opacity', 0);
		}
		else
		{
			$('#up_arrow').animate({opacity: 0.999}, 600);
			$('#down_arrow').animate({opacity: 0.999}, 600);
		}
		
		$('#gallery').stop();
		var new_top = iMinOffset - iCurOffset + "px";
		$('#gallery').css('top', new_top);
		
		prepareImages();
	};
	
	function scrollDown ()
	{	
		if (iCurOffset > 0)
		{
			iCurOffset -= t_height * iNumOfThumbsShown;
		}
		
		iCurOffset = clamp(iCurOffset, 0, iMaxOffset);
		var new_top = iMinOffset - iCurOffset + "px";
  		$('#gallery').stop().animate({top: new_top}, iScrollSpeed);
	}
	
	function scrollUp ()
	{
		if (iCurOffset < iMaxOffset)
		{
			iCurOffset += t_height * iNumOfThumbsShown;
		}
		
		iCurOffset = clamp(iCurOffset, 0, iMaxOffset);
		var new_top = iMinOffset - iCurOffset + "px";
		$('#gallery').stop().animate({top: new_top}, iScrollSpeed);
	}
	
	function clamp (value, minVal, maxVal)
	{
		if (value < minVal)
		{
			value = minVal;
		}
		
		if (value > maxVal)
		{
			value = maxVal;
		}
		
		return value;
	}
	
	$('#up_arrow').click(function(e) 
	{
		scrollDown();
	});
	
	$('#down_arrow').click(function(e) 
	{
		scrollUp();
	});
	
	var images = [];
	var old_src = [];
	
	function prepareImages ()
	{
		$('#gallery img').each(function(i) 
		{	
			var imgFile = $(this).attr('src');
			
			images[i] = $(this);
			old_src[i] = imgFile;
			
			var preloadImage = new Image();
			preloadImage.src = imgFile.replace('_grey','');
		
			$(this).hover(
				function() 
				{
					setSrc($(this), preloadImage.src);
				},
				function() 
				{
					if (curImage != i)
					{
						setSrc($(this), imgFile);
					}
				}); // end hover
			
			$(this).click(function(evt)
			{
				// dehighlight old image
				setSrc(images[curImage], old_src[curImage]);
				// set our current image
				curImage = i;
				// highlight our new image
				setSrc($(this), preloadImage.src);
			});
			
			setSrc = function(img, src)
			{
				img.attr('src', src);
			};
			
		}); // end each
		
		$('#gallery a').click(function(evt) 
		{
			evt.preventDefault();
			
			var imgPath = $(this).attr('href');
			var newImage = $('<img src="' + imgPath + '">');
			
			newImage.hide();
			
			$('#biggie').html(newImage);
			
			newImage.fadeIn(1000);
		}); // end of click
		
		$('#gallery a:first').click();
		$('#gallery img:first').click();
	}
}
