AGISImgArray = new Array();
AGISImgCount = 0;

function AGISLoadImage(imgSrc, elementID, w, h, hyperlink)
{
	if (document.images && (document.all || document.layers))
	{
		// preload image
		AGISImgArray[AGISImgCount] = new Image();
		AGISImgArray[AGISImgCount].src = imgSrc;
		// after image loading, get width and height for resizing
		AGISImgArray[AGISImgCount].onLoad = AGISResizeImg(AGISImgCount, elementID, w, h,hyperlink);
		AGISImgCount++;
	}
	else // not supported
	{
		if (hyperlink)
			document.write('<a href="' + hyperlink + '">');
		document.write('<img src="' + imgSrc + '" border=0' + (w ? ' width=' + w : "") + (h ? ' height=' + h : "") + '>');
		if (hyperlink)
			document.write('</a>');
	}
}

function AGISResizeImg(imgCount, elementID, w, h, hyperlink)
{
	var img = AGISImgArray[imgCount];
	// resize proporationly
	w = w ? w : 520;
	h = h ? h : 450;
	
	var imgW = img.width;
	var imgH = img.height;

	if(imgW > w || imgH > h || imgW == 0 || imgH == 0)
	{
		if (imgW == 0 || imgH == 0)
		{
			window.setTimeout("AGISResizeImg(" + imgCount + ",'" + elementID + "'," + w + "," + h + (hyperlink ? ",'" + hyperlink + "'" : "") + ")", 500);
			return;
		}
	
		if (imgW / w > imgH / h)
		{	
			imgH = Math.round(imgH / (imgW / w));
			imgW = w;
		} 
		else
		{
			imgW = Math.round(imgW / (imgH / h));
			imgH = h;
		}
	}

	var imgHTML = "";
	if (hyperlink)
		imgHTML = '<a href="' + hyperlink + '">';
	imgHTML += '<img src="' + img.src + '" border=0' + ' width=' + imgW + ' height=' + imgH+ '>';
	if (hyperlink)
		imgHTML += '</a>';

	if (document.layers)
	{
		document.layers[elementID].visibility='hide';
		document.layers[elementID].document.open();
		document.layers[elementID].document.write(imgHTML);
		document.layers[elementID].document.close();
//		window.setTimeout("document.layers['" + elementID + "'].visibility='show'",100);
	}
	else if (document.all)
	{
		document.all[elementID].innerHTML = imgHTML;
	}
}
