/**
 * Create an EsriAction which creates a timer and does a callback every time the
 * timer completes.
 */
 
 
function CalcularEnergiaAction() {
  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.onmouseout = processMouseOut;
    //element.onmousemove = processMouseMove;
    element.onclick = processOnClick;
  
  	//taskWindowManager.windows['win_divDescargaDatos'].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_divDescargaDatos'].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 CalcularEnergiaAction and on callback, sends
 * a request to the server to the attributes for the map tip.
 */
function CalcularEnergiaTool(id, toolName) {
  this.inheritsFrom(new EsriMapToolItem(id, toolName, new CalcularEnergiaAction(), true));

  this.tipWin;
  this.tipTb;
  this.nombreTool = toolName;
  var self = this;

  this.init = function() {
  
    
  
  //********************************************
  //********************************************
  //********************************************
  //********************************************
   
  //********************************************
  //********************************************
  //********************************************
  //********************************************
  
   
  }

  this.deactivate = function() {
    if (this.action) this.action.deactivate();
    this.isActive = false;
    //hide tip windows when tool deactivated
//    this.tipWin.hide();
	//EsriControls.maps['map1'].clearCurrentToolItem();
	//taskWindowManager.windows['win_divDescargaDatos'].hide();
  }

  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 params = "parametro=calcularEnergia&formId=" + formId + "&mapId=" + map.id + "&x=" + point.x + "&y=" + point.y + "&" + EsriUtils.buildRequestParams(formId);
    //send AJAX request to server and pass callback function
    var xh = EsriUtils.sendAjaxRequest(url, params, true, function() { calcularEnergiaResponse(xh, map.id, point); });
	
  }



  function calcularEnergiaResponse(xh, mapId, point,longitud, latitud) {
  
  
  	if (xh != null && xh.readyState == 4 && xh.status == 200) {
  	
  		self.update();
  		var map = self.control;
		var xml = xh.responseXML;
	
mostrarAero(xh,point.x,point.y);		
					
		//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();
      	
     	//map.hideLoading();
  		if(document.getElementById(identifyLoadingIcon.id)) {
      		map.divObject.removeChild(document.getElementById(identifyLoadingIcon.id));
    	}
	}

  }


 // this.init();
  
  
}

  