function addPoint(lat, long, bubble)
{
    //console.log("adding point" + lat + long + bubble);
    point = Array(lat,long,bubble)
    this.points.push(point);
}

function renderPoints()
{
    //console.log("render points");
   for(i=0; i<this.points.length; i++)
   {
        this.renderPoint(this.points[i]);
   }
}

function renderPoint(point)
{
    //console.log("render point");
    var latLong = new GLatLng(point[0],point[1])
    var g_point = new GMarker(latLong);
    //console.log("point 2" + point[2]);
    if(point[2])
    {
	    GEvent.addListener(g_point, "click", function(){
	       g_point.openInfoWindowHtml(point[2]);
	    });
	}
    
    this.map.addOverlay(g_point);
    this.bounds.extend(g_point.getPoint());
    
}


function render()
{
    //console.log("render");
    if (GBrowserIsCompatible() && (this.points.length || this.isAddMap))
    {
        
        if(this.enableDoubleClickZoom)
        {
           this.map.enableDoubleClickZoom();
        }
        
        if(this.enableContinuousZoom)
        {
           this.map.enableContinuousZoom();
        }
        if(this.enableScrollWheelZoom)
        {
        	this.map.enableScrollWheelZoom();
        }
        
        if(this.smallMapControl)
        {
           this.map.addControl(new GSmallMapControl());
        }
        
        if(!this.dragging)
        {
           this.map.disableDragging()
        }
        
        if(this.mapTypeControl)
        {
           this.map.addControl(new GMapTypeControl());
        }
        
        this.map.setCenter(this.defaultLocation, this.zoom, this.map.getMapTypes()[this.view]);
        
        if(!this.isAddMap)
        {
	        this.renderPoints();
	        if(this.points.length > 1)
	        {
	            this.map.setZoom(this.map.getBoundsZoomLevel(this.bounds));
	        }
            this.map.setCenter(this.bounds.getCenter());
        }
        else
        {
            var point = this.map.getCenter();
		    marker = new GMarker(point);
		    this.map.addOverlay(marker);
		    marker.hide();
		    
		    
		    GEvent.addListener(this.map, "click",
		        function(overlay, point)
		        {
		                marker.setPoint(point);
		                if (marker.isHidden())
		                {
		                    marker.show();
		                }
		                
		                
		                
		                document.getElementById('id_latitud').value = point.lat();
		                document.getElementById('id_longitud').value = point.lng();
		        }
		    );
        }
        
        
   }
}

//addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(response) {
    //console.log("addAddressToMap");
    //mapa.map.clearOverlays();
    if (!response || response.Status.code != 200)
    {
        alert("Perdon, no se pudo encontrar ese lugar");
    }
    else
    {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
                        
        marker.setPoint(point);
        if (marker.isHidden())
        {
            marker.show();
        }
        mapa.map.panTo(point)
        document.getElementById('id_latitud').value = point.lat();
		document.getElementById('id_longitud').value = point.lng();
        //console.log(place.AddressDetails)

    }
}

function getPlaceInfo(place)
{
    var pais = place.AddressDetails.Country.CountryNameCode;
    var provincia = place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
    var ciudad = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName;
    var localidad = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName
}
// showLocation() is called when you click on the Search button
// in the form.  It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation(map)
{
    var address = document.getElementById('address_field').value + ", Argentina";

    geocoder.getLocations(address, map.addAddressToMap);
}


function Map(name, width, height, zoom, view)
{
    //console.log("map init");
    this.name = name;
    this.width = width;
    this.height = height;
    this.zoom = zoom;
    this.view = view;
    
    this.enableDoubleClickZoom = false;
    this.enableContinuousZoom = false;
    this.smallMapControl = true;
    this.dragging = true;
    this.mapTypeControl = true;
    this.enableScrollWheelZoom = false;
    
    this.points = Array();
    this.defaultLocation = new GLatLng(-34.609475,-58.416367);
    this.bounds = new GLatLngBounds();
    
    this.addPoint = addPoint;
    this.renderPoints = renderPoints;
    this.renderPoint = renderPoint;
    this.render = render;
    this.addAddressToMap = addAddressToMap;
    
    var container = document.getElementById("map_" + name);
    container.style.width = this.width + "px";
    container.style.height = this.height + "px";
    
    this.map = new GMap2(document.getElementById("map_" + name));
    
    this.isAddMap = false;
    
    geocoder = new GClientGeocoder();
    
    
}
geocoder = null;
marker = null;

function disableEnterKey(e)
{
     var key;     
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox
     
     ret = (key != 13)
    
     if(!ret)
    	 showLocation(mapa);
    	 
     return ret;
}
