/**
 * @author Marcel Werk
 * @copyright 2001-2007 WoltLab GmbH
 * @license GNU Lesser General Public License
 *          <http://opensource.org/licenses/lgpl-license.php>
 */
function ImageResizer() {
	this.defaultMaxWidth = 530;

	this.hideImages = function() {
		$$(".resizeImage").each(function(e) {
			e.hide();
			$(e).observe("load", function() {
				this.resize(e);
			}.bind(this));
		}.bind(this));
	}

	this.resize = function(e) {
		if (!INLINE_IMAGE_MAX_WIDTH) {
			INLINE_IMAGE_MAX_WIDTH = this.defaultMaxWidth;
		}

		var id = e.up("div.message");
		if (!id) {
			var portal = true;
			id = e.up("div.messageContent");
		}

		var imageWidth = e.width;
		var imageHeight = e.height;

		if (imageWidth > INLINE_IMAGE_MAX_WIDTH) {
			var w, h;
			if (!portal)
				w = INLINE_IMAGE_MAX_WIDTH;
			else
				w = INLINE_IMAGE_MAX_WIDTH - 30;

			h = Math.round(imageHeight * (INLINE_IMAGE_MAX_WIDTH / imageWidth));

			e.setStyle( {
				"width" : w + "px",
				"height" : h + "px"
			});

			// if (!this.isLinked(e)) {
			// var popupLink = document.createElement("a");
			// popupLink.className = 'externalURL';
			// popupLink.setAttribute('href', e.src);
			//
			// var postGroup = id.identify();
			//
			// popupLink.setAttribute("rel", "lightbox[" + postGroup + "]");
			// popupLink.appendChild(e.cloneNode(true));
			//
			// e.parentNode.replaceChild(popupLink, e);
			// }
		}

		e.show();
	}

	this.isLinked = function(node) {
		do {
			node = node.parentNode;
			if (node == undefined)
				break;
			if (node.nodeName == 'A')
				return true;
		} while (node.nodeName != 'TD' && node.nodeName != 'P' && node.nodeName != 'DIV' && node.nodeName != 'BODY');

		return false;
	}

	this.hideImages();
}

$(document).observe("dom:loaded", function() {
	new ImageResizer();
});

// adjust embedded images
onloadEvents.push(function() {
	$$('.embeddedAttachment').each(function(image) {
		image.setStyle( {
			width : 'auto',
			height : 'auto'
		});
	});
});

