﻿// We define the function first 
function GocciaControl() { 
} 
 
// To "subclass" the GControl, we set the prototype object to 
// an instance of the GControl object 
GocciaControl.prototype = new GControl(); 
 
// Creates a one DIV for each of the buttons and places them in a container 
// DIV which is returned as our control element. We add the control to 
// to the map container and return the element for the map class to 
// position properly.
 
GocciaControl.prototype.initialize = function(mapContainer) { 
  var container = document.createElement("div"); 
  container.id = "containerToolGoccia";
  
  
  var divGoccia = document.createElement("div"); 
  this.setButtonStyle_(divGoccia); 
  container.appendChild(divGoccia);
   
  divGoccia.appendChild(document.createTextNode(LANGUAGE.AnalisiIdraulica));
   
  GEvent.addDomListener(divGoccia, "click", function() {
    
    //controlli prima di creare un profilo
    //***************************************************************
    if (camerettaGoccia.length != 1)
    {
       alert(LANGUAGE.SelezionareUnaCameretta);
       return;
    }
    
    /*
    //controllo che il profilo non sia tra quelli già generati
    var hasGenerateProfilo = false;
    for (var i = 0; i < cameretteHistoryProfilo.length; i++)
    {
        if (((cameretteHistoryProfilo[i][0] == cameretteProfilo[0]) && (cameretteHistoryProfilo[i][1] == cameretteProfilo[1])) 
        || ((cameretteHistoryProfilo[i][0] == cameretteProfilo[1]) && (cameretteHistoryProfilo[i][1] == cameretteProfilo[0])))
        {
          hasGenerateProfilo = true;
          break;
        }
    }
    
    if (hasGenerateProfilo) return;
    */
    //***************************************************************
    
    
    $('#divElaborazioneGoccia').css({display : 'block'});
        
    
    //alert("http://" + nomeWebSito + "/" + pathUrlSchedaCameretta + "/GocciaService.svc/ArcSewer/Goccia?OIDCameretta=" + camerettaGoccia[0] + "&versoMonte=" + document.getElementById("chkversoMonte").checked + "&sfioroIn=" + document.getElementById("chkattivaSfiori").checked + "&serviceName=" + encodeURIComponent(nomeServizioSOE));
    $.ajax({
        type        : "GET", //GET or POST or PUT or DELETE verb
        url         : "http://" + nomeWebSito + "/" + pathUrlSchedaCameretta + "/GocciaService.svc/ArcSewer/Goccia?OIDCameretta=" + camerettaGoccia[0] + "&versoMonte=" + document.getElementById("chkversoMonte").checked + "&sfioroIn=" + document.getElementById("chkattivaSfiori").checked + "&serviceName=" + encodeURIComponent(nomeServizioSOE), // Location of the server
        contentType : "application/json; charset=utf-8", // content type sent to server
        dataType    : "json", //Expected data format from server
        processdata : false, //True or False
        success     : ServiceGoccia,
        error: ServiceFailedGoccia  // When Service call fails
    });
    
        
  });
  
  var divGocciaClear = document.createElement("div"); 
  this.setButtonStyle_(divGocciaClear); 
  container.appendChild(divGocciaClear);
   
  divGocciaClear.appendChild(document.createTextNode(LANGUAGE.RimuoviAnalisiIdraulica)); 
  GEvent.addDomListener(divGocciaClear, "click", function() {
        
       
       for (var i = 0; i < gListaGoccia.length; i++)
       { 
          mapExtension.removeFromMap(gListaGoccia[i]);
       }
       gListaGoccia=[];
       
       //elimina le camerette per i profili
       for (var i = 0; i < camerettaGocciaMarker.length; i++)
       { 
          map.removeOverlay(camerettaGocciaMarker[i]);
       }
             
       camerettaGocciaMarker=[];       
       camerettaGoccia=[];
       
              

  });
  
  var divattivaSfiori = document.createElement("div");
  container.appendChild(divattivaSfiori);

  var attivaSfiori = document.createElement("input");
  attivaSfiori.setAttribute('type','checkbox');
  attivaSfiori.setAttribute('id','chkattivaSfiori');
  divattivaSfiori.appendChild(attivaSfiori);
  
  var lblattivaSfiori = document.createElement("label");
  lblattivaSfiori.setAttribute('for','chkattivaSfiori');
  lblattivaSfiori.innerHTML = LANGUAGE.AttivaSfiori;
  divattivaSfiori.appendChild(lblattivaSfiori);
  
  var divversoMonte = document.createElement("div");
  container.appendChild(divversoMonte);

  var versoMonte = document.createElement("input");
  versoMonte.setAttribute('type','checkbox');
  versoMonte.setAttribute('id','chkversoMonte');
  
  divversoMonte.appendChild(versoMonte);
  
  var lblversoMonte = document.createElement("label");
  lblversoMonte.setAttribute('id','lblversoMonte');
  lblversoMonte.setAttribute('for','chkversoMonte');
  lblversoMonte.innerHTML = LANGUAGE.ReteValle;
  divversoMonte.appendChild(lblversoMonte);
  
  GEvent.addDomListener(versoMonte, "click", function() {
     
     $('#lblversoMonte').html(this.checked?LANGUAGE.ReteMonte:LANGUAGE.ReteValle);
     $('#chkattivaSfiori').attr('disabled',this.checked);
     if (this.checked)
       $('#chkattivaSfiori').attr('checked',this.checked);
     
  });
  
  
 
  
  var divElaborazioneGoccia = document.createElement("div");
  divElaborazioneGoccia.id = "divElaborazioneGoccia";
  divElaborazioneGoccia.style.display ="none";
  divElaborazioneGoccia.appendChild(document.createTextNode(LANGUAGE.ElaborazioneInCorso));
  container.appendChild(divElaborazioneGoccia); 
   
  mapContainer.getContainer().appendChild(container); 
  return container; 
  
}






var gListaGoccia = []; //lista profili generati sulla mappa

function ServiceGoccia(result,status)
{
     
     
     if (result.HasError)
     {
           alert(LANGUAGE.ErroreAnalisiIdraulica);
           $('#divElaborazioneGoccia').css({display:'none'});
     }
     else
     {
           var numeroCondotti = result.OIDsCondotti.length;
           if (numeroCondotti == 0)
           {
              $('#divElaborazioneGoccia').css({display:'none'});
                     
              return;
           }
           else if (numeroCondotti > numeroMaxSelezione)
           {
                var r = confirm(LANGUAGE.AttenzioneNumeroElementiMax); 
		if (!r)
		{ 
		     $('#divElaborazioneGoccia').css({display:'none'});
		     return;
		}
           }
           
           queryProfilo = new esri.arcgis.gmaps.Query();
           queryProfilo.returnGeometry = true;
           queryProfilo.where = OIDMaster + " in (" + result.OIDsCondotti.join(",") + ")";
           qtaskProfilo.execute(queryProfilo, false, createGoccia);
     }
}

function ServiceFailedGoccia(XMLHttpRequest, textStatus, errorThrown)
{
     
     alert(LANGUAGE.ErroreAnalisiIdraulica);
     $('#divElaborazioneGoccia').css({display:'none'});         
}

//callback del queryTask per crea profili
function createGoccia(fset) {
         
        
        var overlayOptions = {
          strokeColor: "#FF0000", //random_color('hex'), //"#FF0000",
          strokeWeight:3,
          strokeOpacity:0.75
        };
         
        var infoWindowOptions = {
           content:'<div style="text-align: center;"><br>' + LANGUAGE.VisualizzaAnalisiIdraulica + ' ' + currentFeature.attributes['NomeCameretta'] + '</div>'
        };
        
        
        gListaGoccia.push(mapExtension.addToMap(fset,overlayOptions,infoWindowOptions));
        $("#divElaborazioneGoccia").css({display:'none'});        
}






 
 
// By default, the control will appear in the top left corner of the 
// map with 7 pixels of padding. 
GocciaControl.prototype.getDefaultPosition = function() { 
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7)); 
} 
 
// Sets the proper CSS for the given button element. 
GocciaControl.prototype.setButtonStyle_ = function(button) { 
  button.style.textDecoration = "underline"; 
  button.style.color = "#0000cc"; 
  button.style.backgroundColor = "white"; 
  button.style.font = "small Arial"; 
  button.style.border = "1px solid black"; 
  button.style.padding = "2px"; 
  button.style.marginBottom = "3px"; 
  button.style.textAlign = "center"; 
  button.style.width = "9em"; 
  button.style.cursor = "pointer"; 
}
