// Copyright (C) 2007-2012 Bristle Software, Inc.
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 1, or (at your option)
// any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc.

/******************************************************************************
* com.bristle.jslib.Window.js
*******************************************************************************
* Purpose:
*       This file contains routines to manage browser windows.
* Usage:
*       - The typical scenario for using this file from an HTML file is:2*         <script language='JavaScript' src='com.bristle.jslib.Window.js'></script>
*         Call the various functions that reside here.
* Assumptions:
* Effects:
*       - None.
* Anticipated Changes:
* Notes:
* Implementation Notes:
* Portability Issues:
* Revision History:
*   $Log$
******************************************************************************/

// Create the "namespace" to hold the functions in this file.
com.bristle.jslib.Window = {};


/******************************************************************************
* Open a named window, containing a specified URL, and returning a 
* handle to it.  Also, keep a handle to that window to support calls to
* closeAllManagedWindows().
* If a window by that name already exists and is still open, and the 
* name does not start with underscore ("_"), just show the window, 
* don't re-load it's contents, and don't create a new window.
******************************************************************************/
com.bristle.jslib.Window.strWINDOW_NAME_NEW = "_blank";
    // Note:  Causes the specified URL to be opened in a new unnamed 
    //        window.
com.bristle.jslib.Window.strWINDOW_NAME_SELF = "_self";
    // Note:  Causes the specified URL to be opened in the current
    //        window.
com.bristle.jslib.Window.strWINDOW_NAME_PARENT = "_parent";
    // Note:  Causes the specified URL to be opened in the parent 
    //        window of the current frame.  If no parent, same as 
    //        com.bristle.jslib.Window.strWINDOW_NAME_SELF.
com.bristle.jslib.Window.strWINDOW_NAME_TOP = "_top";
    // Note:  Causes the specified URL to be opened in top level window,
    //        replacing any framesets.
com.bristle.jslib.Window.strWINDOW_NAME_SEARCH = "_search";
    // Note:  Causes the specified URL to be opened in the browser's 
    //        search pane.  (IE 5.0 and later)
com.bristle.jslib.Window.strWINDOW_NAME_MEDIA = "_media";
    // Note:  Causes the specified URL to be opened in the IE Media Bar.
    //        (IE 6.0 and later)
com.bristle.jslib.Window.blnSHOW_ADDRESS_BAR = true;
com.bristle.jslib.Window.blnDEFAULT_SHOW_ADDRESS_BAR = true;
com.bristle.jslib.Window.blnSHOW_MENU_BAR = true;
com.bristle.jslib.Window.blnDEFAULT_SHOW_MENU_BAR = true;
com.bristle.jslib.Window.blnSHOW_TOOL_BAR = true;
com.bristle.jslib.Window.blnDEFAULT_SHOW_TOOL_BAR = true;
com.bristle.jslib.Window.blnSHOW_DIRECTORY_BAR = true;
com.bristle.jslib.Window.blnDEFAULT_SHOW_DIRECTORY_BAR = true;

com.bristle.jslib.Window.arrManagedWindows = new Array();

com.bristle.jslib.Window.openManagedWindow =
function(strURL
        ,strName
        ,blnShowAddressBar
        ,blnShowMenuBar
        ,blnShowToolBar
        ,blnShowDirectoryBar
        )
{
    // Fill in default parameter values.
    if (blnShowAddressBar == null || typeof (blnShowAddressBar) == "undefined")
    {
        blnShowAddressBar = true;
    }
    if (blnShowMenuBar == null || typeof (blnShowMenuBar) == "undefined")
    {
        blnShowMenuBar = true;
    }
    if (blnShowToolBar == null || typeof (blnShowToolBar) == "undefined")
    {
        blnShowToolBar = true;
    }
    if (blnShowDirectoryBar == null || typeof (blnShowDirectoryBar) == "undefined")
    {
        blnShowDirectoryBar = true;
    }

    var objWindow = com.bristle.jslib.Window.arrManagedWindows[strName];
    if (   (strName.charAt(0) == "_")
        || (typeof (objWindow) == "undefined")
        || (objWindow.closed)
       )
    {
        // Note:  Beware of spaces after commas, per O'Reilly DHTML book
        var strFeatures = 
                    "fullscreen=no"
              // + ",top=100"          // No need to force the window
              // + ",left=100"         // to a fixed position.
              // + ",width=600"
              // + ",height=300"
                            //?? Could compute the location of the new 
                            //?? window, relative to the old one, to 
                            //?? create a cascading effect that doesn't 
                            //?? seem to otherwise occur.
                 + ",resizable=yes"

                 + ",scrollbars=yes"

                 // Input field for typing a URL
                 + ",location="    + (blnShowAddressBar   ? "yes" : "no")

                 + ",menubar="     + (blnShowMenuBar      ? "yes" : "no")

                 + ",toolbar="     + (blnShowToolBar      ? "yes" : "no")

                 // Directory buttons toolbar
                 + ",directories=" + (blnShowDirectoryBar ? "yes" : "no")

                 // Limited situation.  See docs.
                 + ",titlebar=yes"

                 // Status bar
                 + ",status=yes"

                 + ",copyhistory=no"

                 // Theater mode showing channel band
                 + ",channelmode=no"
                 ;
        objWindow = window.open(strURL, strName, strFeatures);
        com.bristle.jslib.Window.arrManagedWindows[strName] = objWindow;
    }
    else
    {
        objWindow.focus();
    }
    return objWindow;
}

/******************************************************************************
* Open a new Window.
******************************************************************************/
com.bristle.jslib.Window.openNewWindow =
function(strURL)
{
    return com.bristle.jslib.Window.openManagedWindow
                (strURL, com.bristle.jslib.Window.strWINDOW_NAME_NEW);
}

/******************************************************************************
* Close the current window.
******************************************************************************/
com.bristle.jslib.Window.closeWindow =
function()
{
    // Re-open the current window, using the same contents, into the same 
    // frame if any, and with no new features.
    // Note:  This is a trick to get Firefox (version 1.5.0.10 anyhow) to not 
    //        ignore the window.close()
    window.open('',com.bristle.jslib.Window.strWINDOW_NAME_SELF,'');

    // Close the window.
    window.close();
}

/******************************************************************************
* Close all windows that were opened with openManagedWindow().
* Note: The list of managed Windows only lasts as long as the original HTML
*       page that opened the windows.  Each time a new HTML page loads, and
*       loads this JavaScript file, a new list of managed Windows is created.
*       ?? Is there any more persistent scope available to store this array?
******************************************************************************/
com.bristle.jslib.Window.closeAllManagedWindows =
function()
{
    for (var i in com.bristle.jslib.Window.arrManagedWindows)
    {
        //?? Doesn't work.  Always reports no such method.  Why?
        i.close();
    }
}

/******************************************************************************
* Go back to the previous window in the browser history, if any, or close
* the current window.
******************************************************************************/
com.bristle.jslib.Window.goBackOrCloseWindow =
function()
{
    if (history.length > 0)
    {
        history.back();
    }
    else
    {
        com.bristle.jslib.Window.closeWindow();
    }
}

