function getWindowWidth(win)
{
	var win = win || window;

	if (!isNaN(win.innerWidth))
		return win.innerWidth;

	if (win.document.body && !isNaN(win.document.body.clientWidth))
		return win.document.body.clientWidth;

	return 0;
}

function getWindowHeight(win)
{
	var win = win || window;
	var height = 0;

	if (!isNaN(win.innerHeight))
		return win.innerHeight;

	if (win.document.body && !isNaN(win.document.body.clientHeight))
		return win.document.body.clientHeight;

	return 0;
}

/**
 * Open a window centered in available screen width
 *
 * @param string	URL
 * @param integer	window width
 * @param integer	window height
 * @param string	optional, additional features as used in window.open() method
 * @param string	optional, window name
 * @return object	reference to opened window, if a window with this name exists,
 *					a reference to it will be returned
 */
function openCenteredWindow(url, width, height, features, name)
{
	if (!url || !width || !height)
		return false;

	var left = Math.floor((screen.width - width) / 2);
	var top = Math.floor((screen.height - height) / 2);
	var params = 'top=' + top + ',left=' + left + ',height=' + height + ',width=' + width;
	if (features)
		params += ',' + features;
	var name = (name ? name : '');
	var win = window.open(url, name, params);
	if (parseInt(navigator.appVersion) >= 4)
		win.focus();
	return win;
}

/**
 * Opens a window and writes an image element to its document.
 * Window resizes to image dimensions.
 *
 * @param String	URL for image file
 * @param String	optional, document title
 * @param String	optional, image alt attribute value, also set as title
 * @param String	optional, image event type
 * @param Function	optional, image event handler or reference to one,
 *					default closes window onclick
 * @return Boolean
 */
function popupImage(src, doc_title, alt, ev_type, ev_handler)
{
	var doc_title = doc_title || '';
	doc_title = (doc_title != '' ? doc_title : '-');
	var win_params = 'left=' + screen.width / 2 + ',top=' + screen.height / 2
		+ ',width=1,height=1,resizable=yes,status=no';
	var win = window.open('', '', win_params);
	var initialize = function()
	{
		var img = this.document.getElementById('img');
		img.style.cursor = 'pointer';
		if (alt)
			img.alt = img.title = alt;
		if (ev_type && ev_handler) {
			img[ev_type] = ev_handler;
		} else {
			img.onclick = function() { win.close(); };
		}
		this.resizeTo(img.width, img.height);
		this.moveTo(
		    Math.floor((screen.width - img.width) / 2),
		    Math.floor((screen.height - img.height) / 2)
		);
	};

	win.document.write(
		'<html><head><title>' + doc_title + '</title></head>'
		+ '<body><img id="img" src="' + unescape(src) + '"></body></html>'
	);
	if (document.all) {
		win.document.body.onload = initialize;
	} else {
		win.onload = initialize;
	}
	win.document.body.style.margin = 0;
	win.document.close();

	return true;
}