/****************************************************************
 ****************************************************************
 **
 ** Popup window tracking
 **
 ** Copyright (c) 2004 Nameless-UK
 **
 ** NB: Do NOT use this file directly; because it supports localization,
 ** you must use the Tapestry wrapper component called "Window".
 **
 ****************************************************************
 ****************************************************************
 */

/****************************************************************
 * Globals
 ****************************************************************
 */
var windowCurrentWindow = null; // Our child pop-up window (if any)

/****************************************************************
 * Popup a window
 ****************************************************************
 */
function windowOpenPopup(sName, sURL, iWidth, iHeight) {
	var standardWidth = 820;
	var standardHeight = 620;
	var width;
	var height;
	if (iWidth && iHeight) {
		width = iWidth;
		height = iHeight;
	}
	else {
		width = standardWidth;
		height = standardHeight;
	}

    return(windowOpenWindow(sName, width, height, sURL));
}

function windowOpenPersistentWindow(sName, iWidth, iHeight, sURL) {
	res = windowOpenWindow(sName, iWidth, iHeight, sURL);
	windowCurrentWindow = null; // don't track this window
	return res;         
}


/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function windowOpenWindow(sName, iWidth, iHeight, sURL) {

    try {
        if (windowCurrentWindow) windowCurrentWindow.close();
    }
    catch (e) {
        // swalllow
    }

        // extra pixels to take into account the title bar at the top of the window
        // Center the window in the screen.
		var iLeft  = (screen.width  - iWidth)  >>> 1;
        var iTop = (screen.height - iHeight - 30 ) >>> 1;

        var aOptions = new Array(	'toolbar=0',
									'location=0',
									'directories=0',
									'status=0',
									'menubar=0',
									'scrollbars=0',
									'resizable=1',
									'width=' + iWidth,
									'height='+ iHeight,
									'top='   + iTop,
									'left='  + iLeft);

		// Under certain versions of Windows/IE it decides to fire up a new IE instance for popup windows
		// This new IE instance does not share any session cookies and so the session id and session are lost.
		// So here we splice that information into the URL - we extract it out in the servlet later.
    	var sessionId = ';jsessionid=' + getCookie('JSESSIONID');
	    var question  = sURL.indexOf('?');
	    sURL = (question == -1) ? sURL + sessionId : sURL.substring(0, question) + sessionId + sURL.substring(question);
    
		// Creation...
		var sProxyUrl = "/sitereporter/app?service=external/PageLoader&sp=S" + escape(sURL);
		windowCurrentWindow = top.open(sProxyUrl, sName, aOptions.join(','));
}

function windowOnUnLoad()
{
//  alert('window onunload, windowCurrentWindow=' + windowCurrentWindow);
  if (windowCurrentWindow)
  {
    try {
      windowCurrentWindow.close();
    } catch (e) {
      // no-op
    }
  }
}

addToOnUnloadChain(window, windowOnUnLoad);


function windowOnLoad()
{
	if ((typeof isPageModal != "undefined") && isPageModal) {
		top.isApplicationModal = true;
	}
	else {
		top.isApplicationModal = false;
	}	
}

addToOnLoadChain(window, windowOnLoad);

/*
 * Resizes a window but keeps it centre point in the same location on the screen.
 */
function resizeWindow(newWidth, newHeight)
{
	var oldWidth, oldHeight;
	var dw, dh;

	oldWidth = window.document.body.clientWidth;
	oldHeight = window.document.body.clientHeight;

	dw = newWidth - oldWidth;
	dh = newHeight - oldHeight;

	window.resizeBy(dw, dh);
	window.moveBy(-dw / 2, -dh / 2);
}

/*
 * Finds the main window from an arbitrary descendant (or itself)
 */
function findMainWindow()
{
	var candidate = top;
	while (candidate.opener != null)
	{
		candidate = candidate.opener.top;
	}
	return candidate;
}

