//funcio que prepara l'objecte MapaGoogle
function MapaGoogle(div) {
  this.map = null;
  this.geocoder = null;
  this.divContainer = div;
}

//funcio que crea l'objecte MapaGoogle, inicialitza variables i li posa els controls pertinents
MapaGoogle.prototype.createMapaGoogle = function() {
  this.map = new GMap2(document.getElementById(this.divContainer));
  this.map.addControl(new GSmallMapControl());
  this.map.addControl(new GMapTypeControl());
  this.geocoder = new GClientGeocoder();
}

//funció que posa totes les variables a null
MapaGoogle.prototype.deshabilitaMapaGoogle = function() {
  this.map = null;
  this.geocoder = null;
}

//Mostra la div del contenidor i inicialitza l'objecte MapaGoogle
MapaGoogle.prototype.mostrarContenidor = function() {
  document.getElementById(this.divContainer).style.display="block";
  document.getElementById(this.divContainer).style.visibility="visible";
  this.createMapaGoogle();
}

//Amaga la div i deshabilita l'objecte MapaGoogle
MapaGoogle.prototype.amagarContenidor = function() {
  document.getElementById(this.divContainer).style.display="none";
  document.getElementById(this.divContainer).style.visibility="hidden";
  this.deshabilitaMapaGoogle();
}

//funcio que carrega qualsevol kml que li passis per la url
//posa el centre al centre de catalunya i amb un zoom prou gran perque es vegi sencera
//l'ideal seria que et busques el centre segons el kml --> s'haura d'investigar com fer-ho
MapaGoogle.prototype.carregaKml = function(url) {
  this.mostrarContenidor();
  this.map.setCenter(new GLatLng(41.727639, 1.832571), 7)
  
  this.map.clearOverlays();
  
  var kml = new GGeoXml(url);
  
  this.map.addOverlay(kml);
}

//Mostra un punt (aquest punt no es pot moure del mapa, es fixe)
MapaGoogle.prototype.showLocation = function(lat, lon) {
  this.mostrarContenidor();
  var gLatLon = new GLatLng(lat, lon, false);
  this.map.setCenter(gLatLon, 15);

  this.map.clearOverlays();
  
  var marker = new GMarker(gLatLon);

  this.map.addOverlay(marker);
}

//mostra un punt (aquest punt es pot agafar i arrossegar per el mapa)
//porta 2 events:
// dragstart, quan comença l'arrosegament tanca la finestra d'informacio
// dragend, quan acaba d'arrossegar, obre una finestra mostrant la latitud i longitud i posa a les caixes de text el valor de la posicio
MapaGoogle.prototype.showLocationLatLon = function(lat, lon) {
  this.mostrarContenidor();
  var gLatLon = new GLatLng(lat, lon);
  
  this.map.setCenter(gLatLon, 15);

  this.map.clearOverlays();
  
  var marker = new GMarker(gLatLon, {draggable: true});
  
  GEvent.addListener(marker, "dragstart", function() { marker.closeInfoWindow(); });
  GEvent.addListener(marker, "dragend", function() {
                    var lat = marker.getLatLng().lat();
                    var lng = marker.getLatLng().lng();
                    
                    marker.openInfoWindowHtml("Latitud:&nbsp;"+lat+"<br/>Longitud&nbsp;:"+lng);
                    
                    document.forms[0].latitudCal.value = lat;
                    document.forms[0].longitudCal.value = lng;
                  });

  this.map.addOverlay(marker);
}

// agafa l'adreça de la caixa de text i crid a la funcio de showLocationAdr
MapaGoogle.prototype.findLocationAdr = function() {
  var ad = document.forms[0].adreca.value;
  if (ad && ad!="") this.showLocationAdr(ad);
}

// Mostra el punt que conte les caixes de text (agafa el punt i crida a la funcio de showlocationLatLon)
MapaGoogle.prototype.findLocationLatLon = function() {
  var lat = document.forms[0].latitudCal.value;
  var lon = document.forms[0].longitudCal.value;
  if (lat && lat!="" && lon && lon!="")
  {
    this.showLocationLatLon(lat, lon);
  }
}

//aquesta funcio envia l'informacio adreca a google maps i busca possibles destinacions
MapaGoogle.prototype.showLocationAdr = function(adreca) {
  this.mostrarContenidor();
  if (adreca && adreca!="")
  {
    this.geocoder.getLocations(adreca, addAddressToMap);
  }
}

//Aquesta funcio rep les possibles destinacions de l'adreça i en mostra la primera (es pot arrossegar el punt)
function addAddressToMap (response) {
  gm.map.clearOverlays();
  if (!response || response.Status.code != 200) {
    alert("Sorry, we were unable to geocode that address");
  } else {

    var place = response.Placemark[0];
    var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);

    gm.map.setCenter(new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]), 15);

    var marker = new GMarker(point, {draggable: true});
    GEvent.addListener(marker, "dragstart", function() {
          marker.closeInfoWindow();
        });
    
    GEvent.bind(marker, "dragend", gm, function() {
          var lat = marker.getLatLng().lat();
          var lng = marker.getLatLng().lng();
          
          marker.openInfoWindowHtml("Latitud:&nbsp;"+lat+"<br/>Longitud&nbsp;:"+lng);
          
          document.forms[0].latitudCal.value = lat;
          document.forms[0].longitudCal.value = lng;
        });
        
    gm.map.addOverlay(marker);

    document.forms[0].latitudCal.value = place.Point.coordinates[1];
    document.forms[0].longitudCal.value = place.Point.coordinates[0];
  }
}