//
// Imager(iinfo) ;
//
// Writes to document at the current position a TABLE that contains a single
// cell with an image.  "iinfo" is a structure with the following values:
//
// img        The file name in which the image is stored.
// huge       The directory name in which huge versions of images are stored.
// large      The directory name in which large versions of images are stored.
// medium     The directory name in which medium versions of images are stored.
// small      The directory name in which small versions of images are stored.
// web        The directory name in which WEB versions of images are stored.
// cdrom      The directory name in which CDRom versions of images are stored.
// hwidth     The width of the image as it appears in the huge directory.
// hheight    The height of the image as it appears in the huge directory.
// lwidth     The width of the image as it appears in the large directory.
// lheight    The height of the image as it appears in the large directory.
// mwidth     The width of the image as it appears in the medium directory.
// mheight    The height of the image as it appears in the medium directory.
// swidth     The width of the image as it appears in the small directory.
// sheight    The height of the image as it appears in the small directory.
// wwidth     The width of the image as it appears in the web directory.
// wheight    The height of the image as it appears in the web directory.
// cwidth     The width of the image as it appears in the cdrom directory.
// cheight    The height of the image as it appears in the cdrom directory.
// alt        The ALT text to be included in the image's IMG tag.
// caption    The caption to be applied to the table.
// hidden     1 if the picture should be hidden behind a "[Reveal]" link,
//            0 otherwise.
//

// Replace() basically does the same thing as String.replace(), but without
// benefit of Javascript 1.2-only functionality such as regexes.

function Replace(str, from, to)
{
	var result = str ;
	var idx    = result.indexOf(from) ;
	
	while (idx != -1)
	{
		result = result.substring(0, idx) + to 
			   + result.substring(idx + from.length)  ;
		idx = result.indexOf(from) ;
	}
	
	return result ;
}

// The main Imager() function.

function Imager(iinfo)
{
	var winSize      = WindowSize(80, 100) ;
	var windowWidth  = winSize.width ;
	var windowHeight = winSize.height ;
	var imageWidth   = iinfo.swidth ;
	var imageHeight  = iinfo.sheight ;
	var dir          = iinfo.small ;

	if (! dir || windowWidth > 640)
	{
		if (iinfo.medium)
		{
			dir         = iinfo.medium ;
			imageWidth  = iinfo.mwidth ;
			imageHeight = iinfo.mheight ;
		}
	
		if (! dir || windowWidth > 800)
		{
			if (iinfo.large)
			{
				dir         = iinfo.large ;
				imageWidth  = iinfo.lwidth ;
				imageHeight = iinfo.lheight ;
			}

			if (! dir || windowWidth > 1024)
			{
				if (iinfo.huge)
				{
					dir         = iinfo.huge ;
					imageWidth  = iinfo.hwidth ;
					imageHeight = iinfo.hheight ;
				}
			}
		}
	}

	if (! dir)
	{
		dir         = iinfo.web ;
		imageWidth  = iinfo.wwidth ;
		imageHeight = iinfo.wheight ;
		if (! dir || windowWidth > 900)
		{
			if (iinfo.cdrom)
			{
				dir         = iinfo.cdrom ;
				imageWidth  = iinfo.cwidth ;
				imageHeight = iinfo.cheight ;
			}
		}
	}
		
	var image = new Image(imageWidth, imageHeight) ;
	image.src = dir + '/' + iinfo.img ;

	if (imageWidth > windowWidth)
	{
		var ratio   = windowWidth / imageWidth ;
		imageWidth  = windowWidth ;
		imageHeight = Math.round(imageHeight * ratio) ;
	}

	if (imageHeight > windowHeight)
	{
		var ratio   = windowHeight / imageHeight ;
		imageHeight = windowHeight ;
		imageWidth  = Math.round(imageWidth * ratio) ;
	}

	iinfo.caption = Replace(iinfo.caption, "&lt;",       "<") ;
	iinfo.caption = Replace(iinfo.caption, "&gt;",       ">") ;
	iinfo.caption = Replace(iinfo.caption, "&quot;",     '"') ;
	iinfo.caption = Replace(iinfo.caption, "&amp;",      "&") ;
	iinfo.caption = Replace(iinfo.caption, "__IMGDIR__", dir) ;
	
	document.write("<DIV ALIGN=CENTER>\n") ;
	document.write("  <TABLE BORDER=3>\n") ;
	document.write("    <CAPTION ALIGN=BOTTOM>\n") ;
	document.write("      " + iinfo.caption + "\n") ;
	document.write("    </CAPTION>\n") ;
	document.write("    <TR>\n") ;
	document.write("      <TD WIDTH=" + imageWidth + ">\n") ;
	
	var imagename = image.src ;
	if (iinfo.hidden)
	{
		imagename = imagename.replace('.jpg', '-hidden.jpg') ;
	}
	
	document.write("        <img src='" + imagename + "'\n") ;
	document.write("             width='" + imageWidth + "'\n") ;
	document.write("             height='" + imageHeight + "'\n") ;
	document.write("             align='middle'\n") ;
	document.write("             alt='" + iinfo.alt + "'\n") ;
	document.write("             id='theimage'\n") ;
	document.write("             onclick='theimage.src = \"" + image.src
															 + "\" ;'>\n") ;
	document.write("      </TD>\n") ;
	document.write("    </TR>\n") ;
	document.write("  </TABLE>\n") ;
	document.write("</DIV>\n") ;
	
	if (iinfo.hwidth && iinfo.hheight)
	{
		iinfo.maxWidth  = iinfo.hwidth ;
		iinfo.maxHeight = iinfo.hheight ;
	}
	else if (iinfo.cwidth && iinfo.cheight)
	{
		iinfo.maxWidth  = iinfo.cwidth ;
		iinfo.maxHeight = iinfo.cheight ;
	}
	else if (iinfo.lwidth && iinfo.lheight)
	{
		iinfo.maxWidth  = iinfo.lwidth ;
		iinfo.maxHeight = iinfo.lheight ;
	}
	else if (iinfo.wwidth && iinfo.wheight)
	{
		iinfo.maxWidth  = iinfo.wwidth ;
		iinfo.maxHeight = iinfo.wheight ;
	}
	else if (iinfo.mwidth && iinfo.mheight)
	{
		iinfo.maxWidth  = mwidth ;
		iinfo.maxHeight = mheight ;
	}
	else if (iinfo.swidth && iinfo.sheight)
	{
		iinfo.maxWidth  = iinfo.swidth ;
		iinfo.maxHeight = iinfo.sheight ;
	}
	
	iinfo.imageWidth   = imageWidth ;
	iinfo.imageHeight  = imageHeight ;
	iinfo.usedDir      = dir ;
}

function CanMakeLarger(iinfo)
{
	return (iinfo.imageWidth  < iinfo.maxWidth
		||  iinfo.imageHeight < iinfo.maxHeight) ;
}

function CanMakeSmaller(iinfo)
{
	return (iinfo.usedDir != "Small" && iinfo.usedDir != "Huge") ;
}
