
var pr_maxTries = 100;
var pr_tryDelay = 100;
var pr_containerWidthDiff = 20;
var pr_waitingElems = new Array();
var pr_pics = new Array();
var pr_picClassName = 'forumPic';

function resizeObject(elem, maxWidth, maxHeight, tries) {
	if (isNaN(elem.complete) || (elem.complete&&elem.offsetWidth>0) || tries >= pr_maxTries) {
		newWidth = elem.offsetWidth;
		if (newWidth > maxWidth) {newWidth = maxWidth;}
		if (newWidth > elem.parentNode.offsetWidth-pr_containerWidthDiff) {newWidth = elem.parentNode.offsetWidth-pr_containerWidthDiff;}
		if (newWidth != elem.offsetWidth) {
			scale = newWidth / elem.offsetWidth;
			oldHeight = elem.offsetHeight;
			elem.width = newWidth;
			elem.height = Math.ceil(oldHeight * scale);
			subEmbed = elem.getElementsByTagName('embed')[0];
			if (subEmbed) {
				subEmbed.width = newWidth;
				elem.height = Math.ceil(oldHeight * scale);
			}
		}
		if (maxHeight && elem.offsetHeight > maxHeight) {
			scale = maxHeight / elem.offsetHeight;
			oldWidth = elem.offsetWidth;
			elem.height = maxHeight;
			elem.width = Math.ceil(newWidth * scale);
		}
	}
	else {
		if (!tries) tries = 0;
		if (tries >= pr_maxTries) return;
		setTimeout(function() {resizeObject(elem, maxWidth, maxHeight, tries+1);}, pr_tryDelay);
	}
}

function resizeObjects(className, maxWidth, maxHeight) {
	if (!maxHeight) maxHeight = 0;
	if (document.getElementsByClassName) {
		elems = document.getElementsByClassName(className);
	}
	else {
		oElems = document.getElementsByTagName('object');
		elems = new Array();
		i2 = 0;
		for (i = 0; i < oElems.length; i++) {
			if (oElems[i].className == className) {elems[i2] = oElems[i];i2++;}
		}
		pElems = document.getElementsByTagName('img');
		for (i = 0; i < pElems.length; i++) {
			if (pElems[i].className == className) {elems[i2] = pElems[i];i2++;}
		}
	}
	if (className == pr_picClassName) {
		for (i = 0; i < elems.length; i++) {
			//pr_pics[i] = new Image();
			//pr_pics[i].src = elems[i].src;
			//elems[i].id = className + i;
			//elems[i].src = '/images/loadingPic.jpg';
			//elems[i].style.display = 'inline';
			resizeObject(elems[i], maxWidth, maxHeight, 0);
		}
	}
	else {
		for (i = 0; i < elems.length; i++) {
			resizeObject(elems[i], maxWidth, maxHeight, 0);
		}
	}
}

var pr_img = new Array();
var pr_urlLoading = '/images/loadingPic.jpg';
var pr_classLoading = 'loadingBgrnd2';
var pr_classNotFound = 'picNotFound';
var pr_urlSpacer = '/images/spacer.gif';
var pr_maxWidth = 600;
var pr_maxHeight = 500;

function pr_getNewSize(w, h, mw, mh) {
	if (!mw) mw = pr_maxWidth;
	if (!mh) mh = pr_maxHeight;
	var s = {w:w, h:h, ar:w/h};
	if (s.w > mw) {s.w = mw;s.h = Math.round(s.w / s.ar);}
	if (s.h > mh) {s.h = mh;s.w = Math.round(s.h * s.ar);}
	return s;
}

function pr_onload() {
	$(this).attr('pr_loading', '2');
	this.onload = null;
	printDebug('PR: Pic '+$(this).attr('pr_id')+' ready ('+this.src+')...');
	var pw = $(this).parent().outerWidth()-20;
	var s = pr_getNewSize($(this).width(), $(this).height(), pw<pr_maxWidth?pw:pr_maxWidth);
	$(this).height(s.h).width(s.w).attr('oldSrc', $(this).attr('src')).attr('src', pr_urlSpacer).show().addClass(pr_classLoading);
}

function pr_loaded() {
	$(this).attr('pr_loading', '3');
	$(this).unbind('load');
	if (this.onload) this.onload();
	printDebug('PR: Pic '+$(this).attr('pr_id')+' loaded ('+this.src+')...');
	$(this).replaceWith($(this).clone(true).attr('pr_loading', '4').hide().removeClass(pr_classLoading).attr('src', $(this).attr('oldSrc')).fadeIn(500));
}

function resizePics(className, maxWidth, maxHeight) {
	printDebug('PR: Starting Resizing...');
	$('.'+className).each(function() { 
		if ($(this).attr('pr_loading')) return;
		$(this).attr('pr_loading', '1');
		this.onload = pr_onload; 
		$(this).load(pr_loaded);
		if (this.complete && $(this).attr('pr_loading')!='3') $(this).load();
	});
}
