function hideDialog()
{
	var popup = document.getElementById('popupdiv');
	if (popup)
	{
		popup.style.display = 'none';
	}
}

function popupimageLoaded(image)
{
	var popup = document.getElementById('popupdiv');
	if (popup && image)
	{
		popup.style.cursor = 'default';
		image.style.visibility = 'visible';
	}
}

function enlargeDialog(exampleid, width, height)
{
	var enlargediv = document.getElementById('enlargediv');
	if (enlargediv)
	{
		enlargediv.className = 'enlargediv enlargediv-hidden';
	}

	var popup = document.getElementById('popupdiv');
	var image = document.getElementById('popupimage');
	if (popup && image)
	{
		popup.style.cursor = 'wait';
		image.style.visibility = 'hidden';
		image.width = width;
		image.height = height;
		image.src = 'showexample.php?exampleid=' + exampleid + '&height=' + height;
	}
}

function showImageDialog(image)
{
	var maxwidth = image.getAttribute('maxwidth');
	var maxheight = image.getAttribute('maxheight');
	var exampleid = image.getAttribute('exampleid');

	var dim = document.viewport.getDimensions();
	var perc80width = Math.round(dim.width * 0.8);
	var perc80height = Math.round(dim.height * 0.8);

	var fx = (perc80width >= maxwidth)?1:(perc80width/maxwidth);
	var fy = (perc80height >= maxheight)?1:(perc80height/maxheight);

	var	smallWidth = maxwidth;
	var	smallHeight = maxheight;
	var scaled = (Math.min(fx, fy) < 1);
	if (scaled)
	{
		if (fx < fy)
		{
			smallWidth = perc80width;
			smallHeight = Math.round(maxheight * fx);
		}
		else
		{
			smallWidth = Math.round(maxwidth * fy);
			smallHeight = perc80height;
		}
	}

	var enlargediv = document.getElementById('enlargediv');
	if (enlargediv)
	{
		enlargediv.onclick = function() { enlargeDialog(exampleid, maxwidth, maxheight); };
		enlargediv.className = 'enlargediv enlargediv-' + (scaled?'visible':'hidden');
	}

	var image = document.getElementById('popupimage');
	var popup = document.getElementById('popupdiv');
	if (popup && image)
	{
		popup.style.cursor = 'wait';
		popup.style.marginTop = document.body.scrollTop + 'px';
		popup.style.marginLeft = document.body.scrollLeft + 'px';
		popup.style.display = 'block';

		image.style.visibility = 'hidden';
		image.width = smallWidth;
		image.height = smallHeight;
		image.src = 'showexample.php?exampleid=' + exampleid + '&height=' + smallHeight;
	}
}
