/**
 * @author	Marcel Werk (redesigned C.T.)
 * @copyright	2001-2007 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 */
function ImageResizer() {
	var i, 
	images = document.images,
	imgMax = images.length;
	this.defaultMaxWidth = 530;
	
	this.resize = function() {
		if (!INLINE_IMAGE_MAX_WIDTH) {
			INLINE_IMAGE_MAX_WIDTH = this.defaultMaxWidth;
		}
		
		for (i = 0; i < imgMax ;i++){
			if (images[i].className == 'resizeImage') {
				var imageWidth = images[i].width;
				var imageHeight = images[i].height;				
				
				if (imageWidth > INLINE_IMAGE_MAX_WIDTH) {
					images[i].width = INLINE_IMAGE_MAX_WIDTH;
					images[i].height = Math.round(imageHeight * (INLINE_IMAGE_MAX_WIDTH / imageWidth));
					
					if (!this.isLinked(images[i])) {
						var popupLink = document.createElement("a");
						popupLink.className = 'externalURL';
						popupLink.setAttribute('href', images[i].src);
						popupLink.appendChild(images[i].cloneNode(true));
						
						images[i].parentNode.replaceChild(popupLink, images[i]);
					}
				}
				images[i].style.display = "block";
			}
		}
	}
	
	this.isLinked = function(node) {
		do {
			node = node.parentNode;
			if (node == undefined) break;
			if (node.Name == 'A') return true;
		}
		while (node.Name != 'TD' && node.Name != 'P' && node.Name != 'DIV' && node.Name != 'BODY');
			
		return false;
	}
	
	this.resize();
}

onloadEvents.push(function() { new ImageResizer(); });
// adjust embedded images
onloadEvents.push(function() {
	$$('.embeddedAttachment').each(function(image) {
		image.setStyle({
			width: 'auto',
			height: 'auto'
		});
	});	
});
