/* This file contains javascript functions for managing popup windows.  
	It is designed to be accessible, and degrades gracefully if 
	javascript is disabled
	
	To open a pop up use pl_PopLink(..).
	
	To set default options for all popup windows, use the 
	pl_SetDefault function.  
	
	To open a link in the parent window use pl_PushLink(..)	
	
*/

var pl_gDefaultOptions = new Array();
var pl_gWindow = null;
var pl_gWindowName = "PopLink::" + window.location;


/* Set the default option to always include scroll bars. 
	I hate it when content overflows and I can't scroll */
pl_SetDefault( "scrollbars", "1");

function pl_SetDefault( optName, optValue) {
/* Sets default options to be applied to any new popup 

	Use: 	pl_SetDefault("height",500);
*/
	if( optValue != null )
		pl_gDefaultOptions[optName] = optValue;	
	else
		delete pl_gDefaultOptions[optName];
}

function pl_PopLink( a , optHeight, optWidth ) {
/* Displays a link as a popup window.
		
	Options can be controlled by using pl_SetDefault(..).
		
	The height and width are "prefered" values.  They will be honored if there is no
	popup window currently open, but will not be applied to an open window already.
		
	Stores the pop up window var in pl_gWindow
	
	a = anchor object
	optHeight = (optional) height int
	optWidth = (optional) width int
	
	Use: <a href="..." onclick="return pl_PopLink(this);">
*/
	
	if( pl_gWindow != null ) {
		if( ! pl_gWindow.closed  ) {
			pl_gWindow.location = a.href;
			pl_gWindow.focus();
			return false;
		}
	}
	
	/* create an option list for this window based on the defaults */
	var myOptions = pl_gDefaultOptions;
	
	// add width override
	if( optWidth != null )
		myOptions["width"] = optWidth;
	// add height override
	if( optHeight != null )
		myOptions["height"] = optHeight;
		
	var popOptions = "";
	/* local func used as a macro to add values to the option string */
	function addOption( option, value ){
		if( popOptions != "" )
			popOptions += ",";
		popOptions += option + "=" + value;
	}
	
	// create the option string to pass to open
	for( option in myOptions )
		addOption( option, myOptions[option] );
		
	pl_gWindow = window.open( a.href, pl_gWindowName, popOptions );
	return false; // this stops the main browser from loading the link
}


function pl_PushLink( a ) {
	/* opens a link in the opening window. If there is 
		no opening window, returns true indicating that 
		the click event should bubble and be handled normally
		
		Used in help pages when there is a link that
		should open in the orginal browser
		instead of the tiny help window.
		
		a = anchor object
		
		Use: <a href="..." onclick="return pl_PushLink(this);"*/
		return (opener.location = a.href ) == null;
}