/*****************************************************************************
 * RCLIB
 * JavaScript Popup Functions
 *
 * @desc        Manipulates and creates pop-up windows in JavaScript
 * @version     0.1
 * @author      Rick Christy <rick@funklabs.org>
 * @copyright   Copyright  2005, Rick Christy
 * @example     tests/form/index.php
 *
 * FUNCTIONS                DESCRIPTION
 * popup_window             Creates a pop-up window
 * create_popup_document    Creates popup window source code
 ****************************************************************************/

/**
 * @desc    Creates a pop-up window
 *
 * @param   string  windowName      Name of the window
 * @param   string  url             URL to load into window
 * @param   string  imageSrc        Source of image
 * @param   int     imageWidth      Width of source image
 * @param   int     imageHeight     Height of source image
 * @param   int     windowWidth     Desired width of window
 * @param   int     windowHeight    Desired height of window
 * @param   bool    centered        Center pop-up window on screen?
 * @param   string  features        Feature set for window control
 * @param   string  oldFeatures     Feature set of existing window control
 * @param   string  windowTitle     Title to give window document on load
 * @param   string  imageAlt        ALT text for source image
 *
 * @return  void
 */
function popup_window(windowName, url, imageSrc, imageWidth, imageHeight, windowWidth, windowHeight, centered, features, oldFeatures, windowTitle, imageAlt)
{
	var sw   = screen.width;
	var sh   = screen.height;
	var adjH = windowHeight - 256;
	var adjW = sw - 96;

	theRegExp = new RegExp("(width=.+?,)", "i");
	adj = "width=" + (parseInt(imageWidth) + 32) + ",";
	features = features.replace(theRegExp, adj);

	theRegExp = new RegExp("(height=.+?,)", "i");
	adj = "height=" + (parseInt(imageHeight) + 16) + ",";
	features = features.replace(theRegExp, adj);

	if (imageHeight >= sh)
	{
		theRegExp = new RegExp("(height=.+?,)","i");
		adjHs = "height=" + adjH + ",";
		features = features.replace(theRegExp, adjHs);
		windowHeight = adjH;
		theRegExp = new RegExp("(scrollbars=.+?,)","i");
		features = features.replace(theRegExp, "scrollbars=yes,");
		theRegExp = new RegExp("(resizable=.+?$)","i");
		features = features.replace(theRegExp, "resizable");
	}
	if (imageWidth >= sw)
	{
		theRegExp = new RegExp("(width=.+?,)","i");
		adjWs = "width=" + adjW + ",";
		features = features.replace(theRegExp, adjWs);
		windowWidth = adjW;
		theRegExp = new RegExp("(scrollbars=.+?,)","i");
		features = features.replace(theRegExp, "scrollbars=yes,");
		theRegExp = new RegExp("(resizable=.+?$)","i");
		features = features.replace(theRegExp, "resizable");
	}

	var top  = parseInt((screen.height - windowHeight) / 2);
	var left = parseInt((screen.width - windowWidth) / 2);

	top  += '';
	left += '';

	if (centered)
	{
		features += ',top=' + top + ',left=' + left;
	}

	windowName = window.open('', windowName, features);
	if (imageSrc)
	{
		create_popup_document(windowName, imageSrc, imageWidth, imageHeight, imageAlt, windowTitle);
	}
	else
	{
		if (windowName)
		{
			windowName.location = url;
		}
	}
	if (oldFeatures == 'fullscreen')
	{
		if (windowName)
		{
			windowName.moveTo(0,0);
			windowName.resizeBy(sw, sh);
		}
	}
	if (windowName)
	{
		windowName.focus();
	}
	return false;
} //popup_window



/**
 * @desc    Creates the popup window source code (helper to popup_window)
 *
 * @param   string  windowName   Name of the window to target
 * @param   string  imageSrc     Source of image
 * @param   int     imageWidth   Width of source image
 * @param   int     imageHeight  Height of source image
 * @param   string  imageAlt     ALT text of image
 * @param   string  windowTitle  Title for window
 *
 * @return false
 */
function create_popup_document(windowName, imageSrc, imageWidth, imageHeight, imageAlt, windowTitle)
{
	if (windowName)
	{
		title = windowTitle;
		alt = imageAlt;
		windowName.document.clear();
		windowName.document.open();
		if (title == '') title = 'Click to close';
		if (alt == '') alt = 'Click to close';
	    windowName.document.writeln('<html><head><title>' + title + ' - Click to close</title><meta http-equiv="imagetoolbar" content="false"><meta http-equiv="imagetoolbar" content="no"></head>');
	    windowName.document.writeln('<body style="background: #808080 url(http://designermonkey.rekkerd.org/scripts/wait.gif) no-repeat center center;" bgcolor="#808080" style="margin: 8px;" onclick="javascript:window.close();" onload="document.body.style.backgroundImage=\'\'; return true" topmargin="8" leftmargin="8" bgcolor="#000000" textcolor="#FFFFFF" link="#FFFFFF" alink="#FFFFFF" vlink="#FFFFFF" title="Click to close.">');
	    windowName.document.writeln('<table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0"><tr><td align="center" valign="middle">');
	    windowName.document.writeln('<img src="' + imageSrc + '" width="' + imageWidth + '" height="' + imageHeight + '" border="0" alt="' + alt + '">');
	    windowName.document.writeln('</tr></td></table>');
	    windowName.document.writeln('</body></html>');
		windowName.document.close();
		return false;
	}
} //create_popup_document
