/*
 * JavaScript to make small popup windows with an image in it
 */

var POPUP_FEATURES = "dependent=0,location=0,menubar=0,resizable=0,toolbar=0";
var POPUP_DETAIL_FEATURES = "dependent=0,location=0,menubar=0,resizable=1,toolbar=0,scrollbars=1";
var POPUP_PADDING = 20;					// pixels
var POPUP_SPACE_BELOW = 80;				// pixels

var openPopups = new Array();
var aantalOpenPopups = 0;

/*
 * Returns the base of this page (top url minus the file name)
 */
function getBase() {
	var url = top.location.href;
	for (var q = url.length; q > 0; q--) {
		if ((url.charAt(q) == "/") || (url.charAt(q) == "\\")) {
			return url.substring(0, q + 1);
		}
	}
}

/*
 * Opens an popup window with an image in it
 *
 * Parameters:	url:			url of the image (relative to this document)
 *				description:	description ("onderschrift") of the image
 * 				width:			width of the image (in pixels)
 *				height:			height of the image (in pixels)
 *
 * Returns:		false (useful to prevent following a link)
 */
 function popup(url, description, width, height) {
 	// set features of the window
	var features = POPUP_FEATURES + ",width=" + (width + POPUP_PADDING);
	features += ",height=" + (height + POPUP_PADDING + POPUP_SPACE_BELOW);
	if (NN) {
		features += ",screenX=" + ((screen.availWidth - width - POPUP_PADDING) / 2);
		features += ",screenY=" + ((screen.availHeight - height - POPUP_PADDING - POPUP_SPACE_BELOW) / 2);
	} else {
		features += ",left=" + ((screen.availWidth - width - POPUP_PADDING) / 2);
		features += ",top=" + ((screen.availHeight - height - POPUP_PADDING - POPUP_SPACE_BELOW) / 2);
	}
	
	// build window
	var popupWindow = window.open("", uniqueWindowName(), features);
	popupWindow.document.open("text/html");
	popupWindow.document.writeln(
		"<HTML><BASE HREF=\"" + getBase() + "\">\n" +
		"<HEAD><TITLE>" + description + "</TITLE>\n" +
		START_STYLE_TAG + getStyleSheetFileName() + END_STYLE_TAG +
		"</HEAD>\n" +
		"<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\">\n" +
		"<TABLE ALIGN=\"center\" BORDER=\"0\" CELLPADDING=\"0\" CELLSPACING=\"0\">\n" +
		"<TR><TD WIDTH=\"" + (width + 2) + "\" ALIGN=\"center\">\n" +
		"    <IMG SRC=\"" + url + "\" WIDTH=\"" + width + "\" HEIGHT=\"" + height + "\" BORDER=\"1\" ALIGN=\"center\"><BR>\n" +
		"    <SPAN CLASS=\"onderschrift\">" + description + "</SPAN>\n" +
		"</TD></TR>\n" +
		"<TR><TD ALIGN=\"right\" VALIGN=\"bottom\">\n" +
		"    <BR><FORM><INPUT TYPE=\"button\" VALUE=\"Close\" onClick=\"self.close()\"></FORM>\n" +
		"</TD></TR>\n" +
		"</TABLE>\n" +
		"</BODY></HTML>"
	);
	popupWindow.document.close();

	// add window to open window list
	openPopups[aantalOpenPopups++] = popupWindow;

	return false;
}

/*
 * Opens an popup window with a detailed (zoomed) image in it.
 *
 * Parameters:	url:			url of the image (relative to this document)
 *				description:	description ("onderschrift") of the image
 * 				width:			width of the image (in pixels)
 *				height:			height of the image (in pixels)
 *				winWidth:		width of the window (in pixels)
 *				winHeight:		height of the window (in pixels)
 *
 * Returns:		false (useful to prevent following a link)
 */
 function popupDetail(url, description, width, height, winWidth, winHeight) {
 	// set features of the window
	var features = POPUP_DETAIL_FEATURES + ",width=" + winWidth;
	features += ",height=" + winHeight;
	if (NN) {
		features += ",screenX=" + ((screen.availWidth - winWidth) / 2);
		features += ",screenY=" + ((screen.availHeight - winHeight) / 2);
	} else {
		features += ",left=" + ((screen.availWidth - winWidth) / 2);
		features += ",top=" + ((screen.availHeight - winHeight) / 2);
	}

	// build window
	var popupWindow = window.open("", uniqueWindowName(), features);
	popupWindow.document.open("text/html");
	popupWindow.document.writeln(
		"<HTML><BASE HREF=\"" + getBase() + "\">\n" +
		"<HEAD><TITLE>" + description + "</TITLE>\n" +
		START_STYLE_TAG + getStyleSheetFileName() + END_STYLE_TAG +
		"</HEAD>\n" +
		"<BODY MARGINHEIGHT=\"1\" MARGINWIDTH=\"1\" STYLE=\"background: #ffffff none\" BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\">\n" +
		"<IMG SRC=\"" + url + "\" WIDTH=\"" + width + "\" HEIGHT=\"" + height + "\" BORDER=\"1\" HSPACE=\"0\" VSPACE=\"0\"><BR>\n" +
		"</BODY></HTML>"
	);
	popupWindow.document.close();

	// add window to open window list
	openPopups[aantalOpenPopups++] = popupWindow;

	return false;
}

/*
 * Opens an popup window with a document in it
 *
 * Parameters:	url:			url of the document (relative to this document)
 *				winWidth:		width of the window (in pixels)
 *				winHeight:		height of the window (in pixels)
 *
 * Returns:		false (useful to prevent following a link)
 */
 function popupDocument(url, winWidth, winHeight) {
 	// set features of the window
	var features = POPUP_DETAIL_FEATURES + ",width=" + winWidth;
	features += ",height=" + winHeight;
	if (NN) {
		features += ",screenX=" + ((screen.availWidth - winWidth) / 2);
		features += ",screenY=" + ((screen.availHeight - winHeight) / 2);
	} else {
		features += ",left=" + ((screen.availWidth - winWidth) / 2);
		features += ",top=" + ((screen.availHeight - winHeight) / 2);
	}

	// build window
	var popupWindow = window.open(url, uniqueWindowName(), features);

	// add window to open window list
	openPopups[aantalOpenPopups++] = popupWindow;

	return false;
}

function uniqueWindowName() {
	var result = "popup";
	now = new Date();
	result += now.getTime();
	
	return result;
}

/*
 * Closes all popup windows that are opened from this document
 */
function closePopups() {
	for (var i = 0; i < openPopups.length; i++) {
		if (!openPopups[i].closed) openPopups[i].close();
	}	
}
