// Written by Ian Henderson in 2005.  Copy and modify freely, as long as this line is present and unmodified.

var maxWidth = 256;
var maxHeight = 256;

function scaleImage(image)
{
	var widthRatio;
	var heightRatio;
	var width = image.offsetWidth;
	var height = image.offsetHeight;
	var newWidth;
	var newHeight;
	if (width > maxWidth || height > maxHeight) {
		widthRatio = maxWidth / width;
		heightRatio = maxHeight / height;
		if (widthRatio < heightRatio) {
			newWidth = width * widthRatio;
			newHeight = height * widthRatio;
		} else {
			newWidth = width * heightRatio;
			newHeight = height * heightRatio;
		}
		image.style.width = newWidth + "px";
		image.style.height = newHeight + "px";
		image.onclick = function () {
			image.style.width = width + "px";
			image.style.height = height + "px";
			image.onclick = function () { scaleImage(image); }
		}
	}
}

function scaleImages()
{
	var images = document.getElementsByTagName("img");
	for (var i=0; i<images.length; i++) {
		scaleImage(images[i]);
	}
}
