var pic_window = null;
var popup_image = null;

/*
- popupImage will try to open a little window with no controls or decorations.
  It will return its success at opening the little window.
- drawPopup will then either put the image in the popup and resize it or put an
  error message in it.
*/
function popupImage( img ) {
	pic_window = open( img, "pw", "scrollbars=no,menubar=no,status=no,titlebar=no,toolbar=no,width=100,height=100,resizable=yes" );

	popup_image = new Image();
	popup_image.onload = drawPopup;
	popup_image.onerror = popupImageError;
	popup_image.src = img;

	return pic_window;
} // popupImage

function drawPopup( ev ) {
	if ( pic_window == null ) {
		// little window wasn't opened successfully
		alert( "pic_window is null!" );
		return false;
	} // if

	var w = popup_image.width;
	var h = popup_image.height;
	var s = popup_image.src;
	with ( pic_window.document ) {
		open();
		writeln( "<html><head>" );
		writeln( "<script type='text/javascript'>" );
		writeln( "function resz() {" );
		writeln( "  window.resizeTo("+(w+12)+","+(h+37)+");" );
		writeln( "  window.innerWidth="+w+"; window.innerHeight="+h+";" );
		writeln( "  window.scrollbars=false;" );
		writeln( "}" );
		writeln( "</script></head>" );
		writeln( "<body style='margin: 0px; padding: 0px; text-align: center; vertical-align: middle' onload='resz()'>" );
		writeln( "<img src='"+popup_image.src+"' width='"+w+"' height='"+h+"'>" );
		writeln( "</body></html>" );
		close();
	} // with
	pic_window.focus();
} // doPopup

function popupImageError( ev ) {
	with ( pic_window.document ) {
		open();
		writeln( "<html><head>" );
		writeln( "<script type='text/javascript'>" );
		writeln( "function resz() {" );
		writeln( "  window.resizeTo(400,300);" );
		writeln( "}" );
		writeln( "</script></head>" );
		writeln( "<body onload='resz()'>" );
		writeln( "<h2 style='color: red'>File not found</h2>" );
		writeln( "</body></html>" );
		close();
	} // with
	pic_window.focus();
} // popupImageError
