<!--

/*  ***********************************************  */
/*  For inclusion in the HEAD of each "viewer" page  */
/*  ***********************************************  */

/*  **********  */
/*  Background  */

/*  A "viewer" page prepares a frameset for presentation of a "document" 
    page in some sort of context, e.g., with a table of contents. The 
    document is specified in a search-string argument, e.g., 

        http://hostname/viewer.htm?doc=path/document.htm 

    There may be other search-string arguments, e.g., to set an expansion 
    state for a table of contents. 

    Configuration (and also some helper functions) is obtained from prior 
    execution of MASTER.JS.  */

/*  ****************  */
/*  Public Variables  */

/*  Other scripts access these variables as members of the "window.top" 
    object.  */

/*  Arguments from the viewer's search string, parsed once for common 
    benefit  */

var oUrlArguments = new ParsedSearch ();

/*  Local URLs for frame redirection  */

var locSubweb = null;
var locToc = null;
var locDocument = null;

/*  An ugly flag to be cleared when the document redirection is finished  */

var bInitialDocument = true;

/*  *****************  */
/*  Frame Redirection  */

function IsValidDocArgument (Doc)
{
    /*  All paths that are valid for redirection will begin with a 
        lower-case letter.  */

    var first = Doc.charAt (0);
    return 'a' <= first && first <= 'z';
}

function InitialiseLocations ()
{
    /*  Of particular interest to the viewer in the search-string is an 
        argument that may name a document to show in the document frame.  */

    var viewerpath = PathnameGetPath (window.location.pathname);

    var doc = oUrlArguments.GetValue (sDocumentArgumentName);
    if (doc == null || !IsValidDocArgument (doc)) doc = sDefaultFilename;

    /*  The document is named as a pathname relative to the directory that 
        contains the viewer. Compose the corresponding absolute pathname. 

        The present implementation happens to have only one viewer for the 
        whole web and to have it in the root directory, but a viewer 
        elsewhere would know its location when executing this script and 
        therefore doesn't need to depend on being in any particular place.  */

    var docpathname = PathAppend (viewerpath, doc);

    /*  From the document's absolute pathname in the whole site, determine 
        which table of contents governs the document.  */

    var subwebpath = GetSubwebPath (docpathname);
    if (subwebpath == null) subwebpath = viewerpath;

    /*  Save locations of the document and TOC pages. These are then 
        retrievable from the frames that are to show those pages.  */

    locDocument = new LocalUrl (docpathname);
    var bm = oUrlArguments.GetValue (sBookmarkArgumentName);
    if (bm != null) locDocument.hash = "#" + bm;

    locSubweb = new LocalUrl (subwebpath);

    locToc = new LocalUrl (PathAppend (subwebpath, sTocFilename));
}

/*  *********************  */
/*  Global Initialisation  */

/*  The viewer sets up frames. For the viewer itself to appear in a frame is 
    not expected, but would look silly. As a brief defence, simply reset the 
    top window so that it shows whatever was being attempted in the frame.  */

if (window != window.top) {
    window.top.location.replace (window.location);
}
else {

    /*  Assign the window a name that is specific to the author and purpose. 
        Then, pages that find themselves running in a frame can verify that 
        their frame has the expected top window.  */

    window.name = sViewerWindow;

    /*  Parse the search string - once, for common benefit.  */

    oUrlArguments.AddString (window.location.search);

    /*  Prepare the global records of pages that are to be shown in various 
        frames. This is done separately only so that local variables defined 
        for the work do not get global scope.  */

    InitialiseLocations ();
}

/*  Copyright © 2007-2009. Geoff Chappell. All rights reserved.  */

//-->