window.onload   = resizeimg;
window.onresize = resizeimg;

// Adapted from: Open a window with the right dimension for an image
// http://www.rgagnon.com/jsdetails/js-0094.html
function popupFullImage(myimage) {
	// Create a new image to obtain its dimensions
	image = new Image();
	image.src = myimage;
	image.width  +=  10; // Account for frame borders
	image.height += 100; // Account for titlebar, address, status

	// Calculate the upper top-left position based on the screen and image width
	if (image.width < screen.width)
		leftPos = Math.round((screen.width - image.width) / 2);
	else
		leftPos = 0;
	if (image.height < screen.height)
		topPos  = Math.round((screen.height - image.height) / 2);
	else
		topPos = 0;

	// Prepare the HTML code for the new window
	html = "<HTML><HEAD><TITLE>" + myimage + "</TITLE>" + "</HEAD><BODY LEFTMARGIN=0 ";

	if (window.screen.availWidth < image.width || window.screen.availHeight < image.height) {
		html = html + "scroll='yes' "; // Image size exceeds desktop dimension so scrollbars are needed
		resize = 1; // In this situation the window is also resizable
	} else
		resize = 0; // Do not permit to resize

	html = html
	+ "MARGINWIDTH=0 TOPMARGIN=0 MARGINHEIGHT=0><CENTER>" 
	+ "<IMG SRC='" + myimage + "' BORDER=0 NAME=image " 
	+ 'onload="window.resizeTo(document.image.width, document.image.height);" '
	+ "onClick='window.close()' " // Click the image to close the window
	+ "style=\"cursor:hand\" " // Show the appropriate cursor
	+ ">"
	+ "</CENTER>" 
	+ "</BODY></HTML>";

	// Open the popup window at the specified positions
	popup = window.open('',
	'',
	'top='+topPos+',left='+leftPos+',toolbar=0,location=0,directories=0,menuBar=0,resizable='+resize+',statusBar=0');
	popup.document.open();
	popup.document.write(html);
	popup.document.focus();
	popup.document.close();
}

function resizeimg() {
	if (document.getElementsByTagName) {
		var imageElements = document.getElementsByTagName('img');
		for (i = 0; i < imageElements.length; i++) {
			im = imageElements[i];
			// Search parent for an acceptable width
			var parent = im.parentNode;
			var parentClientWidth;
			while (parent != document.body) { // Keep searching until the parent is document.body
				if (parent.tagName == 'BLOCKQUOTE') { // If blockquote is the parent, it means that the image is in a quote, resize the image to 200px (plus 6px for the border and padding)
					parentClientWidth = 206;
					break;
				} else if (parent.tagName == 'DIV' && parent.className == 'content') { // If the parent is a div with the class content, resize the picture to that width
					parentClientWidth = parent.clientWidth;
					break;
				}
				parent = parent.parentNode;
			}
			// Check if parentClientWidth is set and this image needs to be resized
			if (parentClientWidth != null && im.width > parentClientWidth) { 
				eval("pop" + String(i) + " = new Function(\"popupFullImage('" + im.src + "');\")");
				eval("im.onclick = pop" + String(i) + ";");
				if (document.all) im.style.cursor = 'hand';
				if (!document.all) im.style.cursor = 'pointer';
				im.style.padding = "2px";
				im.style.marginBottom = "2px";
				im.style.border = "1px dashed #FF0000";
				var allowedWidth = parentClientWidth - 6; // 6px is needed for the padding and border
				im.style.width = allowedWidth + 'px';
			}
		}
	}
}
