/* vim: set sw=2 ts=8 sts=2 sta et : */
/* UMN MapServer JavaScript Client functions - Dynamic HTML
 *   
 *   Authors:   Frank Koormann <frank.koormann@intevation.de>
 *              Silke Reimer <silke.reimer@intevation.de>
 *   Copyright: 2004, Intevation GmbH, Germany
 *		This program is free software under the GPL (>=v2)
 *		See the GNU General Public License at 
 *		http://www.gnu.org/copyleft/gpl.html 
 *		for more details.
 *
 *   Description: 
 *              The dynamic user feedback defines three different input
 *              types which can be used by the different buttons
 *              - box: does allow to draw a box while mouse button is
 *                pressed. Releasing the button leads to submission of the
 *                form
 *              - point: User can set one point which leads to immediate
 *                submission 
 *              - poly: User can draw a polyline until he
 *                o selects a new button
 *                o hits return -> form is submitted with inserted
 *                                 coordinates.
 *                o hits 'Esc' -> all coordinates are deleted
 *                he can delete the last coordinate by pressing 'r' or 'R'
 *                
 *              Available tools: zoomin, zoomout, recenter, fullextent, info, measure
 *
*/
      
// The core function to handle the different tool buttons
function setActiveTool(tool) {
    var buttonIcon;

    // Clean up
    if (activeTool != '') {
      buttonIcon = document.getElementById("button_" + activeTool);
      if (buttonIcon) {
        buttonIcon.src =  gpath + '/icon_' + activeTool + '_up.gif';
        document.getElementById('status_' + activeTool).style.visibility = 'hidden';
        activeTool = '';
      }
      // Set measure to false in case it hase been set
      measure = false;
      document.getElementById('measure').style.visibility = 'hidden';
//       document.getElementById('area').style.visibility = 'hidden';
    } 

    if (tool && buttonIcon) {
      buttonIcon = document.getElementById("button_" + tool);
      buttonIcon.src = gpath + '/icon_' + tool + '_down.gif';
    }

    if (tool == 'fullextent') {
      mapserv.mode.value = 'browse';
      mapserv.imgxy.value = '';
      mapserv.lastactivetool.value='zoomin';
      mapserv.submit();
    } else if (tool == 'zoomin') {
      mapserv.action.value  = 'zi'; // for Geo::UMNmapserver
      mapserv.mode.value = 'browse';
      mapserv.zoomdir.value = '1';
      mapserv.lastactivetool.value='zoomin';
      setActionType('box');
    } else if (tool == 'update') { 
      mapserv.mode.value    = 'browse';
      mapserv.zoomdir.value = '0';
      mapserv.lastactivetool.value = 'zoomin';
      submit();
    } else if (tool == 'zoomout') { 
      mapserv.action.value  = 'zo'; // for Geo::UMNmapserver
      mapserv.mode.value    = 'browse';
      mapserv.zoomdir.value = '-1';
      mapserv.lastactivetool.value = 'zoomout';
      setActionType('point');
    } else if (tool == 'recenter') {
      mapserv.mode.value    = 'browse';
      mapserv.action.value  = 'ce'; // for Geo::UMNmapserver
      mapserv.zoomdir.value = '0';
      mapserv.lastactivetool.value = 'recenter';
      setActionType('point');
    } else if (tool == 'info') {
      mapserv.action.value  = 'qx'; // for Geo::UMNmapserver
      mapserv.mode.value    = 'nquery';
      mapserv.lastactivetool.value = 'info';
      setActionType('point');
    }
    // needs a div named __MEAS__ to be defined in html-template
    else if (tool == 'measure') {
      measure = true;
      document.getElementById("measure").style.visibility = 'visible';
      setActionType('poly');
    }

    cellsize = mapserv.cellsize.value;
    activeTool = tool;
    var elem = document.getElementById('status_' + activeTool);
    if (elem) elem.style.visibility = 'visible';
  }

