/******************************************************************************
 *
 *  Copyright 2008 David D. Emory [additional contributors append names here]
 *
 *  This file is part of Five Points. See <http://www.fpdev.org> for
 *  additional project information and documentation.
 *  
 *  Five Points 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 3 of the License, or
 *  (at your option) any later version.
 *  
 *  Five Points 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 Five Points.  If not, see <http://www.gnu.org/licenses/>.
 *  
 ******************************************************************************/    

// FIELD DECLARATIONS

var fp_IE_; // (Boolean); indicates whether we are working w/ Internet Explorer 

var fp_winWidth_, fp_winHeight_; // (Number); browser window dimensions

var fp_currentApp_; // (Number); the code for the currently active application module

// constants for the different application modules
var FP_APP_TRIP_PLANNER = 1;
var FP_APP_ROUTE_EXPLORER = 2;

var fp_AppHTMLArray_; // (Array of Strings); contains app panel contents for each module 
    
// FUNCTION DEFINITIONS

/**
 * fp_init()
 * 
 * The main initialization function for the 5P web app. Calls initialzation
 * functions for various subsystems (e.g. the map display, layout manager).
 * Uses ajax to read the initial content of each application module. 
 */
function fp_init() {
  fp_IE_ = document.all ? true : false;
  fp_resized();
  fp_initLayout();
  fp_initMap();

  fp_AppHTMLArray_ = new Array();
  fp_ajaxLoadAsText("fp_trp.php", fp_loadTrpHtml);
  fp_ajaxLoadAsText("fp_rte.php", fp_loadRteHtml);
  fp_dbg("fp_init done");
}

/**
 * fp_loadTrpHtml(html)
 * 
 * Loads the html content for the Trip Planner module. Also sets that module as
 * the one that is active by default.
 */
function fp_loadTrpHtml(html) {
  fp_AppHTMLArray_[FP_APP_TRIP_PLANNER] = html;
  
  // this application is active by default:
  fp_selectApplication(FP_APP_TRIP_PLANNER);
}

/**
 * fp_loadTrpHtml(html)
 * 
 * Loads the html content for the Route Viewer module.
 */
function fp_loadRteHtml(html) {
  fp_AppHTMLArray_[FP_APP_ROUTE_EXPLORER] = html;
}

/**
 * fp_resized()
 * 
 * Called when the browser window is resized. Updates internal references to
 * current display area dimensions.
 */
function fp_resized() {  
  if(fp_IE_) {
    fp_winWidth_ = document.documentElement.clientWidth;
    fp_winHeight_ = document.documentElement.clientHeight;
  }
  else {
    fp_winWidth_ = window.innerWidth;
    fp_winHeight_ = window.innerHeight;
  }
  if(fp_GMap_ != null) fp_GMap_.checkResize();
}

/**
 * fp_selectApplication(appID)
 *
 * Activates the specified application module.
 */
function fp_selectApplication(appID) {
  fp_currentApp_ = appID;
  fp_dbg("app: "+fp_currentApp_);
  
  var app = document.getElementById("fp_app");
  //fp_dbg("app.innerHTML1: "+app.innerHTML);
  app.innerHTML = fp_AppHTMLArray_[appID];
  //fp_dbg("app.innerHTML2: "+app.innerHTML);
  if(fp_currentApp_ == FP_APP_TRIP_PLANNER) fp_initTrp();
  if(fp_currentApp_ == FP_APP_ROUTE_EXPLORER) fp_initRte();
}
