/**
 * Copyright 2003, IMTEAM www.imteam.it
 * QUESTO PROGRAMMA E' COPERTO DA COPYRIGHT E NON PUO' ESSERE COPIATO O
 * MODIFICATO (NEMMENO PARZIALMENTE) SENZA ESPLICITO CONSENSO DEL PROPRIETARIO.
 * NON PUO' ESSERE UTILIZZATO AL DI FUORI DEL PRODOTTO CON CUI E' STATO FORNITO.
 */

//  DATA ULTIMA MODIFICA: 2003-10-17;
//  VERSIONE: 0.1-2003;
//  MODIFICATO: C.Lanza clanza@imteam.it;

// CVS: $Revision: 1.14 $  misto

var deltaX = 0;
var deltaY = 0;
var goniometring = false;

var loading = toolGetActualDate();

var panning = false;
var resizing = false; // se true e' in corso resize senza avere rilasciato il pulsante del
var tempToolMode = 0;
var toolButtaEvento = false;
var toolCurrent = '';
var toolBetweenDownAndUp = false;
var toolLocatorLeft = 0;
var toolLocatorTop = 0;
var toolLocatorRight = 0;
var toolLocatorBottom = 0;
var x1 = 0;
var x2 = 0;
var y1 = 0;
var y2 = 0;
var zooming = false;

var toolImageOn = new Array(); // immagini dei tools quando attivi
var toolImageOff = new Array(); // immagini dei tools quando non attivi
var toolDescrizioni = new Array(); // descrizioni dei tools
toolDescrizioni["bussola"]="Misura coordinate";
toolDescrizioni["edit"]="Editing";
toolDescrizioni[ "info"]="Informazioni";
toolDescrizioni[ "label_print"]="Stampa";
//toolDescrizioni["areainfo"]="Informazioni";
toolDescrizioni["pan"]="Pan";
toolDescrizioni["zoomfx"]="Estensione Massima";
toolDescrizioni["zoomin"]="Zoom in";
toolDescrizioni["areazoomin"]="Zoom in";
toolDescrizioni["zoomout"]="Zoom out";
toolDescrizioni["zoomlast"]="Zoom precedente";
toolDescrizioni["extinfo1"]="ExtInfo1";
toolDescrizioni["extinfo2"]="ExtInfo2";
toolDescrizioni["bookmarks"]="Bookmarks";
toolDescrizioni["highlight_clear"]="Cancella Highlight";
toolDescrizioni["ricerca_avanzata"]="Selezione avanzata";
toolDescrizioni["cartina"]="Locator";
toolDescrizioni["identify"]="Informazioni";
toolDescrizioni["telecamere"]="Telecamere";
toolDescrizioni["polygon"]="xxxxxxx";
toolDescrizioni["closevarchi"]="Chiudi varco";
toolDescrizioni["ricercaparticelle"]="Ricerca Particelle";
toolDescrizioni["ricercacivici"]="Ricerca Civici";
toolDescrizioni["aiuto"]="Help";



// Da una stringa tipo 111px restituisce 111
function toolGetNumberWithoutPx(s) {
  var retval = 1*s;
  if (s.substr(s.length-2, 2)=="px") {
    retval = 1*s.substr(0,s.length-2);
  }
  return retval;
}

// inizializzazione della pagina
function toolStartUpTemplate(needreload) {
  mapDebugMessage("toolStartUpTemplate");
  if (is.ns4 || is.ns6) {
    document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
  }
  document.onmousedown = toolGeneralMouseDown;
  document.onmousemove = toolGeneralMouseMove;
  document.onmouseup   = toolGeneralMouseUp;
  // inizializzo variabili
  mappaLeft = toolGetNumberWithoutPx(layerGetStyle('theMap').left); // horizontal image offset
  mappaTop = toolGetNumberWithoutPx(layerGetStyle('theMap').top); // vertical image offset
  mappaWidth = document.images['laMappa'].width;
  mappaHeight = document.images['laMappa'].height;
  //layerGetStyle('miniCartina').left = mappaLeft+2;
  //layerGetStyle('miniCartina').top = mappaTop+2;
  //toolLocatorLeft = toolGetNumberWithoutPx(layerGetStyle('miniCartina').left);
  //toolLocatorTop = toolGetNumberWithoutPx(layerGetStyle('miniCartina').top);
  //toolLocatorRight = toolLocatorLeft + toolGetNumberWithoutPx(layerGetStyle('miniCartina').width);
  //toolLocatorBottom = toolLocatorTop + toolGetNumberWithoutPx(layerGetStyle('miniCartina').height);
  // adatto la pagina all'area disponibile
  mapResize();
  if (needreload) {
    toolRefreshDoOp();
  }
}

// gestisce evento resize
function winResizeHandler(e) {
  if ( typeof(is)=="undefined" || is == null) {
    return true;
  }
  if (toolIsLoading()) {
    return;
  }
  if (is.ie4 || is.ie5) {
    e = window.event;
  }
  if (e.type == 'resize') {
    mapResize();
  }
  resizing = true;
}

function addhandlers(o) {
  init();
  o.onresize = winResizeHandler;
}


function toolAttivaImmagine(nomeImmagine,immagine) {
  // metto off tutte le immagine dei tool
  winChangeImage('zoominImg', Gimgpath + 'zoomin_off.gif');
  winChangeImage('zoomoutImg', Gimgpath + 'zoomout_off.gif');
  winChangeImage('panImg', Gimgpath + 'pan_off.gif');
  winChangeImage('infoImg', Gimgpath + 'info_off.gif');
  winChangeImage('extinfo1Img', Gimgpath + 'extinfo1_off.gif');
  winChangeImage('extinfo2Img', Gimgpath + 'extinfo2_off.gif');
  // metto on l'immagine del tool che voglio attivare
  if (nomeImmagine != "") {
    winChangeImage(nomeImmagine,immagine);
  }
}

function toolNegaVisibilitaLocator() {
  if (layerGetStyle('miniCartina').visibility == 'visible') {
    layerHide('miniCartina');
    layerHide('locator');
    document.processa.cartinaState.value = 'no';
  } else {
    layerShow('miniCartina');
    layerShow('locator');
    document.processa.cartinaState.value = 'si';
  }
}

function toolRicordaVisibilitaLocator() {
  if (document.processa.cartinaState.value == 'si') {
    layerShow('miniCartina');
    layerShow('locator');
  } else {
    layerHide('miniCartina');
    layerHide('locator');
  }
}

function toolSalvaStatoPreLocator() {
  alert("toolSalvaStatoPreLocator ï¿½deprecata.");
  oldTarget = document.processa.target;
  oldTypeRefresh = document.processa.type_refresh.value;
  oldOp = document.processa.op.value;
}

function toolDisattivaImmagine(idTool) {
  winChangeImage(idTool+'Img', toolImageOff[idTool]);
  document.processa.op.value = oldOp;
  document.processa.target = oldTarget;
  document.processa.type_refresh.value = oldTypeRefresh;
}


function toolSetOperazione(valore) {
  alert("toolSetOperazione("+valore+"): funzione deprecata. usare form_setAction.");
  document.processa.op.value = valore;
  document.processa.type_refresh.value = "image";
  document.processa.target = "loadImageFrame";
}

function toolDoPan(direction) {
  oldStatus = form_getStato();
  document.processa.panDirection.value = direction;
  form_setAction("pandir");
  toolLoad();
  form_restoreStato(oldStatus);
  toolButtaEvento = true;
}

function toolDoLocator() {
  mapDebugMessage("toolDoLocator");
  oldStatus = form_getStato();
  //document.processa.panDirection.value = direction;
  form_setAction("locator");
  toolLoad();
  form_restoreStato(oldStatus);
  toolButtaEvento = true;
}

function toolClearHighlight() {
  document.processa.SelectionLayerIds.value = "";
  document.processa.SelectionWhereClauses.value = "";
  document.processa.HighlightLayerIds.value = "";
  document.processa.HighlightWhereClauses.value = "";
  toolRefreshDoOp();
}

function toolGetActualDate() {
  return (new Date()).getTime();
}

function toolIsLoading() {
  /*if (loading != 0 && (loading-toolGetActualDate()) > 15000) {
    toolLoaded();
    return false;
  }*/
  return (loading != 0);
}

var loadingTimeoutId = 0;

function toolLoad() {
  var _status = form_getStato();
  mapDebugMessage("toolLoad:"+_status);
  if (toolIsLoading()) {
    alert("L'operazione precedente non è stata ancora completata.");
  }
  loading = toolGetActualDate();
  layerShow("loadingDiv"); // l'ho messa anche sulla onSubmit() della form
  document.processa.submit();
  loadingTimeoutId = setTimeout("toolLoadingTimeout()", 20000);
}

function toolLoaded() {
  clearTimeout(loadingTimeoutId);
  loading = 0;
  layerHide("loadingDiv");
}

function toolLoadingTimeout() {
  alert("L'operazione precedente non si è completata nel tempo massimo. \nE' possibile attendere ancora o proseguire con altre operazioni.\n");
  toolLoaded();
}


/****************************************************************
 * CONFIGURAZIONE DEGLI HANDLERS
 * Ci sono azioni immediate (tipo zoomfx) che definiscono il solo handler doop,
 * ed azioni che necessitano di azioni utente (tipo zoomin) che definiscono cinque
 * metodi: start, stop, mousedown, mousemove, mouseup
 */
var toolHandlers = new Object();

toolHandlers.bookmarks = new Object();
toolHandlers.bookmarks.doop    = toolBookmarksDoOp;

toolHandlers.bussola = new Object();
toolHandlers.bussola.start = toolLongActionStart; //toolBussolaStart;
toolHandlers.bussola.stop  = toolLongActionStop;
toolHandlers.bussola.down  = toolBussolaDown;
toolHandlers.bussola.move  = toolBussolaMove;
toolHandlers.bussola.up    = toolBussolaUp;

toolHandlers.cartina = new Object();
toolHandlers.cartina.doop    = toolCartinaDoOp;

toolHandlers.info = new Object();
toolHandlers.info.start = toolLongActionStart;
toolHandlers.info.stop  = toolLongActionStop;
toolHandlers.info.down  = toolInfoDown;
toolHandlers.info.move  = toolMoveCaptureMousePos;
toolHandlers.info.up    = toolInfoUp;

//alert("lllllll");
toolHandlers.telecamere = new Object();
toolHandlers.telecamere.start = toolLongActionStart;
toolHandlers.telecamere.stop  = toolLongActionStop;
toolHandlers.telecamere.down  = toolInfoDown;
toolHandlers.telecamere.move  = toolMoveCaptureMousePos;
toolHandlers.telecamere.up    = toolInfoUp;


toolHandlers.polygon = new Object();
toolHandlers.polygon.start = toolLongActionStart;
toolHandlers.polygon.stop  = toolLongActionStop;
toolHandlers.polygon.down  = toolInfoDown;
toolHandlers.polygon.move  = toolMoveCaptureMousePos;
toolHandlers.polygon.up    = toolInfoUp;


toolHandlers.closevarchi = new Object();
toolHandlers.closevarchi.start = toolLongActionStart;
toolHandlers.closevarchi.stop  = toolLongActionStop;
toolHandlers.closevarchi.down  = toolInfoDown;
toolHandlers.closevarchi.move  = toolMoveCaptureMousePos;
toolHandlers.closevarchi.up    = toolInfoUp;




toolHandlers.pan = new Object();
toolHandlers.pan.start = toolLongActionStart;
toolHandlers.pan.stop  = toolLongActionStop;
toolHandlers.pan.down  = toolPanDown;
toolHandlers.pan.move  = toolPanMove;
toolHandlers.pan.up    = toolPanUp;

toolHandlers.scala = new Object();
toolHandlers.scala.doop    = toolScalaDoOp;

toolHandlers.refresh = new Object();
toolHandlers.refresh.doop    = toolRefreshDoOp;

toolHandlers.highlight_clear = new Object();
toolHandlers.highlight_clear.doop = toolClearHighlight;

toolHandlers.zoomfx = new Object();
toolHandlers.zoomfx.doop = toolZoomFxDoOp;

toolHandlers.zoomin = new Object();
toolHandlers.zoomin.down  = toolZoomInDown;
toolHandlers.zoomin.move  = toolMoveCaptureMousePos;
toolHandlers.zoomin.start = toolZoomInStart;
toolHandlers.zoomin.stop  = toolLongActionStop; //toolZoomInStop;
toolHandlers.zoomin.up    = toolZoomInUp;

toolHandlers.zoomout = new Object();
toolHandlers.zoomout.down  = toolZoomOutDown;
//toolHandlers.zoomout.move  = toolZoomOutMove;
toolHandlers.zoomout.start = toolZoomOutStart;
toolHandlers.zoomout.stop  = toolZoomOutStop;
//toolHandlers.zoomout.up    = toolZoomOutUp;

toolHandlers.zoomlast = new Object();
toolHandlers.zoomlast.doop = toolImmediateActionDoOp;


// Questo (e tutti i precedenti) sono da mettere fuori!
toolHandlers.extinfo1 = new Object();
if ("undefined"!=typeof(toolExtInfo1Down)) toolHandlers.extinfo1.down  = toolExtInfo1Down;
if ("undefined"!=typeof(toolExtInfo1Move)) toolHandlers.extinfo1.move  = toolExtInfo1Move;
if ("undefined"!=typeof(toolExtInfo1Start)) toolHandlers.extinfo1.start = toolExtInfo1Start;
if ("undefined"!=typeof(toolExtInfo1Stop)) toolHandlers.extinfo1.stop  = toolExtInfo1Stop;
if ("undefined"!=typeof(toolExtInfo1Up)) toolHandlers.extinfo1.up    = toolExtInfo1Up;
// Questo (e tutti i precedenti) sono da mettere fuori!
toolHandlers.extinfo2 = new Object();
if ("undefined"!=typeof(toolExtInfo2Down)) toolHandlers.extinfo2.down  = toolExtInfo2Down;
if ("undefined"!=typeof(toolExtInfo2Move)) toolHandlers.extinfo2.move  = toolExtInfo2Move;
if ("undefined"!=typeof(toolExtInfo2Start)) toolHandlers.extinfo2.start = toolExtInfo2Start;
if ("undefined"!=typeof(toolExtInfo2Stop)) toolHandlers.extinfo2.stop  = toolExtInfo2Stop;
if ("undefined"!=typeof(toolExtInfo2Up)) toolHandlers.extinfo2.up    = toolExtInfo2Up;
// Questo (e tutti i precedenti) sono da mettere fuori!
toolHandlers.identify = new Object();
if ("undefined"!=typeof(toolIdentifyDown)) toolHandlers.identify.down  = toolIdentifyDown;
if ("undefined"!=typeof(toolIdentifyMove)) toolHandlers.identify.move  = toolIdentifyMove;
if ("undefined"!=typeof(toolIdentifyStart)) toolHandlers.identify.start = toolIdentifyStart;
if ("undefined"!=typeof(toolIdentifyStop)) toolHandlers.identify.stop  = toolIdentifyStop;
if ("undefined"!=typeof(toolIdentifyUp)) toolHandlers.identify.up    = toolIdentifyUp;

if ( typeof(wantFlash)!="undefined" && wantFlash && flashIsDetected() ) {
  toolHandlers.edit = new Object();
  if ("undefined"!=typeof(flashEditStart)) toolHandlers.edit.start = flashEditStart;
  if ("undefined"!=typeof(flashEditStop)) toolHandlers.edit.stop  = flashEditStop;
  //if ("undefined"!=typeof(flashEditDown)) toolHandlers.edit.down  = flashEditDown;
  //if ("undefined"!=typeof(flashEditMove)) toolHandlers.edit.move  = flashEditMove;
  //if ("undefined"!=typeof(flashEditUp)) toolHandlers.edit.up    = flashEditUp;
}

function toolButtonHandlerStart(idOggetto) {
  alert("Deprecata, usare toolStartOp/toolDoOp.");
  toolHandlers[idOggetto].start(idOggetto);
  return false;
}

/**
 * Se chiamo direttamente la funzione sull'array,
 * quando l'elemento non c'e' si genera l'errore FASTIDIOSISSIMO "toolHandlers[...] ? nullo o non ? un oggetto"
 *  quindi chiamo questa funzione cha fa tutti i controlli
 */
function toolDoOp(_op)
{
  var currHand = toolHandlers[_op];
  if ("undefined" != typeof(currHand) && "undefined" != typeof(currHand["doop"])) {
    currHand.doop(_op);
  } else {
    // debug
    alert("toolDoOp("+_op+") non definito");
  }
}

/**
 * Se chiamo direttamente la funzione sull'array,
 * quando l'elemento non c'e' si genera l'errore FASTIDIOSISSIMO "toolHandlers[...] ? nullo o non ? un oggetto"
 *  quindi chiamo questa funzione cha fa tutti i controlli
 */
function toolStartOp(_op)
{
  mapDebugMessage("toolStartOp("+_op+")");
  var currHand = toolHandlers[_op];
  if ("undefined" != typeof(currHand) && "undefined" != typeof(currHand["start"])) {
    currHand.start(_op);
  } else {
    // debug
    alert("toolStartOp("+_op+") non definito");
  }
}

/**
 * Questa funzione chiama lo stop tramite toolHandlers, verificandone l'esistenza.
 * In genere la funzione ? chiamata da tutti gli start dei tool che prendono controllo della
 * form per gestire azioni utente (vedi zoom, pan, etc...), MA NON dai tool che agiscono immediatamente
 * e poi lasciano la form cos? come l'hanno trovata (e.g. zoomfx, refresh, etc...).
 */

function toolStopOp(_op)
{
  var currHand = toolHandlers[_op];
  if ("undefined" != typeof(currHand) && "undefined" != typeof(currHand["stop"])) {
    currHand.stop(_op);
  }
}

/**
 * Adesso definisci alcuni handlers generici
 */

/**
 * start di una azione "lunga"
 * aspetto interazione utente (e.g. un click sulla mappa), quindi stoppo l'op precedente,
*  setto la form per l'op corrente e accendo l'immagine
 */
function toolLongActionStart(idObj) {
  toolStopOp(window.document.processa.op.value);
  form_setAction(idObj);
  toolAttivaImmagine(idObj + 'Img',Gimgpath + idObj + '_on.gif');
  return false;
}

/**
 * stop di una azione "lunga"
 * spengo l'immagine.
 */
function toolLongActionStop(idObj) {
 // toolDisattivaImmagine(idObj);
  toolAttivaImmagine((idObj) + 'Img',Gimgpath + (idObj) + '_off.gif');
  return false;
}

function toolImmediateActionDoOp(idObj) {
  toolAttivaImmagine(idObj);
  mapDebugMessage("toolImmediateActionDoOp:"+idObj);
  oldStatus = form_getStato();
  form_setAction(idObj)
  toolLoad();
  //toolLoaded();
  form_restoreStato(oldStatus);
  return false;
  //return false;
}

function toolImmediateActionDoOp_NoWait(idObj) {
  mapDebugMessage("toolImmediateActionDoOp:"+idObj);
  oldStatus = form_getStato();
  form_setAction(idObj)
  toolLoad();
  toolLoaded();
  form_restoreStato(oldStatus);
  return false;
}

/**
 * Adesso definisci gli handlers specifici
 */
function toolBookmarksDoOp(idObj) {
  mapDebugMessage("toolBookmarksDoOp");
  open_Map_bookmarks();
  return false;
}

function toolBussolaDown(evento) {
  if (toolButtaEvento) {
    return false;
  } else {
    startGoniometro(evento);
    return false;
  }
}

function toolBussolaMove(evento) {
  if (toolButtaEvento) {
    return false;
  } else {
    if (!goniometring) {
      return false;
    }
    var mouseString = "";
    x2=mouseX;
    y2=mouseY;
    goniometro();
    var g_coord = mapPixel2Coord(mouseX, mouseY);
    var g_origcoord = mapPixel2Coord(x1, y1);
    mouseString += "Coordinate GB : " + g_coord[0] + ", " + g_coord[1];
    mouseString += " - Direzione (Th): " + Math.round(getDirezioneCardinale(x2-x1,y2-y1)*100)/100. + " - Distanza (m): " +
                   Math.round(Math.sqrt((g_coord[0]-g_origcoord[0])*(g_coord[0]-g_origcoord[0])+(g_coord[1]-g_origcoord[1])*(g_coord[1]-g_origcoord[1])));
    window.status = mouseString;
    return false;
  }
}


function toolBussolaUp(evento) {
  if (toolButtaEvento) {
    toolButtaEvento = false;
    return false;
  } else {
    var g_extent = mapGetExtent();
    stopGoniometro(evento); // questa chiama setAction e processa
    return false;
  }
}


function toolCartinaDoOp(idObj) {
  toolNegaVisibilitaLocator();
  return false;
}

/**************************************************/

function toolInfoDown(evento) {
  if (toolButtaEvento) {
    return false;
  } else {
    mapStartZoomBox(evento);
    return false;
  }
}

function toolImmediateInfoDown(evento) {
  if (toolButtaEvento) {
    return false;
  } else {
    map_BuildPointSmallEnvelope(evento);
    infowin = window.open("","infoWindow","width=600,height=450,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=1,resizable=1");
    form_setAction("info");
    toolLoad();
    return false;
  }
}

function toolInfoUp(evento) {
  mapDebugMessage("toolInfoUp");
  if (toolButtaEvento) {
    toolButtaEvento = false;
    return false;
  } else {
     infowin = window.open("","infoWindow","width=600,height=480,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=1,resizable=1");
   
    mapStopZoomBox(evento);
    return false;
  }
}

function toolExtInfoUp(evento) {
  mapDebugMessage("toolExtInfoUp");
  if (toolButtaEvento) {
    toolButtaEvento = false;
    return false;
  } else {
    /* DC 03-11-2003 */
    infowin = window.open("","infoWindow","width=600,height=450,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=1,resizable=1");
    document.processa.target="infoWindow";
    document.processa.type_refresh.value="extern";
    mapStopZoomBox(evento);
    return false;
  }
}

function toolPanDown(evento) {
  if (toolButtaEvento) {
//    toolButtaEvento = false;
    return false;
  } else {
    mapStartPan(evento);
    return false;
  }
}

function toolPanMove(evento) {
  if (toolButtaEvento) {
//    toolButtaEvento = false;
    return false;
  } else {
   if (panning) {
      mapMakePan();
    }
    return false;
  }
}


function toolPanUp(evento) {
  if (toolButtaEvento) {
    toolButtaEvento = false;
    return false;
  } else {
    mapStopPan(evento);
    return false;
  }
}

function toolRefreshDoOp(idObj) {
  mapDebugMessage("toolRefreshDoOp");
  if ("undefined"==typeof(idObj)) {
    idObj="refresh";
  }
  oldStatus = form_getStato();
  form_setAction(idObj)
  toolLoad();
  form_restoreStato(oldStatus);
  return false;
}

function toolScalaDoOp(idObj) {
  mapDebugMessage("toolScalaDoOp:"+idObj);
  oldStatus = form_getStato();
  form_setAction(idObj)
  toolLoad();
  form_restoreStato(oldStatus);
  return false;
}

function toolSelezioneClearDoOp(idObj) {
  document.processa.SelectionLayerIds.value = "";
  document.processa.SelectionWhereClauses.value = "";
  document.processa.HighlightLayerIds.value = "";
  document.processa.HighlightWhereClauses.value = "";
  toolRefreshDoOp("refresh");
  return false;
}

function toolZoomFxDoOp(idObj) {
  return toolImmediateActionDoOp(idObj);
}

function toolZoomInDown(evento) {
  if (toolButtaEvento) {
//    toolButtaEvento = false;
    return false;
  } else {
    mapStartZoomBox(evento);
    return false;
  }
}

function toolMoveCaptureMousePos(evento) {
  if (toolButtaEvento) {
//    toolButtaEvento = false;
    return false;
  } else {
    if (!zooming) {
      return false;
    }
    var mouseString = "";
    x2 = mouseX;
    y2 = mouseY;
    mapReorderCoords();
    //mouseString += "Estensione : [" + (x1-mappaLeft) + "," + (y1-mappaTop) + " x " + (x2-mappaLeft) + "," + (y2-mappaTop) + "]    ";
    var g_coord = mapPixel2Coord(mouseX,mouseY);
    var g_origcoord = mapPixel2Coord(x1 ,y1);
    mouseString += "Coordinate del mouse: " + g_coord[0] + ", " + g_coord[1];
    window.status = mouseString;
    return false;
  }
}

function toolZoomInStart(idObj) {
  toolStopOp(window.document.processa.op.value);
  form_setAction(idObj);
  toolAttivaImmagine((idObj) + 'Img',Gimgpath + (idObj) + '_on.gif');
  return false;
}


function toolZoomInUp(evento) {
  if (toolButtaEvento) {
    toolButtaEvento = false;
    return false;
  } else {
    mapStopZoomBox(evento);
    return false;
  }
}

function toolZoomOutDown(evento) {
  if (toolButtaEvento) {
    toolButtaEvento = false;
    return false;
  } else {
    mapMakeZoomOut();
    return false;
  }
}


function toolZoomOutStart(idObj) {
  toolStopOp(window.document.processa.op.value);
  form_setAction(idObj);
  toolAttivaImmagine((idObj) + 'Img',Gimgpath + (idObj) + '_on.gif');
  return false;
}

function toolZoomOutStop(idObj) {
  toolAttivaImmagine((idObj) + 'Img',Gimgpath + (idObj) + '_off.gif');
  return false;
}


function toolGeneralMouseDown(e) {
  winCursorPosition(e);
  window.status="toolGeneralMouseDown: overmap: "+toolMouseOverMap()+", overlocator: "+toolMouseOverLocator()+", current op: "+window.document.processa.op.value;
  if (toolMouseOverMap() && !toolMouseOverLocator()) {
    toolBetweenDownAndUp = true;
    if (typeof(toolHandlers[window.document.processa.op.value])!="undefined") {
      if (typeof(toolHandlers[window.document.processa.op.value]["down"])!="undefined") {
        return (toolHandlers[window.document.processa.op.value].down(e));
      }
    }
  } else {
    return true;
  }
}

function toolGeneralMouseMove(e) {
  winCursorPosition(e);
  if (toolMouseOverMap() && !toolMouseOverLocator()) {
    var g_coord1 = mapPixel2Coord(mouseX, mouseY);
    window.status = "X:"+g_coord1[0]+", Y:"+g_coord1[1];
    if (typeof(toolHandlers[window.document.processa.op.value])!="undefined") {
      if (typeof(toolHandlers[window.document.processa.op.value]["move"])!="undefined") {
        return (toolHandlers[window.document.processa.op.value].move(e));
      }
    }
  } else if (!toolMouseOverMap() && toolMouseOverLocator()) {
      return true;
  } else {
    if (resizing == true) {
      resizing = false;
      toolRefreshDoOp();
    }
    if (toolBetweenDownAndUp) {
      toolBetweenDownAndUp = false;
      if (typeof(toolHandlers[window.document.processa.op.value])!="undefined") {
        if (typeof(toolHandlers[window.document.processa.op.value]["up"])!="undefined") {
          return (toolHandlers[window.document.processa.op.value].up(e));
        }
      }
    }
    return true;
  }
}

function toolGeneralMouseUp(e) {
  toolBetweenDownAndUp = false;
  winCursorPosition(e);
  if (toolMouseOverMap() && !toolMouseOverLocator()) {
    if (typeof(toolHandlers[window.document.processa.op.value])!="undefined") {
      if (typeof(toolHandlers[window.document.processa.op.value]["up"])!="undefined") {
        return (toolHandlers[window.document.processa.op.value].up(e));
      }
    }
  } else {
    return true;
  }
}

function toolMouseOverMap() {
  return (!winMouseOnScrollbars(mouseX,mouseY) && (mouseX > mappaLeft) && (mouseX < (mappaWidth+mappaLeft)) && (mouseY > mappaTop) && (mouseY < (mappaHeight+mappaTop)));
}

function toolMouseOverLocator() {
  if (layerIsVisibile("miniCartina")) {
    return ((mouseX >= toolLocatorLeft) && (mouseX <= toolLocatorRight) && (mouseY >= toolLocatorTop) && (mouseY <= toolLocatorBottom));
  } else {
    return false;
  }
}
