﻿ // Seperate latitudes and longitudes to individual arrays.
    var latitudes = [];
    var longitudes = [];
    
    if( communityData != '')
    {
        communityData.each(function(position)
        {
            latitudes.push(parseFloat(position[0]));           
            longitudes.push(parseFloat(position[1]));
        });
        
        var longitudeMin = longitudes.min();
        var longitudeMax = longitudes.max();
        var latitudeMin = latitudes.min();  
        var latitudeMax = latitudes.max();
        
        var centerLongitude = longitudeMin + (longitudeMax - longitudeMin) / 2; 
        var centerLatitude = latitudeMin + (latitudeMax - latitudeMin) / 2; 
        var mapCenterPoint = new GLatLng(centerLatitude, centerLongitude);
        
        var mapWidth = 700; //Element.getDimensions("map").width;
        var mapSouthWestPoint = new GLatLng(latitudeMin, longitudeMin);
        var mapNorthEastPoint = new GLatLng(latitudeMax, longitudeMax);
        var mapBounds = new GLatLngBounds(mapSouthWestPoint, mapNorthEastPoint)
    }
    
    function load() {
      if (GBrowserIsCompatible()) 
      {
        var map = new GMap2($("map"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(mapCenterPoint, map.getBoundsZoomLevel(mapBounds));  
            
        // Create a base icon for all of our markers that specifies the
        // shadow, icon dimensions, etc.
        var baseIcon = new GIcon();
        baseIcon.shadow = basePath + "images/FlagShadow.png";
        baseIcon.iconSize = new GSize(27, 40);
        baseIcon.shadowSize = new GSize(41, 40);
        baseIcon.iconAnchor = new GPoint(9, 34);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);
        baseIcon.infoShadowAnchor = new GPoint(18, 25);

        communityData.each(function(position) {
            var commLatitude = position[0];
            var commLongitude = position[1];
            var directoryName = "<a href='" + basePath + position[5] + "/'>" + position[4] + "</a>";
            var pageDescription = position[6];
            var grandparentid = position[7];

            //Create icon based upon community type
            var icon = new GIcon(baseIcon);
            if (grandparentid == '1781') {
                icon.image = basePath + "images/MapleLeaf.png"; //Canada Icon
            }
            else
                icon.image = basePath + "images/FlagSF.png"; //Single Family Icon

            //Create our marker based upon lat/long and icon
            var marker = new GMarker(new GLatLng(commLatitude, commLongitude), icon);
            var html = "<span style='font-family:Verdana;font-size:12px;width:250px;'>" + directoryName + "<br>" + pageDescription;

            html = html + "</span>"
            GEvent.addListener(marker, 'click', function() {
                marker.openInfoWindowHtml(html);
            });

            map.addOverlay(marker);
        });   
      }
    }




