
//window.onload ="initialize()";
window.onunload ="GUnload()";

    var ICONS = [];
    var map = null;
    var IconPontos = null;
    var arrPontos = [];
    var marker = null;


// Create the Google Map to be used.
function initialize() {
	//alert("abriu")
  if (!GBrowserIsCompatible()) {
    alert('Your browser is not compatible with the Google Maps API');
    return;
  }

  map = new GMap2(document.getElementById("mapinha"));
  map.setCenter(new GLatLng(37.1962, -7.4662), 12);
  map.addControl(new GSmallMapControl());
  map.addControl(new GMapTypeControl());
   map.disableDoubleClickZoom();
   ///----- codigo novo
   
   
   function mapaControl() {
		}

		// To "subclass" the GControl, we set the prototype object to
		// an instance of the GControl object
	/*	mapaControl.prototype = new GControl();

		mapaControl.prototype.initialize = function(map) {
		  var container = document.createElement("div");
		  
		   var limparDiv = document.createElement("div");
		  this.setButtonStyle_(limparDiv);
		  container.appendChild(limparDiv);
		  limparDiv.appendChild(document.createTextNode("Limpar Mapa"));
		  GEvent.addDomListener(limparDiv, "click", function() {
		    clearMap();
		  });

		  map.getContainer().appendChild(container);
		  return container;
		}

		// By default, the control will appear in the top left corner of the
		// map with 7 pixels of padding.
		mapaControl.prototype.getDefaultPosition = function() {
		  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(50, 7));
		}

		// Sets the proper CSS for the given button element.
		mapaControl.prototype.setButtonStyle_ = function(button) {
		  //button.style.textDecoration = "underline";
		  button.style.color = "#046380";
		  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 = "6em";
		  button.style.cursor = "hand";
		}
		
			GMapTypeControl.setButtonStyle_ = function(button) {
			  //button.style.textDecoration = "underline";
			  button.style.color = "#046380";
			  
			}
		

			
			map.addControl(new mapaControl());*/
   

desenhaPonto();
}
    
function criaIco (img, point)
{
    var ico = new GIcon()
    ico.image = ""+img;
    //tinyIcon.shadow = "images\mapa\localizacao.png";
    ico.iconSize = new GSize(35, 28);
    ico.iconAnchor = new GPoint(30, 23);
    //ico.infoWindowAnchor = new GPoint(5, 1);
        
    newMarker = new GMarker(point,
    {icon: ico,
     draggable: true});

  return newMarker;
}    


function desenhaPonto()
{
//	GEvent.clearListeners(map, "click");
	Lat =  document.getElementById("coord").value;
	//Lng = document.getElementById("form1_longitude").value;

 if(Lat != ""&& Lat != null){
	 
	var vect= Lat.split(",");
	
     	  var point = new GLatLng(vect[0], vect[1]);   

 		map.setCenter(new GLatLng(vect[0], vect[1]), 14);
		map.savePosition();

    if (marker == null) {
      marker = criaIco("select.png", point);
      marker.enableDragging();

      GEvent.addListener(marker, "drag", function() {
       document.getElementById("coord").value = marker.getPoint().y +" , "+marker.getPoint().x;
      });

      map.addOverlay(marker);
    } else {
      marker.setPoint(point);
    }
	}
	else{



  GEvent.addListener(map, "click", function(overlay, point) {
       document.getElementById("coord").value = point.y +" , "+point.x;

    if (marker == null) {
      marker = criaIco("select.png", point);
      marker.enableDragging();

      GEvent.addListener(marker, "drag", function() {
	 
        document.getElementById("coord").value = marker.getPoint().y +" , "+marker.getPoint().x;
      });

      map.addOverlay(marker);
    } else {
      marker.setPoint(point);
    }
  });
	}
	}
	
///------------ new code ---------


function Ponto()
{
	action ="ponto";
	clearMap() ;
	desenhaPonto();
}
	

function addIcon(icon) { // Add icon attributes

 icon.iconSize = new GSize(35, 28);
 //icon.shadowSize = new GSize(22, 20);
 icon.iconAnchor = new GPoint(6, 20);
 icon.infoWindowAnchor = new GPoint(5, 1);
}

function clearMap() {

 // Clear current map and reset arrays
 marker = null;
 map.clearOverlays();
 points.length = 0;
 markers.length = 0;

 count = 0;
 document.getElementById("coord").value = "";
// document.getElementById("form1_longitude").value = "";
}
//--------------------------------localiza morada -----------------------------------
var geocoder = new GClientGeocoder();


function busca(){
	var morada = document.getElementById("morada_mapa").value;
	var localidade = document.getElementById("freguesia_mapa").value;
	if(localidade == "Outra")
		localidade = " ";
		
	var texto="";
	if(morada != null && morada!= ""){
	texto = morada + ", " + localidade;
        var pesquisa = texto + ", vila real santo antonio, Portugal";        
        zoom = 15;         
        //geocoder.getLocations(pesquisa, addToMap) 
	}
	else
	{
	var pesquisa = "";
	alert("Por favor indique a sua morada e localidade")	
	}
	if(pesquisa!=""){
		 geocoder.getLatLng(
          pesquisa,
          function(point) {
            if (!point) {
              alert("A morada : " + texto + " não foi encontrada");
            } else {
				
				map.setCenter(point, 16);
				if (marker == null) {

					marker = criaIco("select.png", point);
					marker.enableDragging();

					GEvent.addListener(marker, "drag", function() {
	 
					document.getElementById("coord").value = marker.getPoint().y +" , "+marker.getPoint().x;
																
																});
					document.getElementById("coord").value = marker.getPoint().y +" , "+marker.getPoint().x;

					map.addOverlay(marker);
				}
				else
				{
					marker.setPoint(point);	
					document.getElementById("coord").value = marker.getPoint().y +" , "+marker.getPoint().x;
				}
            }
          }
        );		
	}
}


      function addToMap(response)
       {
       if(response.Status.code!=200)
       {
         alert("Não foi possivel encontrar a morada pretendida.\n");
       }
       else{   
      // Retrieve the object
      place = response.Placemark[0];
      // Retrieve the latitude and longitude      
      point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);

      map.setCenter(point, zoom);
      
	   if (marker == null) {
      marker = criaIco("select.png", point);
      marker.enableDragging();

      GEvent.addListener(marker, "drag", function() {
       document.getElementById("coord").value = marker.getPoint().y +" , "+marker.getPoint().x;
      });
}
	  
      marker.setLatLng(point)
      }
   
      }


  function showAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 13);
              var marker = new GMarker(point);
              map.addOverlay(marker);
              marker.openInfoWindowHtml(address);
            }
          }
        );
      }
    }




