_time = 0;

function resize(imgid, imgsrc, maxwidth, maxheight) {
	img = new Image();
	img.src = imgsrc;

	xsize = img.width;
	ysize = img.height;

	xratio = xsize / maxwidth;
	yratio = ysize / maxheight;

	ratio = (yratio >= xratio) ? yratio : xratio;
	if(ratio > 1) {
		newwidth = xsize/ratio;
		newheight = ysize/ratio;
	} else {
		newwidth = xsize;
		newheight = ysize;
	}


	clearTimeout(_time);
	if(newwidth == 0) {
		_time = setTimeout(eval("resize(" + imgid + ", " + imgsrc + ", " + maxwidth + ", " + maxheight+ ");"), 10);
		return;
	}

	document.images[imgid].width = newwidth;
	document.images[imgid].height = newheight;
	document.images[imgid].src = imgsrc;
}