otoOlcumleme.IMAGE_ID_BASE = 'ncode_imageresizer_container_';
otoOlcumleme.WARNING_ID_BASE = 'ncode_imageresizer_warning_';

function otoOlcumleme(id, img) {
	this.id = id;
	this.img = img;
	this.originalWidth = 0;
	this.originalHeight = 0;
	this.warning = null;
	this.warningTextNode = null;
	
	img.id = otoOlcumleme.IMAGE_ID_BASE+id;
}

otoOlcumleme.getNextId = function() {
	id = 1;
	while(document.getElementById(otoOlcumleme.IMAGE_ID_BASE+id) != null) {
		id++;
	}
	return id;
}

otoOlcumleme.createOn = function(img) {
	isRecovery = false; // if this is a recovery from QuickEdit, which only restores the HTML, not the OO structure
	if(img.id && img.id.indexOf(otoOlcumleme.IMAGE_ID_BASE) == 0 && document.getElementById(otoOlcumleme.WARNING_ID_BASE+img.id.substr(otoOlcumleme.IMAGE_ID_BASE.length)) != null) {
		newid = img.id.substr(otoOlcumleme.IMAGE_ID_BASE.length);
		resizer = new otoOlcumleme(newid, img);
		isRecovery = true;
		resizer.restoreImage();
	} else {
		newid = otoOlcumleme.getNextId();
		resizer = new otoOlcumleme(id, img);
	}
	
	if (resizer.originalWidth == 0) resizer.originalWidth = img.width;
	if (resizer.originalHeight == 0) resizer.originalHeight = img.height;
	
	if((otoOlcumleme.MAXWIDTH > 0 && resizer.originalWidth > otoOlcumleme.MAXWIDTH) || (otoOlcumleme.MAXHEIGHT > 0 && resizer.originalHeight > otoOlcumleme.MAXHEIGHT)) {
		if(isRecovery) {
			resizer.reclaimWarning(warning);
		} else {
			resizer.createWarning();
		}
		resizer.scale();
	}
}

otoOlcumleme.prototype.restoreImage = function() {
	newimg = document.createElement('IMG');
	newimg.src = this.img.src;
	this.img.width = newimg.width;
	this.img.height = newimg.height;
}

otoOlcumleme.prototype.reclaimWarning = function() {
	warning = document.getElementById(otoOlcumleme.WARNING_ID_BASE+newid);
	
	this.warning = warning;
	this.warningTextNode = warning.firstChild.firstChild.childNodes[1].firstChild;
	this.warning.resize = this;
	
	this.scale();
}

otoOlcumleme.prototype.createWarning = function() {
	mtable = document.createElement('TABLE');
	mtbody = document.createElement('TBODY');
	mtr = document.createElement('TR');
	mtd1 = document.createElement('TD');
	mtd2 = document.createElement('TD');
	mimg = document.createElement('IMG');
	mtext = document.createTextNode('');
	
	mimg.src = '';
	mimg.width = 0;
	mimg.height = 0;
	mimg.alt = '';
	mimg.border = 0;
	
	//mtd1.width = 20;
	//mtd1.className = 'viyk1';
	
	//mtd2.unselectable = 'on';
	//mtd2.className = 'viyk2';
	
	mtable.className = '';
	mtable.textNode = mtext;
	mtable.resize = this;
	mtable.id = otoOlcumleme.WARNING_ID_BASE+this.id;
	
	//mtd1.appendChild(mimg);
	//mtd2.appendChild(mtext);
	
	//mtr.appendChild(mtd1);
	//mtr.appendChild(mtd2);
	
	//mtbody.appendChild(mtr);
	
	//mtable.appendChild(mtbody);
	
	this.img.parentNode.insertBefore(mtable, this.img);
	
	this.warning = mtable;
	this.warningTextNode = mtext;
}

otoOlcumleme.prototype.scale = function() {
	if(otoOlcumleme.MAXWIDTH > 0 && this.originalWidth > otoOlcumleme.MAXWIDTH) {
		resized = true;
		this.img.width = otoOlcumleme.MAXWIDTH;
		this.img.height = (otoOlcumleme.MAXWIDTH / this.originalWidth) * this.originalHeight;
	}
	if(otoOlcumleme.MAXHEIGHT > 0 && this.originalHeight > otoOlcumleme.MAXHEIGHT) {
		resized = true;
		this.img.height = otoOlcumleme.MAXHEIGHT;
		this.img.width = (otoOlcumleme.MAXHEIGHT / this.originalHeight) * this.originalWidth;
	}
	
	this.warning.width = this.img.width;
	this.warning.onclick = function() { return this.resize.unScale(); }
	

	
	return false;
}

otoOlcumleme.prototype.unScale = function() {
	switch(otoOlcumleme.MODE) {
		case 'samewindow':
			window.open(this.img.src, '_self');
			break;
		case 'newwindow':
			window.open(this.img.src, '_blank');
			break;
		case 'enlarge':
		default:
			this.img.width = this.originalWidth;
			this.img.height = this.originalHeight;
			this.img.className = 'ncode_imageresizer_original';
			if(this.warning != null) {
				this.warningTextNode.data = vbphrase['ncode_imageresizer_warning_fullsize'];
				this.warning.width = this.img.width;
				this.warning.onclick = function() { return this.resize.scale() };
			}
			break;
	}
	
	return false;
}