var i = 0;
var geocoder = new google.maps.Geocoder();
var infowindow = new google.maps.InfoWindow({
    content: ''
});             
function GetNext()
{
    if (i < g_aryZipCounts.length)
    {
        if (g_aryZipCounts[i].lat == undefined)
        {
            geocoder.geocode( {
                'address': g_aryZipCounts[i].Zipcode.toString()
                }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) 
                {
                    g_aryZipCounts[i].lat = results[0].geometry.location.lat();
                    g_aryZipCounts[i].lng = results[0].geometry.location.lng();
                    $("#Status").append(i + " - Success - " + g_aryZipCounts[i].lat + ", " + g_aryZipCounts[i].lng + "<br>");
                } 
                else
                {
                    $("#Status").append(i + " - Error - " + status + "<br>");
                    if (status == 'OVER_QUERY_LIMIT')
                        i = g_aryZipCounts.length;
                }
                i++;
                GetNext();
            });                     
        }
        else
        {
            $("#Status").append(i + " - Already Loaded<br>");
            i++;
            GetNext();
        }
    }
    else
    {
        $("#Status").append("Done" + "<br>");
        $("#Out").html((JSON.stringify(g_aryZipCounts)).replace(/"(\w+)"\s*:/g, '$1:'));                
    }
                
                
}
function DoIt()
{
    $("#Status").html("Start<br>");
    i = 0;
    GetNext();
                
}
            
var map;
function initialize() {
    var latlng = new google.maps.LatLng(g_aryZipCounts[2].lat, g_aryZipCounts[2].lng);
    var myOptions = {
        zoom: 10,
        center: latlng,
		scrollwheel: false,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("Map"), myOptions);
    setMarkers(map);
}
function setMarkers(map) {
    // Add markers to the map
    // Marker sizes are expressed as a Size of X,Y
    // where the origin of the image (0,0) is located
    // in the top left of the image.
    // Origins, anchor positions and coordinates of the marker
    // increase in the X direction to the right and in
    // the Y direction down.
    var shape = {
        coord: [1, 1, 1, 20, 18, 20, 18 , 1],
        type: 'poly'
    };
    for (var i = 0; i < g_aryZipCounts.length; i++) {
        if ((g_aryZipCounts[i].lat != undefined) && (g_aryZipCounts[i].lng != undefined))
        {
		

            var image = new google.maps.MarkerImage('/images/m5.png',
                // This marker is 20 pixels wide by 32 pixels tall.
                new google.maps.Size((g_aryZipCounts[i].Count/20), (g_aryZipCounts[i].Count/20)),
                // The origin for this image is 0,0.
                new google.maps.Point(0,0),
                // The anchor for this image is the base of the flagpole at 0,32.
                new google.maps.Point(0, 9),
                new google.maps.Size((g_aryZipCounts[i].Count/20), (g_aryZipCounts[i].Count/20))
                );

				
				
            var shadow = new google.maps.MarkerImage('/images/m5.png',
                // The shadow image is larger in the horizontal dimension
                // while the position and offset are the same as for the main image.
                new google.maps.Size(4, 4),
                new google.maps.Point(0,0),
                new google.maps.Point(0, 18));
            // Shapes define the clickable region of the icon.
            // The type defines an HTML &lt;area&gt; element 'poly' which
            // traces out a polygon as a series of X,Y points. The final
            // coordinate closes the poly by connecting to the first
            // coordinate.
            var myLatLng = new google.maps.LatLng(g_aryZipCounts[i].lat, g_aryZipCounts[i].lng);
            var marker = new google.maps.Marker({
                position: myLatLng,
                map: map,
                shadow: shadow,
                icon: image,
                shape: shape,
                title: 'Zipcode = ' + g_aryZipCounts[i].Zipcode,
                myIndex: i
            });
            google.maps.event.addListener(marker, 'mouseover', function() {
                infowindow.setContent(g_aryZipCounts[this.myIndex].Zipcode + ' Sales = ' + g_aryZipCounts[this.myIndex].Count);
                infowindow.open(map,this);
            }); 
            google.maps.event.addListener(marker, 'mouseout', function() {
                infowindow.close(map,this);
            }); 
            google.maps.event.addListener(marker, 'click', function() {
				location.href = 'search.asp?zip=' + g_aryZipCounts[this.myIndex].Zipcode;
            }); 
        }
    }
}
