// PopupHandler requires ZapLoader
if (window.zapLoader)
{
	zapLoader.add(_PopupHandler_Loader);
}

// load the popup handler
function _PopupHandler_Loader()
{
	window.popupHandler= new PopupHandler();
	window.popupHandler.init();
}


/*-----------------------------------------------------------------------------
PopupHandler: class to register popup onclick handlers for links of
a specific class
-----------------------------------------------------------------------------*/
function PopupHandler()
{
	this.init= _PopupHandler_Init;
	this.className= _PopupHandler_ClassName;
	this.blankTarget= _PopupHandler_BlankTarget;
	this.dimensionPattern= _PopupHandler_DimensionPattern;
	this.windowName= _PopupHandler_WindowName;
	this.windowWidth= _PopupHandler_WindowWidth;
	this.windowHeight= _PopupHandler_WindowHeight;
	this.options= _PopupHandler_Options;
	this.linkIsAware= _PopupHandler_LinkIsAware;
	this.clickHandler= _PopupHandler_ClickHandler;
	this.toString= _PopupHandler_ToString;

	// set defaults
	this.className("popup");
	this.blankTarget("_blank");
	this.dimensionPattern("w(\\d+)xh(\\d+)$");
	this.windowName("zapPopupWindow");
	this.windowWidth(600);
	this.windowHeight(400);
	this.options("location=no,menubar=no,resizable=no,scrollbars=no,toolbar=no,dependent=yes");
	this.linkIsAware(true);
}


// accessor to the class name to use for the popup
function _PopupHandler_ClassName(ivalue)
{
	if (ivalue)
	{
		this._className= ivalue;
	}

	return this._className;
}

// accessor to the blank target identifier
function _PopupHandler_BlankTarget(ivalue)
{
	if (ivalue)
	{
		this._blankTarget= ivalue;
	}

	return this._blankTarget;
}

// accessor to the regular expression pattern to use to
// identify custom window size
function _PopupHandler_DimensionPattern(ivalue)
{
	if (ivalue)
	{
		this._dimensionPattern= ivalue;
	}

	return this._dimensionPattern;
}

// accessor to the name of the window
function _PopupHandler_WindowName(ivalue)
{
	if (ivalue)
	{
		this._windowName= ivalue;
	}

	return this._windowName;
}

// accessor to the window width
function _PopupHandler_WindowWidth(ivalue)
{
	if (!isNaN(ivalue))
	{
		this._windowWidth= ivalue;
	}

	return this._windowWidth;
}

// accessor to the window height
function _PopupHandler_WindowHeight(ivalue)
{
	if (!isNaN(ivalue))
	{
		this._windowHeight= ivalue;
	}

	return this._windowHeight;
}

// accessor to the popup options list
function _PopupHandler_Options(ivalue)
{
	if (ivalue)
	{
		this._options= ivalue;
	}

	return this._options;
}

// accessor to the flag that lets the link know its a popup 
function _PopupHandler_LinkIsAware(ivalue)
{
	if (ivalue != null)
	{
		this._linkIsAware= ivalue;
	}

	return this._linkIsAware;
}


// initialize the popup handler
function _PopupHandler_Init()
{
	// regular expression to match the popup dimensions
	// specified in the class
	var dimensions= new RegExp(this.dimensionPattern());

	// loop over all the links in the document
	for (var i= 0; i < document.links.length; i++)
	{
		var l= document.links[i];

		// if the class name contains our target class
		if (l.className.indexOf(this.className()) > -1)
		{
			// does the class name contain popup window dimensions?
			var result= l.className.match(dimensions);

			// if so, assign them as properties of the link
			if (result != null)
			{
				l.popupWidth= result[1];
				l.popupHeight= result[2];
			}

			// are we told to mimic the old target="_blank" attribute?
			if (l.className.indexOf(this.blankTarget()) > -1)
			{
				l.targetBlank= true;
			}

			// let the link target know it's a popup
			if (this.linkIsAware())
			{
				l.href+= ((l.href.indexOf('?') > -1) ? "&" : "?") + "popup=1";
			}

			// register the onclick function for this link
			l.onclick= function() {
				if (window.popupHandler)
				{
					return window.popupHandler.clickHandler(this);
				}
				return true;
			};
		}
	}
}


// the popup onclick handler itself
function _PopupHandler_ClickHandler(ilink)
{
	var newUrl= ilink.href;

	var opts= "";

	if (!ilink.targetBlank)
	{
		var w= (ilink.popupWidth) ? ilink.popupWidth : this.windowWidth();
		var h= (ilink.popupHeight) ? ilink.popupHeight : this.windowHeight();

		opts= "width=" + w + ",height=" + h + "," + this.options();

		newUrl+= ((ilink.href.indexOf('?') > -1) ? "&" : "?") + "width=" + w + "&height=" + h;
	}

	var myWin= window.open(newUrl, this.windowName(), opts);

	return false;
}


// report this object as a PopupHandler
function _PopupHandler_ToString()
{
	return "PopupHandler";
}