document.write('<style>#mainimage img {visibility:hidden}</style>');

var selector = '#mainimage img';
var max_width = 800;
var max_height = 400;

function resizeMainImage() {
	

	
	if($(selector).height() != 0) {
		
	
	var width = new_width = $(selector).width();
	var height = new_height = $(selector).height();
	
		var ratio = (width / height);

		if (height == width) {
			var new_height = max_height;
			var new_width = max_height;
		} else if (width<=max_width && height<=max_height) {
		
		} else {
			var new_height = this.max_height;
			var new_width = (new_height * ratio);
		}
		
		$(selector).height(Math.round(new_height)).width(Math.round(new_width));
		
		$(selector).css({visibility:'visible'});
	
		clearInterval(0);


	}

	}

var thumbnailGallery = {
	
	numberToDisplay: 5,
	MoveAmount: 95,
	ThumbnailSize: 95, // arbitrary 
	RightLimit: 0,
	currentSelected: -1,
	numberOfThumbnails: 0,
	timeout: null,
	leftArrowBack: null,
	RightArrowBack: null,



	
	
	
	getSelected: function () {
		var thumbnails = $('#previewthumbsul').children('li')

		for (var i = 0; i < thumbnails.length; i++) { 
			if (thumbnails[i].className == 'current') {
				return i
			} 
		}

		return -1
	},

	moveLeft: function () {
		var left = parseInt($('#previewthumbsul').css('margin-left'))
		if (left < 0) {
			left += this.MoveAmount
			$('#previewthumbsul').css('margin-left', left + 'px')
			this.checkArrows()
		}
	},

	moveRight: function () { 
		var left = parseInt($('#previewthumbsul').css('margin-left'))
		if (left > this.RightLimit) {
			left -= this.MoveAmount
			$('#previewthumbsul').css('margin-left', left + 'px')
			this.checkArrows()
		}
	},
	
	checkArrows: function () {
		var left = parseInt($('#previewthumbsul').css('margin-left'))
		$('#arrowright').css('background-image', ( left == this.RightLimit ? 'none' : this.rightArrowBack))
		$('#arrowleft').css('background-image', ( left == 0 ? 'none' : this.leftArrowBack))
	
	},
	
	setup: function () { 	
		
		//alert('setup');
			
		this.numberOfThumbnails = $('#previewthumbsul').children('li').length
		
		$('#previewthumbs').css('overflow', 'hidden')	
		
		
		setInterval('resizeMainImage()',500)
	
		
		if (this.numberOfThumbnails > 5) {
			$('#previewthumbs').children('a.arrow').css('display', 'block')
		
			$('#previewthumbsul').css('width', (this.numberOfThumbnails * this.ThumbnailSize) + 'px')
			
			$('#previewthumbscont').css('overflow', 'hidden')			
			
			// Cache the left and right arrow images
			this.leftArrowBack  = $('#arrowleft').css('background-image')
			this.rightArrowBack = $('#arrowright').css('background-image')

			this.currentSelected = this.getSelected()						
			this.RightLimit      = ((this.numberOfThumbnails - this.numberToDisplay) * this.MoveAmount) * -1

			for (var i = 0; i < this.currentSelected; i++) {
				this.moveRight()
			}
		
			
		}
		
	
		this.checkArrows()
		
		
	}
	


	
	
}

$(document).ready(function (){
	
	thumbnailGallery.setup() 
	$('#arrowleft').click(function (){ thumbnailGallery.moveLeft() })
	$('#arrowright').click(function (){ thumbnailGallery.moveRight() })
	
	
});


