function max_image_size(img, width) {
	var ahref = img.parentNode;

	if (ahref && img && width) {
		if (img.width > width) {
			var height = img.height / (img.width / width);

			height = Math.round(height);
			width = Math.round(width);

			if (ahref.nodeName != "A") {
				// create the new elements
				var new_ahref = document.createElement('a');
				var new_image = document.createElement('img');

				// set the link attributes
				new_ahref.setAttribute("href", img.getAttribute("src"));
				new_ahref.setAttribute("class", "postlink");

				// set the image attributes
				new_image.setAttribute("src", img.getAttribute("src"));
				new_image.setAttribute("width", width);
				new_image.setAttribute("height", height);
				new_image.setAttribute("alt", "Image");
				new_image.setAttribute("title", "Image");
				new_image.setAttribute("border", "0");

				// make the image the child of the link
				new_ahref.appendChild(new_image);

				// switch the image for the new link
				ahref.replaceChild(new_ahref, img);

			} else {
				img.setAttribute("width", width);
				img.setAttribute("height", height);

			}

		}

	}

}
