/**
 * Create an EsriAction which creates a timer and does a callback every time the
 * timer completes.
 */
 
function ObtenerPuntoAction() {
  this.inheritsFrom(new EsriAction());
  var element, callback, bounds;
  var timer;
  var timeout = 50;
  var self = this;

  this.activate = function(elem, cb) {
    element = elem;
    callback = cb;
    element.style.cursor = "pointer";
    element.onclick = processOnClick;
    
	taskWindowManager.windows['win_divBuscarCoordenadas'].show();
	 
  }

  this.deactivate = function() {
    clearTimeout(timer);
    if (element != null) {
      element.onmousemove = null;
      element.onclick = null;
      element.style.cursor = "default";
    }
    element = bounds = callback = null;
   
  // taskWindowManager.windows['win_divBuscarCoordenadas'].hide();
  }

  function processOnClick(e) {
 
 	if(element == null){
 	}
 	else{
 		bounds = EsriUtils.getElementPageBounds(element);
    	var pt = EsriUtils.getXY(e).offset(-bounds.left, -bounds.top);
    	callback(pt);
    }
  }


  function processMouseMove(e) {
 	if(element == null){
 	}
 	else{
	    bounds = EsriUtils.getElementPageBounds(element);
    	clearTimeout(timer);
    	var pt = EsriUtils.getXY(e).offset(-bounds.left, -bounds.top);
    	timer = setTimeout( function() { callback(pt); }, timeout);
    }
  }

  function processMouseOut(e) {
    clearTimeout(timer);
  }
}

/**
 * Create an EsriToolItem which uses the MostrarRosaAction and on callback, sends
 * a request to the server to the attributes for the map tip.
 */
function ObtenerPuntoTool(id, toolName) {
  this.inheritsFrom(new EsriMapToolItem(id, toolName, new ObtenerPuntoAction(), true));

  this.tipWin;
  this.tipTb;
  this.nombreTool = toolName;
  var self = this;

  this.init = function() {
  
    
  
  //********************************************
  //********************************************
  //********************************************
  //********************************************
 crearVentana('divBuscarCoordenadas', Res.getString("buscarCoordenadasTitulo"), 305, 80);
 tipWin = taskWindowManager.windows['win_divBuscarCoordenadas'];
 
  //********************************************
  //********************************************
  //********************************************
  //********************************************
  
   
  }

  this.deactivate = function() {
  //tipWin.hide();
    if (this.action) this.action.deactivate();
    this.isActive = false;
  }

  this.update = function() { self = this; }

  /**
   * Send request to server to retrieve attribute information for the point
   */
  this.postAction = function(point) {
    
 	self.update();
    var map = self.control;
    var formId = map.formId;
    var url = EsriUtils.getServerUrl(formId);

  	//map.showLoading();
    map.divObject.appendChild(identifyLoadingIcon);
    EsriUtils.setElementStyle(identifyLoadingIcon,
        "top:" + (point.y - 8) + "px;" +
        "left:" + (point.x - 8) + "px; display: block;");

    point = point.offset(-map.viewBounds.left, -map.viewBounds.top);
    
	var huso=document.getElementById("huso");
	if (huso.value!=null && huso.value!=""){
	
	    var params = "parametro=obtenerPunto&formId=" + formId + "&mapId=" + map.id + "&x=" + point.x + "&y=" + point.y +"&huso=" + huso.value + "&" + EsriUtils.buildRequestParams(formId);
	    //send AJAX request to server and pass callback function
	    var xh = EsriUtils.sendAjaxRequest(url, params, true, function() { obtenerPuntoResponse(xh, map.id, point); });
	}
  }



  function obtenerPuntoResponse(xh, mapId, point) {
  
  
  	if (xh != null && xh.readyState == 4 && xh.status == 200) {
  	
  		self.update();
  		var map = self.control;
    		

		var xmlTemp = xh.responseXML;
		var resultTags = xmlTemp.getElementsByTagName("result");
		var resultTag = resultTags.item(0);
		var details = resultTag.getElementsByTagName("detail");
		var detail = details.item(0);
		var lat = detail.getAttribute("lat");
		lat = Math.round(parseFloat(lat)*100)/100;
		var long = detail.getAttribute("lon");		
		long = Math.round(parseFloat(long)*100)/100;
		
		var x = document.getElementById("coordenadaX");
		var y = document.getElementById("coordenadaY");
		x.value=lat;
		y.value=long;
					
		//Refrescar el mapa y los graficos
		for (var i=0;i<map.mapSourceNames.length;i++) {
			map.mapSources[map.mapSourceNames[i]].update(map);
			map.mapSources[map.mapSourceNames[i]].updateImages(map.viewBounds);
		}
		map.updateWebGraphics();
   
  		if(document.getElementById(identifyLoadingIcon.id)) {
      		map.divObject.removeChild(document.getElementById(identifyLoadingIcon.id));
    	}
   
	}

  }
  
  
  
    this.abrirVentana = function() {
    	
    	taskWindowManager.windows['win_divBuscarCoordenadas'].show();	
    	mostrarAyuda("Obtener Punto");
    
  	}

}

  