function resizeImage( item ) {
	var image = $(item);
	
	if( ! image.complete ) {
		image.setStyle( { display: 'none' } );
		window.setTimeout( function(){resizeImage(image);}, 5 );
		return;
	}
	image.setStyle( { display: 'inline' } );
	
	var parent = $(image.parentNode);

	while( parent.getStyle( "width" ) == null || parent.getStyle( "height" ) == null ) {
		parent = $(parent.parentNode);
		if( parent.nodeName == "BODY" ) {
			break;
		}
	} 
	var parentWidth = parent.getWidth();
	var parentHeight = parent.getHeight();
	
	var imageWidth = image.getWidth();
	var imageHeight = image.getHeight();
	
	var rate = Math.min( parentWidth / imageWidth, parentHeight /  imageHeight );
	
	var width = Math.floor( imageWidth * rate );
	var height = Math.floor( imageHeight * rate );
	
	if( ( parentWidth - width ) % 2 == 1 ) {
		width += 1;
	}
	if( ( parentHeight - height ) % 2 == 1 ) {
		height += 1;
	}
	
	image.setStyle( {
		width: width + 'px',
		height: height + 'px',
		marginTop: ( ( parentHeight - height ) / 2 ) + 'px',
		marginLeft: ( ( parentWidth - width ) / 2 ) + 'px'
	} );
}

function resizeAllImage() {
	var list = $$("img.autoResize");
	list.each( function(item) { resizeImage( item ); } );
}


