
function AutoResizeImage( imgId, containerId, loadBoxId )
{
	var imgObj = document.getElementById( imgId );
	var container = document.getElementById( containerId );
	var loadBox = document.getElementById( loadBoxId );
	
	if( imgObj )
	{
		var sourceSrc = imgObj.src;
		var sourceWidth = imgObj.clientWidth;
		var sourceHeight = imgObj.clientHeight;
		
		if( container )
		{
			var targetWidth = container.clientWidth;
			var targetHeight = container.clientHeight;
		}
		else
		{
			var targetWidth = 100;
			var targetHeight = 100;
		}
		
		if( sourceWidth == 0 || sourceHeight == 0 || targetWidth == 0 || targetHeight == 0 )
		{
			setTimeout("AutoResizeImage(\"" + imgId + "\", \"" + containerId + "\", \"" + loadBoxId + "\")", 1000);
			return;
		}
		
		//alert("sourceSrc = " + sourceSrc + ";\n sourceWidth = " + sourceWidth + ";\n sourceHeight = " + sourceHeight + ";\n targetWidth = " + targetWidth + ";\n targetHeight = " + targetHeight + ";\n");
		
		if( sourceWidth > targetWidth || sourceHeight > targetHeight )
		{
			if( sourceWidth / sourceHeight > targetWidth / targetHeight )
			{
				imgObj.width = parseInt(targetWidth);
				//alert(sourceHeight + " / (" + sourceWidth + " / " + targetWidth + ") = " + parseInt(sourceHeight / (sourceWidth / targetWidth)));
				imgObj.height = parseInt(sourceHeight / (sourceWidth / targetWidth));
				imgObj.vspace = parseInt((targetHeight - imgObj.height)/2);
			}
			else
			{
				imgObj.height = parseInt(targetHeight);
				//alert(sourceWidth + " / (" + sourceHeight + " / " + targetHeight + ") = " + parseInt(sourceWidth / (sourceHeight / targetHeight)));
				imgObj.width = parseInt(sourceWidth / (sourceHeight / targetHeight));
				imgObj.hspace = parseInt((targetWidth - imgObj.width)/2);
			}
		}
		else
		{
			//alert("OK");
			imgObj.vspace = parseInt((targetHeight - sourceHeight)/2);
			imgObj.hspace = parseInt((targetWidth - sourceWidth)/2);
		}
		//alert("OK")
		
		if(loadBox) loadBox.style.display = "none";
		imgObj.style.visibility = "visible";
	}
}