
ImageResizer.MODE = "_blank";
ImageResizer.AREA_ID = "ugc-image-autoresize";
ImageResizer.MAXWIDTH = 500;
ImageResizer.MAXHEIGHT = 0;
ImageResizer.WARNING_SMALL = "wurst1";
ImageResizer.WARNING_FILESIZE = "wurst2";
ImageResizer.WARNING_NOFILESIZE = "wurst3";
ImageResizer.WARNING_FULLSIZE = "wurst4";


ImageResizer.IMAGE_ID_BASE = 'imageresizer_container_';
ImageResizer.WARNING_ID_BASE = 'imageresizer_warning_';
ImageResizer.scheduledResizes = [];

function ImageResizer(id, img) {
	this.id = id;
	this.img = img;
	this.originalWidth = 0;
	this.originalHeight = 0;
	this.warning = null;
	this.warningTextNode = null;
	this.originalWidth = img.originalWidth;
	this.originalHeight = img.originalHeight;
	img.id = ImageResizer.IMAGE_ID_BASE+id;
}

ImageResizer.appendHandling = function () {
	var el = document.getElementById(ImageResizer.AREA_ID);
	if (typeof el == 'undefined') {
		return false;
	}
	var imglist = el.getElementsByTagName('img');
	for (var i = 0, c = imglist.length; i < c; i++) {
		ImageResizer.createOn(imglist[i]);
	}
}

ImageResizer.executeOnload = function() {
	var rss = ImageResizer.scheduledResizes;
	for(var i = 0; i  < rss.length; i++) {
		rss[i].title = 'onload';
		ImageResizer.createOn(rss[i], true);
	}
}

ImageResizer.schedule = function(img) {
	if(ImageResizer.scheduledResizes.length == 0) {
		if(window.addEventListener) {
			window.addEventListener('load', ImageResizer.executeOnload, false);
		} else if(window.attachEvent) {
			window.attachEvent('onload', ImageResizer.executeOnload);
		}
	}
	ImageResizer.scheduledResizes.push(img);
}

ImageResizer.getNextId = function() {
	var id = 1;
	while(document.getElementById(ImageResizer.IMAGE_ID_BASE+id) != null) {
		id++;
	}
	return id;
}

ImageResizer.createOnId = function(id) {
	return ImageResizer.createOn(document.getElementById(id));
}

ImageResizer.createOn = function(img, isSchedule) {
	if(typeof isSchedule == 'undefined') {
		isSchedule = false;
	}
	if(!img || !img.tagName || img.tagName.toLowerCase() != 'img') {
		alert(img+' is not an image ('+img.tagName.toLowerCase()+')');
	}
	if(img.width == 0 || img.height == 0) {
		if(!isSchedule)
			ImageResizer.schedule(img);
		return;
	}
	if(!img.originalWidth) img.originalWidth = img.width;
	if(!img.originalHeight) img.originalHeight = img.height;
	if((ImageResizer.MAXWIDTH > 0 && img.originalWidth > ImageResizer.MAXWIDTH)
		|| (ImageResizer.MAXHEIGHT > 0 && img.originalHeight > ImageResizer.MAXHEIGHT)) {
		var newid, resizer;
		if(img.id && img.id.indexOf(ImageResizer.IMAGE_ID_BASE) == 0) {
			newid = img.id.substr(ImageResizer.IMAGE_ID_BASE.length);
			if(document.getElementById(ImageResizer.WARNING_ID_BASE+newid) != null) {
				resizer = new ImageResizer(newid, img);
				isRecovery = true;
				resizer.restoreImage();
			}
		} else {
			newid = ImageResizer.getNextId();
			resizer = new ImageResizer(newid, img);
		}
		resizer.createWarning();
		resizer.scale();
	}
}

ImageResizer.prototype.restoreImage = function() {
	newimg = document.createElement('img');
	newimg.src = this.img.src;
	this.img.width = newimg.width;
	this.img.height = newimg.height;
}

ImageResizer.prototype.createWarning = function() {
	this.img.resize = this;
	this.warning = this.img;
/*
	var mtable = document.createElement('table');
	var mtbody = document.createElement('tbody');
	var mtr = document.createElement('tr');
	var mtdimage = document.createElement('td');
	var mtdtext = document.createElement('td');
	var mimg = document.createElement('img');
	var mtext = document.createTextNode('');
	mimg.src = '/bilder/shared/base/error.png';
	mimg.width = 16;
	mimg.height = 16;
	mimg.alt = '';
	mimg.border = 0;
	mtdimage.width = 20;
	mtdimage.className = 'tdimage';
	mtdtext.unselectable = 'on';
	mtdtext.className = 'tdtext';
	mtable.className = 'imageresizer_warning';
	mtable.textNode = mtext;
	mtable.resize = this;
	mtable.id = ImageResizer.WARNING_ID_BASE+this.id;
	mtdimage.appendChild(mimg);
	mtdtext.appendChild(mtext);
	mtr.appendChild(mtdimage);
	mtr.appendChild(mtdtext);
	mtbody.appendChild(mtr);
	mtable.appendChild(mtbody);
	this.img.parentNode.insertBefore(mtable, this.img);
	this.warning = mtable;
	this.warningTextNode = mtext;
*/
}

ImageResizer.prototype.setText = function(text) {
	var newnode = document.createTextNode(text);
	this.warningTextNode.parentNode.replaceChild(newnode, this.warningTextNode);
	this.warningTextNode = newnode;
}

ImageResizer.prototype.scale = function() {
	this.img.height = this.originalHeight;
	this.img.width = this.originalWidth;

	if(ImageResizer.MAXWIDTH > 0 && this.img.width > ImageResizer.MAXWIDTH) {
		this.img.height = (ImageResizer.MAXWIDTH / this.img.width) * this.img.height;
		this.img.width = ImageResizer.MAXWIDTH;
	}

	if(ImageResizer.MAXHEIGHT > 0 && this.img.height > ImageResizer.MAXHEIGHT) {
		this.img.width = (ImageResizer.MAXHEIGHT / this.img.height) * this.img.width;
		this.img.height = ImageResizer.MAXHEIGHT;
	}

	this.warning.width = this.img.width;
	this.warning.onclick = function() { return this.resize.unScale(); }

/*
	if(this.img.width < 450) {
		this.setText(ImageResizer.WARNING_SMALL);
	} else if(this.img.fileSize && this.img.fileSize > 0) {
		this.setText(ImageResizer.WARNING_FILESIZE.replace('%1$s', this.originalWidth).replace('%2$s', this.originalHeight).replace('%3$s', Math.round(this.img.fileSize/1024)));
	} else {
		this.setText(ImageResizer.WARNING_NOFILESIZE.replace('%1$s', this.originalWidth).replace('%2$s', this.originalHeight));
	}
*/

	return false;
}

ImageResizer.prototype.unScale = function() {
	switch(ImageResizer.MODE) {
		case '_self':
			window.open(this.img.src, '_self');
			break;
		case '_blank':
			window.open(this.img.src, '_blank');
			break;
		case '_scale':
		default:
			this.img.width = this.originalWidth;
			this.img.height = this.originalHeight;
			this.img.className = 'imageresizer_original';
			if(this.warning != null) {
//				this.setText(ImageResizer.WARNING_FULLSIZE);
				this.warning.width = this.img.width;
				this.warning.onclick = function() { return this.resize.scale() };
			}
			break;
	}

	return false;
}

if(window.addEventListener)
{
	window.addEventListener('load', ImageResizer.appendHandling, false);
}
else if(window.attachEvent)
{
	window.attachEvent('onload', ImageResizer.appendHandling);
}