/**
 * This file defines the configuration object for the Gigapan Explorer Application. <br><br>
 * <span style="font-weight: 800; font-size: 16pt;">How to Configure Gigapan Explorer</span> 
 * <br><br>
 * Configuration parameters are available to control the behavior of this application.  They are
 * used in the http-standard url query string.  <br>See below for the types of parameters, their
 * possible values, and real-life examples.<br><br>
 * <span style="font-weight: 800; font-size: 14pt;">The Query String</span><br>
 * In a url query string:
 * <ul>
 * <li>The first parameter is denoted by a <code>?</code> character.</li>
 * <li>Its value is denoted by an <code>=</code> character.</li> 
 * <li>The second parameter is denoted by an <code>&amp;</code> character.</li>
 * <li>Its value is denoted by an <code>=</code> character.</li> 
 * </ul>
 * <span style="font-weight: 800; font-size: 14pt;">Available Configuration Parameters</span><br> 
 * Here are the available configuration parameters (examples are further below):<br><br>
 * <table border='2px' style="width: 520px; border-style=inset; margin-left: 10px;" >
 * <tr>
 * <td bgcolor="lemonchiffon">Parameter</td><td bgcolor="lemonchiffon">Possible Values</td>
 * </tr>
 * <tr>
 * <td>imageSetRootUrl</td>
 * <td>
 * OPTIONAL This must be a standard http url, with a trailing slash.  
 * The value is optionally used to 
 * set to tell the <code>ImageUrlCalulator</code> what value to use in order 
 * to create the complete urls that point to the images needed for display 
 * by the application. If not set, the 
 * configuration will default to a pre-set value.  Note that a trailing 
 * forward-slash is required for this value.
 * </td></tr><tr>
 * <td>imageSetDescriptionFileName</td>
 * <td>OPTIONAL The value is optionally used to set the name of the image set xml description file. 
 * If not set, the configuration will default to a pre-set value.
 * </td></tr><tr>
 * <td>imageSetDescriptionUrl</td>
 * <td>OPTIONAL The value is optionally used to set the url of the image set description xml file.
 * If it is set, the set value overrides this parameter's default value.  That default value is the concatenation
 *  of the values of the <code>imageSetRootUrl</code> and <code>imageSetDescriptionFileName</code> parameters 
 * (concatenated in that order).  
 * </td>
 * </tr>
 * <tr>
 * <td>numImagesPerSide</td>
 * <td>OPTIONAL The value is optionally used to set the number of image tiles per side on the <code>plate</code>
 * dom object.
 * </td>
 * </tr>
 * <tr>
 * <td>turnOnImageTileDebuggingFlags</td>
 * <td>OPTIONAL This is optionally used turn the debugging flags for 
 * image tiles on and off.  Debugging flags are turned on by setting this parameter 
 * to 'true' (using no quotes).
 * </td>
 * </tr>
 * </table>
 * <br><br>
 * <span style="font-weight: 800; font-size: 14pt;">Examples</span><br> 
 * To configure the application, simply either:
 * <br>
 * <ul>
 * <li>
 * Pass in the parameters via an http-standard query string attached to the web address of the Gigapan Explorer 
 * application.
 * <br>To do this, use any or all of the values of the identifiers that 
 * begin with <code>STR_PARAM_NAME_</code>. These identifiers are documented in this JavaScriptdoc.  <br><br>
 * <b>Example #1:</b>:<br><br>
 * &nbsp; &nbsp;
 * <code>http://localhost/gigapan/?imageSetRootUrl=http://localhost/gigapan/gov/nasa/centers/ames/gigapan_test/test/panorama_images/jaga.gc.cs.cmu.edu/explorer/salttown/&imageSetDescriptionFileName=description.xml&numImagesPerSide=10</code>
 * <br><br>
 * The above example will tell the Gigapan application that:
 *   <ul>
 *   <li>
 *    The root url for both the images and the xml description is: <br>
 *    &nbsp; &nbsp; <code>http://localhost/gigapan/gov/nasa/centers/ames/gigapan_test/test/panorama_images/jaga.gc.cs.cmu.edu/explorer/salttown/</code>
 *    <br>
 *    (Note the trailing forward-slash character on the url above).
 *   </li>
 *   <li>
 *    The file name of the xml description is: <br>
 *    &nbsp; &nbsp; <code>description.xml</code>
 *   </li>
 *   <li>
 *    The Gigapan application should draw the moveable <code>plate</code> dom object so that it has 10 image 
 *    tiles per side.  Note that this parameter will only need to be set for the purpose of research into
 *    optimizing the performance of the Gigapan application.
 *   </li>
 *   </ul>
 * </li>
 * <li>
 * . . . or manually set, as needed,
 * the value of the constant class properties used for application configuration defaults.  
 * These constants are documented below.
 * </li>
 * </ul>
 * <br><br>
 * <b>Example #2</b>:<br><br>
 * <code>http://localhost/gigapan/?imageSetRootUrl=http://jaga.gc.cs.cmu.edu/explorer/salttown/&imageSetDescriptionUrl=http://localhost/gigapan/gov/nasa/centers/ames/gigapan_test/test/panorama_images/jaga.gc.cs.cmu.edu/explorer/salttown/description.xml</code>
 * <br><br>
 * The example above tells the Gigapan Explorer application to fetch its images from the specified location at 
 * <code>jaga.gc.cs.cmu.edu</code>, and its xml image set to the location specified at <code>localhost</code>
 * <br><br>
 * <b>Security Note</b>: Security restrictions on xml-http requests prohibit such requests for xml description 
 * files that 
 * are not located under the same exact complete domain as the Gigapan Explorer application itself. 
 * <br><br>
 * @class-prop <code>string</code> <code>STR_PARAM_NAME_IMAGE_SET_ROOT_URL</code>  This is a constant set to 
 *                                 '<b>imageSetRootUrl</b>' (no quotes).  This value is the name of the query 
 *                                 string parameter that is optionally 
 *                                 set to tell the <code>ImageUrlCalulator</code> what value to use in order 
 *                                 to create the complete urls that 
 *                                 point to the images needed for display by the application. If not set, the 
 *                                 configuration will default to a pre-set value.  Note that a trailing 
 *                                 forward-slash is required for this value. 
 * @class-prop <code>string</code> <code>STR_PARAM_NAME_IMAGE_SET_DESCRIPTION_FILE_NAME</code>  This is a constant 
 *                                 set to '<b>imageSetDescriptionFileName</b>' (no quotes).  This value is 
 *                                 the name of the query string parameter that is optionally used to set the image 
 *                                 set xml description file location, via a web address (url) 
 *                                 http-standard query param.  If not set, the configuration will default to a 
 *                                 pre-set value.
 * @class-prop <code>string</code> <code>STR_PARAM_NAME_IMAGE_SET_DESCRIPTION_URL</code> is a constant 
 *                                 set to '<b>imageSetDescriptionUrl</b>' (no quotes). This value is the name of 
 *                                 the query string parameter that is optionally used to set the url
 *                                 of the image set description xml file.  (It ignores any values set for 
 *                                 <code>STR_PARAM_NAME_IMAGE_SET_ROOT_URL</code> and 
 *                                 <code>STR_PARAM_NAME_IMAGE_SET_DESCRIPTION_FILE_NAME</code>).  Ordinarily, it 
 *                                 will not be necessary to set this configuration value, as the image set is 
 *                                 and image set description xml file are usually located in the same relative
 *                                 file directories.  Thus, unless this parameter is set explicitly in the 
 *                                 query string as described above, its value will be 
 *                                 automatically constructed from the value of the 
 *                                 <code>STR_PARAM_NAME_IMAGE_SET_ROOT_URL</code> parameter
 *                                 concatenated with the value of the 
 *                                 <code>STR_PARAM_NAME_IMAGE_SET_DESCRIPTION_FILE_NAME</code> parameter.
 * @class-prop <code>number</code> <code>STR_PARAM_NAME_NUMBER_IMAGES_PER_SIDE</code> is a constant set to
 *                                 '<b>numImagesPerSide</b>' (no quotes).  This value is the name of the query string
 *                                 parameter that is optionally  
 *                                 used to set the number of image tiles per side on the <code>plate</code>
 *                                 dom object, via a web address (url) http-standard query param. 
 *                                 Note that this parameter will only need to be set for the purpose of research into
 *                                 optimizing the performance of the Gigapan application.
 * @class-prop <code>string</code> <code>STR_PARAM_NAME_DEBUGGING_FLAGS_FOR_IMAGE_TILES_ON</code> is a constant
 *                                 set to '<b>turnOnImageTileDebuggingFlags</b>' (no quotes). This value is the name of 
 *                                 the query string parameter that is optionally used turn the debugging flags for 
 *                                 image tiles on and off.  Debugging flags are turned on by setting this parameter 
 *                                 to 'true' (using no quotes).
 * @class-prop <code>string</code> <code>STR_DEFAULT_IMAGE_SET_ROOT_URL</code> is a constant value representing
 *                                 the root url (url minus filename) of the images needed for display by 
 *                                 the application (the 'image set').
 *                                 This value may be overridden by
 *                                 the corresponding http-standard url query string parameter described above.
 * @class-prop <code>string</code> <code>STR_DEFAULT_IMAGE_SET_DESCRIPTION_FILE_NAME</code> is a constant value 
 *                                 representing the file name of the xml description of the image set. 
 *                                 This value may be overridden by
 *                                 the corresponding http-standard url query string parameter described above.
 * @class-prop <code>number</code> <code>INT_DEFAULT_NUMBER_IMAGES_PER_SIDE</code> is a constant value representing
 *                                 the pre-set default number of 
 *                                 image tiles on the <code>plate</code> dom object.  This value may be overridden by
 *                                 the corresponding http-standard url query string parameter described above.
 * @class-prop <code>boolean</code> <code>BOO_DEFAULT_IMAGE_TILE_DEBUGGING_FLAGS_ON</code> is a constant value representing
 *                                  the default setting for whether or not the image tile debugging flags should be turned on.
 * @object-prop <code>Object</code> <code>priHshParams</code>  The private hash of name-value pairs that 
 *                                  constitute the parameters of the application.
 * @object-prop <code>Object</code> <code>objParent</code>  The parent of this object, the <code>Controller</code> object.
 * <br><br>
 * <a href="mailto:ehudsons@andrew.cmu.edu">Ellen Hudson-Snyder</a>, 
 * <a href="mailto:evedar@andrew.cmu.edu">Elvin Vedar</a>,
 * <a href="mailto:cbalz@andrew.cmu.edu">Christopher M. Balz</a>.
 * <br><br>CVS Version Info:<br>
 *  $Id: Configuration.js,v 1.11 2005/12/06 05:54:43 cbalz Exp $     
 * <br><br>
 * @author Team GigaToasted (Fall-2005-CMU-NASA/Google-Practicum Subteam) 
 * @version 1.0
 * <br><br>
 */

Configuration.STR_DEFAULT_IMAGE_SET_ROOT_URL = 
  "http://localhost/gigapan/gov/nasa/centers/ames/gigapan_test/test/panorama_images/jaga.gc.cs.cmu.edu/explorer/pakistan1/";
Configuration.STR_DEFAULT_IMAGE_SET_DESCRIPTION_FILE_NAME = "description.xml";
Configuration.INT_DEFAULT_NUMBER_IMAGES_PER_SIDE = function() {
        var intS = Math.floor( ( ( ( screen.height > screen.width ) ? screen.height : screen.width )  / 256 ) * 1.5 );
        if (intS % 2 != 0) { 
            intS += 1; 
        } 

        if (intS < 16) {
            intS = 16;
        }
        return intS;
    }();
Configuration.BOO_DEFAULT_IMAGE_TILE_DEBUGGING_FLAGS_ON = false;
Configuration.STR_PARAM_NAME_IMAGE_SET_ROOT_URL = "imageSetRootUrl";
Configuration.STR_PARAM_NAME_IMAGE_SET_DESCRIPTION_URL = "imageSetDescriptionUrl";
Configuration.STR_PARAM_NAME_IMAGE_SET_DESCRIPTION_FILE_NAME = "imageSetDescriptionFileName";
Configuration.STR_PARAM_NAME_NUMBER_IMAGES_PER_SIDE = "numImagesPerSide";
Configuration.STR_PARAM_NAME_DEBUGGING_FLAGS_FOR_IMAGE_TILES_ON = "turnOnImageTileDebuggingFlags";

/**
 * Create a <code>Configuration</code> object.
 * @param pObjParent <code>Object</code>  The parent of this object, the <code>Controller</code> object.
 */
function Configuration(pObjParent) {    
    this.strId = "Configuration";
    this.objParent = pObjParent;

    this.priHshParams = {};

    // Methods:
    this.init = Configuration_init;
    this.getParameter = Configuration_getParameter;   
    
    this.init();
}


/**
 * This method sets any parameters specified in the url so that they are 
 * accessible from the <code>Configuration</code> object.
 */
function Configuration_init() {
    var intI=0, intL, arrNameValuePairs, arrSplit, strName, anyValue, 
        intCountParamsToMakeDescriptionFileUrl = 0;


   /**
    * This inner method performs initialization and setting of the default values
    * of the parameters that may be 
    * specified in the url query string used to browse to the Gigapan 
    * Explorer application.
    * @param objMe <code>object</code>  The current <code>this</code> context for the <code>Configuration</code>
    *              object.  This is needed by JavaScript inner methods.
    */
    function _init_setDefaults(objMe) {
        objMe.priHshParams[ Configuration.STR_PARAM_NAME_IMAGE_SET_ROOT_URL ] = Configuration.STR_DEFAULT_IMAGE_SET_ROOT_URL;
        objMe.priHshParams[ Configuration.STR_PARAM_NAME_IMAGE_SET_DESCRIPTION_FILE_NAME ] = 
            Configuration.STR_DEFAULT_IMAGE_SET_DESCRIPTION_FILE_NAME; 
        // The value below is composed later, at the end of the 'init' method. 
        objMe.priHshParams[ Configuration.STR_PARAM_NAME_IMAGE_SET_DESCRIPTION_URL ] = ""; 
        objMe.priHshParams[ Configuration.STR_PARAM_NAME_NUMBER_IMAGES_PER_SIDE ] = 
            Configuration.INT_DEFAULT_NUMBER_IMAGES_PER_SIDE;
        objMe.priHshParams[ Configuration.STR_PARAM_NAME_DEBUGGING_FLAGS_FOR_IMAGE_TILES_ON ] = 
            Configuration.BOO_DEFAULT_IMAGE_TILE_DEBUGGING_FLAGS_ON 
    }


   /**
    * This inner method performs error checking of the parameters that may be 
    * specified in the url query string used to browse to the Gigapan 
    * Explorer application.  It also notifies the user if there are any errors found and
    * provides a suggested remedy.
    * @param objMe <code>object</code>  The current <code>this</code> context for the 
    *              <code>Configuration</code>
    *              object.  This is needed by JavaScript inner methods.
    */
    function _init_performErrorChecking(objMe) {
        var strCurrentCompleteDomain = self.gloScope.getWholeDomain(window.location.href),
            strXmlDescriptionCompleteDomain = 
               self.gloScope.getWholeDomain(objMe.priHshParams[ Configuration.STR_PARAM_NAME_IMAGE_SET_DESCRIPTION_URL ]),
            intNumImagesPerSide = objMe.priHshParams[ Configuration.STR_PARAM_NAME_NUMBER_IMAGES_PER_SIDE ];
                    
        /*-
         * Note: We do not use 'document.domain' here since it could have been reset (downgraded to a root by 
         * domain) by the JavaScript code, and xml-http does not respect downgraded domains.
         */
        if (strCurrentCompleteDomain != strXmlDescriptionCompleteDomain) {
            alert("Error: Security restrictions on xml-http requests prohibit requests for" +
                  " xml description files that are not located under the same exact complete " +
                  " domain as the Gigapan Explorer application itself. " +
                  "\n\n Your current complete domain is: " + strCurrentCompleteDomain + 
                  "\n and the application is currently set to load the xml description file  " +
                  "from this domain: " + objMe.priHshParams[ Configuration.STR_PARAM_NAME_IMAGE_SET_DESCRIPTION_URL ] + "." +
                  objMe.objParent.strConfigErrorRemedy
                );
        }

        if (!intNumImagesPerSide || intNumImagesPerSide < 1) {
            alert("Error: The number of image tiles to show is not a natural number." + objMe.objParent.strConfigErrorRemedy);
        }
    }


    _init_setDefaults(this);

    arrNameValuePairs = window.location.href.replace(/^[^\?]*\?/, "").split(/&/);
    intL = arrNameValuePairs.length;        

    for (; intI < intL; intI++) {
        arrSplit = arrNameValuePairs[intI].split(/=/);
        strName = arrSplit[0];
        anyValue = arrSplit[1];
        this.priHshParams[ strName ] = anyValue;
    }

    if (this.priHshParams[ Configuration.STR_PARAM_NAME_IMAGE_SET_DESCRIPTION_URL ] == "") {
        this.priHshParams[ Configuration.STR_PARAM_NAME_IMAGE_SET_DESCRIPTION_URL ] =
            this.priHshParams[Configuration.STR_PARAM_NAME_IMAGE_SET_ROOT_URL] + 
            this.priHshParams[Configuration.STR_PARAM_NAME_IMAGE_SET_DESCRIPTION_FILE_NAME];
    }

    if (this.priHshParams[ Configuration.STR_PARAM_NAME_DEBUGGING_FLAGS_FOR_IMAGE_TILES_ON ]) {
        this.priHshParams[ Configuration.STR_PARAM_NAME_DEBUGGING_FLAGS_FOR_IMAGE_TILES_ON ] = 
            (new Boolean(this.priHshParams[ Configuration.STR_PARAM_NAME_DEBUGGING_FLAGS_FOR_IMAGE_TILES_ON ].match("true"))).valueOf();
    }

    /*-
     * Clean off any anchor "#" values from the end of the url (the browser attaches them when the user navigates 
     * on the Gigapan Explorer application).
     */
    this.priHshParams[ Configuration.STR_PARAM_NAME_NUMBER_IMAGES_PER_SIDE ] = 
        parseInt(this.priHshParams[ Configuration.STR_PARAM_NAME_NUMBER_IMAGES_PER_SIDE ]);

    _init_performErrorChecking(this);

}


/**
 * This method returns the value of the parameter specified by <code>pStrParameterName</code>.
 * @param pStrParameterName The name of the parameter.
 * @return <code>any</code> The value of the configuration property specified.
 */
function Configuration_getParameter(pStrParameterName) {
    var undUndefined;

    if (typeof this.priHshParams[pStrParameterName] != "undefined") {
        return this.priHshParams[pStrParameterName];
    }
    
    alert("Gigapan Error: The configuration property '" + pStrParameterName + "' is not defined." );
    
    return undUndefined;
}
