var map = null;
var geocoder = null;
var markers =[];

function onLoad() {
    var mapDiv = document.getElementById("map");
    if (!mapDiv) {
        return;
    }

    if (GBrowserIsCompatible()) {
	    // Center the map on Palo Alto.
        map = new GMap2(mapDiv);
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
       // map.setCenter(new GLatLng(27.805232, -82.680821), 13);
//	map.setCenter(new GLatLng(53.773066,-2.910004),13); 
	geocoder = new GClientGeocoder();
	//var geostring= geocoder.getLatLng("97478",13);
	//alert (geostring);
        map.setCenter(new GLatLng(-94.75, 40.50), 14);
	displayPropertyMarkers();
    }
}

function displayPropertyMarkers() {
    var htmlString;
    var address, postalCode;
    var i;

    for (i=0; i<googleData.properties.length; i++)
    {
    	/*thisrow = googleData.properties[i];
    	for (j=0; j < thisrow.length; j++) 
    	{
    		htmlString += thisrow[j].value + "<br />";
			alert(htmlString);
		}   
    	*/
    	
    	htmlString = googleData.properties[i].linkto + "<br />" +    	    
	        googleData.properties[i].city + ", " +
    	    googleData.properties[i].state + "&nbsp;&nbsp;" +
        	googleData.properties[i].zip + "<br />" +
        	googleData.country;
		

        address = googleData.properties[i].address + " " +
            googleData.properties[i].city + " " +
            googleData.properties[i].state + " " +
            googleData.properties[i].zip + " " + googleData.country;

		postalCode = 
            googleData.properties[i].zip + " " + googleData.country;

        showAddress(address, postalCode, htmlString);
    }
}

// First try to geocode the address.  If that doesn't work,
// just geocode the postal code (and country).

function showAddress(address, postalCode, HTMLAddress) {
    var html = HTMLAddress;

    geocoder.getLatLng(address,
        function(point) {
            if (point) {
                var marker = createMarker(point, html);
                map.addOverlay(marker);
                
                markers[markers.length]=marker;
                this.centerAndZoomOnMarkers(map,markers);

            } else {
            	setTimeout("showPostalCode('" + postalCode + "', '" + HTMLAddress + "');", 250);
            }
        }
    );
}

function showPostalCode(postalCode, HTMLAddress) {
    var html = HTMLAddress;

    geocoder.getLatLng(postalCode,
        function(point) {
            if (point) {
                var marker = createMarker(point, html);
                map.addOverlay(marker);
                
                markers[markers.length]=marker;
                this.centerAndZoomOnMarkers(map,markers);

            }
        }
    );
}

function createMarker(point, address) {
    var marker = new GMarker(point);
    var html = address;

    // Show this marker's index in the info window when it is clicked.
	GEvent.addListener(marker, 'click',
	    function() {
	        marker.openInfoWindowHtml(html);
	    });

    return marker;
}

    this.centerAndZoomOnMarkers = function (map, markers) {
        var bounds = new GLatLngBounds(markers[0].getPoint(), markers[0].getPoint());
        var i;
        for (i=1; i<markers.length; i++) {
            bounds.extend(markers[i].getPoint());
        }
        var lat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) / 2.0;
        var lng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) / 2.0;
        if(bounds.getNorthEast().lng() < bounds.getSouthWest().lng()){
            lng += 180;
        }
        var center = new GLatLng(lat,lng)
        map.setCenter(center, map.getBoundsZoomLevel(bounds));
    } 
