var zoomStep = 1.25;

function openPopup (name, posX, posY, reqWidth, reqHeight, event)
{
	var popupWindow;

	popupWindow = window.open (
		name,
		'Image',
		'left=' + posX + ',' +
		'top=' + posY + ',' +
		'width=' + reqWidth + ',' +
		'height=' + reqHeight + ',' +
		'channelmode=no,' +
		'directories=yes,' +
		'fullscreen=no,' +
		'location=no,' +
		'menubar=no,' +
		'resizable=yes,' +
		'scrollbars=yes,' +
		'status=yes,' +
		'titlebar=yes,' +
		'toolbar=no',
		false
	);
	if ('focus' in popupWindow) {
		popupWindow.focus ();
	}
	if ('preventDefault' in event) {
		event.preventDefault ();
	} else if ('returnValue' in event) {
		event.returnValue = false;
	}
}

function zoomIn ()
{
	if (zoomCheckIncrease () == false) {
		return;
	}
	++currentZoom;
	updateWindowSize ();
}

function zoomOut ()
{
	--currentZoom;
	updateWindowSize ();
}

function zoomCheckIncrease ()
{
	var dims, zoomOld, zoomNew, extra, newWidth, newHeight;

	dims = getBodyDimensions ();
	zoomOld = Math.pow (zoomStep, currentZoom);
	zoomNew = Math.pow (zoomStep, currentZoom + 1);
	extra = getBrowserExtra ();
	newWidth = dims ['width'] - (initialWidth * zoomOld) + (initialWidth * zoomNew) + extra ['width'];
	newHeight = dims ['height'] - (initialHeight * zoomOld) + (initialHeight * zoomNew) + extra ['height'];
	return (
		(newWidth <= screen.availWidth) &&
		(newHeight <= screen.availHeight)
	);
}

function updateWindowSize ()
{
	var zoomFactor, newWidth, newHeight, domStyle, domObj;

	zoomFactor = Math.pow (zoomStep, currentZoom);
	newWidth = initialWidth * zoomFactor;
	newHeight = initialHeight * zoomFactor;
	domObj = document.getElementById ('main_image');
	domObj.style.width = newWidth + 'px';
	domObj.style.height = newHeight + 'px';
	if ((domObj = document.getElementById ('main_description')) !== null) {
		domObj.style.width = newWidth + 'px';
	}
	if (window.opener) {
		setTimeout (adjustPopupDimensions, 0);
	}
}

function adjustPopupDimensions ()
{
	var dims, extra, width, height;

	dims = getBodyDimensions ();
	extra = getBrowserExtra ();
	width = dims ['width'] + extra ['width'];
	height = dims ['height'] + extra ['height'];
	if (width > screen.availWidth) {
		width = screen.availWidth;
	}
	if (height > screen.availHeight) {
		height = screen.availHeight;
	}
	window.resizeTo (width, height);
}

function getBodyDimensions ()
{
	return ({
		width: document.getElementById ('main_image').offsetWidth,
		height:
			(document.body.scrollHeight > document.body.offsetHeight) ?
			document.body.scrollHeight :
			document.body.offsetHeight
	});
}

function getBrowserExtra ()
{
	var msie;

	msie = (navigator.appName == 'Microsoft Internet Explorer');
	return ({
		width: msie ? 50 : 40,
		height: msie ? 115 : 75
	});
}
