﻿    google.load("maps", "2");

    var map;
    var geocoder;
    var evtGetListener;

    // Builds an array of cities
    /*
    var pointList = [
	  {
	      name: "Enken",
	      Status: {
	          code: 200,
	          request: "geocode"
	      },
	      Placemark: [
		  {
		      Point: {
		          coordinates: [-46.6596418, -23.559411, 13]
		      },
		      AddressDetails: {
		          Country: {
		              CountryNameCode: "BR",
		              AdministrativeArea: {
		                  AdministrativeAreaName: "SP",
		                  Locality: { LocalityName: "São Paulo" }
		              }
		          }
		      }
		  }
		],
          Message: [
	        {
		        Title: "Enken SA",
		        Message: "Agência multimídia",
		        Address: "Av Paulista 2001, São Paulo, SP, Brasil",
		        Logo: "/images/ico_maps_logo.gi",
		        MiniLogo: "/images/ico_maps_logo.gif",
		        MiniLogoSizeX: 22,
		        MiniLogoSizeY: 30,
		        URL: "http://www.enken.com.br"
		    }
		]
	  },
	  {
	      name: "Cachorro do carneiro",
	      Status: {
	          code: 200,
	          request: "geocode"
	      },
	      Placemark: [
		  {
		      Point: {
		        coordinates: [-46.591301, -23.537196, 13]
		      },
		      AddressDetails: {
		          Country: {
		              CountryNameCode: "BR",
		              AdministrativeArea: {
		                  AdministrativeAreaName: "SP",
		                  Locality: { LocalityName: "São Paulo" }
		              }
		          }
		      }
		  }
		],
          Message: [
	        {
		        Title: "Doguinho",
		        Message: "Cachorro do carneiro... Não que o carneiro seja um cachorro... ah.. vc me entendeu...",
		        Address: "Av André Cavalcanti 490",
		        Logo: "/images/ico_maps_logo.gif",
		        MiniLogo: "/images/ico_maps_logo.gif",
		        MiniLogoSizeX: 22, 
		        MiniLogoSizeY: 30,
		        URL: "http://carneirim.blogspot.com/"
		    }
		]
	  }
	]*/
 
    // CapitalCitiesCache is a custom cache that extends the standard GeocodeCache.
    // We call apply(this) to invoke the parent's class constructor.
    // function CapitalCitiesCache() {
    //  GGeocodeCache.apply(this);
    //}
    
    // Assigns an instance of the parent class as a prototype of the
    // child class, to make sure that all methods defined on the parent
    // class can be directly invoked on the child class.
    
    // CapitalCitiesCache.prototype = new GGeocodeCache();
    
    // Override the reset method to populate the empty cache with
    // information from our array of geocode responses for capitals.
    
    // CapitalCitiesCache.prototype.reset = function() {
    //  GGeocodeCache.prototype.reset.call(this);
    //   for (var i in pointList) {
    //       this.put(pointList[i].name, pointList[i]);
    //   }
    //}
    
    // Add markers on map
    // lista: items list
    function putMapMarkers(lista) {
        
        var opts = null;
        var marker = null;
        var latLng = null;
        
        for (var i in lista) {
 
            // Logo
            var miniLogo = new GIcon();
            miniLogo.image = lista[i].Message[0].MiniLogo;
            miniLogo.shadow = lista[i].Message[0].MiniLogo;
            miniLogo.iconSize = new GSize(lista[i].Message[0].MiniLogoSizeX, lista[i].Message[0].MiniLogoSizeY);
            miniLogo.shadowSize = new GSize(lista[i].Message[0].MiniLogoSizeX, lista[i].Message[0].MiniLogoSizeY);
            miniLogo.iconAnchor = new GPoint(0, 0);
            
            // Marker options
            opts = { title: lista[i].name };
            
            // Latitude / longitude
            latLng = new GLatLng(lista[i].Placemark[0].Point.coordinates[1], lista[i].Placemark[0].Point.coordinates[0]);
            
            // Create a marker
            marker = new GMarker(latLng, miniLogo, opts);
            
            // Add to map
            map.addOverlay(marker);
        }
    }
    
    // Go to address on map
    function addAddressToMap(response) {
      map.clearOverlays();
    
      if (response && response.Status.code != 200) {
        alert("Unable to locate " + decodeURIComponent(response.name));
      } else {
        var place = response.Placemark[0];
        var point = new GLatLng(place.Point.coordinates[1],
            place.Point.coordinates[0]);
        map.setCenter(point, 6);
        map.openInfoWindowHtml(point, formatMessage(response.Message[0]));
      }
    }
 
    // Format message box who is displayed when a dialog box is shown
    function formatMessage(customMessage) {
        
        var HTMLmessage = '';
 
        if (customMessage.Logo.length > 0) HTMLmessage += "<div align='center'><img src='" + customMessage.Logo + "' alt='" + customMessage.Title + "'></div>";
        if (customMessage.Title.length > 0) HTMLmessage += "<p class='title'>" + customMessage.Title + "</p>";
        if (customMessage.Message.length > 0) HTMLmessage += "<p class='message'>" + customMessage.Message + "</p>";
        if (customMessage.URL.length > 0) HTMLmessage += "<p class='url'><a href='" + customMessage.URL + "' target='_blank'>Ir para</a></p>";
        if (customMessage.Address.length > 0) HTMLmessage += "<p class='address'>" + customMessage.Address + "</p>";
 
        return HTMLmessage;
    }
    
    function findCity(which) {
      if (which != 0) {
        geocoder.getLocations(pointList[which - 1].name, addAddressToMap);
      }
    }
 
    function getAddress(query) {
        if (query != '') {
            geocoder.getLocations(query, addAddressToMap);
        } else {
            alert('Informe um endereço');
        }
    }
 
    function getAddressClick(overlay, latlng) {
        if (latlng != null) {
            address = latlng;
            geocoder.getLocations(latlng, showAddress);
        }
    }
 
    function showAddress(response) {
        map.clearOverlays();
        if (!response || response.Status.code != 200) {
            alert("Status Code:" + response.Status.code);
        } else {
            place = response.Placemark[0];
            point = new GLatLng(place.Point.coordinates[1],
        place.Point.coordinates[0]);
            marker = new GMarker(point);
            map.addOverlay(marker);
            marker.openInfoWindowHtml(
        '<b>orig latlng:</b>' + response.name + '<br/>' +
        '<b>latlng:</b>' + place.Point.coordinates[0] + "," + place.Point.coordinates[1] + '<br>' +
        '<b>Status Code:</b>' + response.Status.code + '<br>' +
        '<b>Status Request:</b>' + response.Status.request + '<br>' +
        '<b>Address:</b>' + place.address + '<br>' +
        '<b>Accuracy:</b>' + place.AddressDetails.Accuracy + '<br>' +
        '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
        }
    }
    
    // Removes the listener for the click event
    function showInfo(checked) {
        if (checked) {
            evtGetListener = GEvent.addListener(map, "click", getAddressClick);
        } else {
            if (evtGetListener) GEvent.removeListener(evtGetListener);
        }
    }