 /******************************************************************************
 *
 *  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/>.
 *  
 ******************************************************************************/  
 
/**
 * fp_layout.js
 * 
 * Declarations and definitions for managing the overall layout of the web
 * application (showing/hiding/resizing panels, etc.)
 */

// FIELD DECLARATIONS

var fp_topoDiv_; // a reference to the <div> element containing the topo-graph panel
var fp_topoMode_; // (Number); 1 if maximized, 2 if minimized 

var fp_topoDivH_; // the height of the topo-graph in pixels
// FUNCTION DEFINITIONS


/**
 * fp_initLayout()
 * 
 * Initializes the layout module. Called once upon page load/reload.
 */
function fp_initLayout() {
  fp_topoMode_ = 0;
  fp_topoDivH_ = 160;
}

/**
 * fp_createTopoGraph()
 * 
 * Initializes the trip topographic profile display, occupying a new panel
 * inserted immediately below the main map panel (which itself is contracted
 * vertically to make room). The panel includes a "minimize" tab allowing the
 * user to hide the topo graph and restore the map panel to its full size. 
 */
function fp_createTopoGraph() {
  if(fp_topoMode_ > 0) return;
  
  document.getElementById("fp_map").style.bottom = (fp_topoDivH_+20)+"px";
  document.getElementById("fp_tip").style.bottom = fp_topoDivH_+"px";

  fp_topoDiv_ = document.createElement("div");
  fp_topoDiv_.id = "fp_topoGraph";

  var topoTabDiv = document.createElement("div");
  topoTabDiv.id = "fp_topoTab";
  topoTabDiv.innerHTML = "<a href='javascript:fp_minimizeTopoGraph()'>HIDE TOPO</a>";
  /*if (topoTabDiv.addEventListener) {
    topoTabDiv.addEventListener('click', fp_minimizeTopoGraph, false); 
  } else if (topoTabDiv.attachEvent) {
    topoTabDiv.attachEvent('onclick', fp_minimizeTopoGraph);
  }*/

  var body = document.getElementsByTagName("body")[0];
  body.appendChild(fp_topoDiv_);
  body.appendChild(topoTabDiv);
  fp_topoMode_ = 2;
  fp_GMap_.checkResize();
}

/**
 * fp_removeTopoGraph()
 * 
 * Completely removes the topo graph panel from the display (compare to 
 * fp_minimizeTopoGraph(), which only minimizes the panel while keeping an
 * active reference to it). 
 */ 
function fp_removeTopoGraph() {
  if(fp_topoMode_ == 0) return;
  
  //alert("hide");
  //var tabDiv = document.getElementById("fp_topoTab");
  document.body.removeChild(document.getElementById("fp_topoTab"));
  if(fp_topoMode_ == 2) document.body.removeChild(fp_topoDiv_);
  document.getElementById("fp_map").style.bottom = "20px";
  document.getElementById("fp_tip").style.bottom = "0px";
  fp_topoMode_ = 0;
}

/**
 * fp_minimizeTopoGraph() 
 *
 * Minimizes the topo graph display to a small tab in the status bar at the
 * bottom of map panel.
 */
function fp_minimizeTopoGraph() {
  if(fp_topoMode_ != 2) return;
  var topoTabDiv = document.getElementById("fp_topoTab");
  topoTabDiv.style.bottom = "0px";
  topoTabDiv.innerHTML = "<a href='javascript:fp_maximizeTopoGraph()'>SHOW TOPO</a>";
  
  document.body.removeChild(fp_topoDiv_);
  
  document.getElementById("fp_map").style.bottom = "20px";
  document.getElementById("fp_tip").style.bottom = "0px";
  fp_topoMode_ = 1;
  
  fp_GMap_.checkResize();
  fp_GMap_.setCenter(fp_tmLastBounds_.getCenter(), fp_GMap_.getBoundsZoomLevel(fp_tmLastBounds_));
    
}

/**
 * fp_maximizeTopoGraph()
 *
 * Restores the topo graph panel to a fully visible state.
 */
function fp_maximizeTopoGraph() {
  if(fp_topoMode_ != 1) return;
  var topoTabDiv = document.getElementById("fp_topoTab");
  topoTabDiv.style.bottom = fp_topoDivH_+"px";
  topoTabDiv.innerHTML = "<a href='javascript:fp_minimizeTopoGraph()'>HIDE TOPO</a>";
  /*if (topoTabDiv.addEventListener) {
    topoTabDiv.addEventListener('click', fp_minimizeTopoGraph, false); 
  } else if (topoTabDiv.attachEvent) {
    topoTabDiv.attachEvent('onclick', fp_minimizeTopoGraph);
  }*/ 
  document.body.appendChild(fp_topoDiv_);
  
  document.getElementById("fp_map").style.bottom = (fp_topoDivH_+20)+"px";
  document.getElementById("fp_tip").style.bottom = fp_topoDivH_+"px";
  fp_topoMode_ = 2;
  
  fp_GMap_.checkResize();
  fp_GMap_.setCenter(fp_tmLastBounds_.getCenter(), fp_GMap_.getBoundsZoomLevel(fp_tmLastBounds_));
}

/**
 * fp_showTip(tip)
 * 
 * Displays a short text-based "tip" in the status bar at the bottom of the
 * map panel.
 */
function fp_showTip(tip) {
  document.getElementById("fp_tip").innerHTML = "<b>TIP</b> "+tip;
}


function fp_contractOutput() {
}

function fp_uncontractOutput() {
}


