// Pop-up window manager

// The following code is executed in the child window.
// It is packaged as a ParentWindow class which needs to be instantiated in the child window
// during loading of the page.
// e.g. 
// <SCRIPT>
//	<!--
//	var parentWindow = new ParentWindow()
//	//-->
// </SCRIPT>
// And later used like this: parentWindow.restore()

// Restore the parent window and close me; the child window.
function pu_restoreParent()
		{
		var parentWindow = window.opener
		if(typeof parentWindow == "undefined" || parentWindow == null || parentWindow.closed == true)
			{
			if(this.URL == "")
				{
				this.URL = getCookie("parentURL")
				}
			parentWindow = window.open(this.URL)
			}
		// parentWindow.focus() commented by tim 26/5
		window.close()
		}
		
// Traverse frames to find a reference to the top window.
function pu_WindowTop()
	{
	var windowReference = null
	if(typeof opener != "undefined" && opener != null)
		{
		windowReference = window.opener
		while(windowReference.parent != null && windowReference != windowReference.parent)
			{
			windowReference = windowReference.parent
			}
		}
	return windowReference
	}

function ParentWindow()
	{
	// Evaluate URL of opener window in case opener window is closed.
	// If it is location.href becomes undefined.
	// location.href for the opener is saved so the opener window can be restored if the user happens
	// to close it and then uses the next button of the childs window.
	this.URL = ""
	this.reference = pu_WindowTop()
	if(this.reference != null && this.reference.location != null)
		{
		this.URL = this.reference.location.href
		setCookie("parentURL", this.URL)
		} 
	this.restore = pu_restoreParent
	}
		
// These calls are made from the parent window
		
// It is packaged as a PopupWindow class which needs to be instantiated in the parent window
// during loading of the page.
// e.g. 
// <SCRIPT>
//	<!--
//	var exerciseWindow = new PopupWindow("exercise")
//	var glossaryWindow = new PopupWindow("glossary")
//	//-->
// </SCRIPT>
// And later used like this: exerciseWindow.open("url")
// or: glossaryWindow.open("url")

// This function is called from the parent window to create a child window
function pu_open(documentUrl) 
	{
	// Make a new window and save a reference to it.
	if(this.reference == null || this.reference.closed == true)
		{
		this.reference = window.open(documentUrl, this.name, this.decorations);
		}
	else
		{
		//pu_popupWindow.location.replace(url) 	// if need backbutton inactive
		this.reference.location.href = documentUrl
		}
	// this.reference.focus() commented by tim 26/5
	} 
	
function PopupWindow(windowName)
	{
	this.name = windowName
	this.reference = null
	this.decorations = "toolbar=no,location=no,directories=no,status=yes,menubar=0,scrollbars=yes,resizable=yes,width=600,height=400,alwaysRaised=yes"
	this.open = pu_open
	}
