﻿// Seperate latitudes and longitudes to individual arrays.
var latitudes = [];
var longitudes = [];
var gmarkers = [];
var latstore;
var longstore;

if (communityData != '') {
    communityData.each(function(position) {
        latitudes.push(parseFloat(position[1]));
        longitudes.push(parseFloat(position[2]));
    });
    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 = Element.getDimensions("map").width;
    var mapSouthWestPoint = new GLatLng(latitudeMin, longitudeMin);
    var mapNorthEastPoint = new GLatLng(latitudeMax, longitudeMax);
    var mapBounds = new GLatLngBounds(mapSouthWestPoint, mapNorthEastPoint);
}

function communityHover(i) {
    if (gmarkers[i] != null)
        GEvent.trigger(gmarkers[i], "mouseover");
}

function load() {
    if (GBrowserIsCompatible() && communityData != '' && mapBounds != null) {
        var map = new GMap2($("map"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(mapCenterPoint, map.getBoundsZoomLevel(mapBounds));

	var zoomLevel = map.getZoom();
	map.setZoom(zoomLevel + -1);

	if(State == 'Minnesota')
		map.setZoom(14); 


        // Create a base icon for all of our markers that specifies the
        // shadow, icon dimensions, etc.
        var baseIcon = new GIcon();

        baseIcon.image = "Flag.png";
        baseIcon.shadow = "FlagShadow.png";
        baseIcon.iconSize = new GSize(40, 47);
        baseIcon.shadowSize = new GSize(40, 47);
        baseIcon.iconAnchor = new GPoint(0, 47);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);
        baseIcon.infoShadowAnchor = new GPoint(18, 25);

        var html2 = "";
        var storelat;
        var storelong;
        var communityAdded = false;
        var sharedCommunity = false;
        var ZipCodeStore;

        var i = 0;
        var communityLength = communityData.length;

        communityData.each(function (position) {


            var html = "";
            var commLatitude = position[1];
            var commLongitude = position[2];

            if (sharedCommunity) {
                html = html2;
                sharedCommunity = false;
            }
            storelat = commLatitude;
            storelong = commLongitude;

            var address = "";
            if (position[8] != null && position[8] != "")
                address += position[8] + "<br>";
            if (position[5] != null && position[5] != "")
                address += position[5] + ", ";
            if (position[4] != null && position[4] != "")
                address += position[4] + " ";
            if (position[6] != null && position[6] != "")
                address += position[6];
            var ID = position[0];
            var ZipCode = position[6];
            ZipCodeStore = ZipCode;

            var bed = position[9];
            var phone = position[13];
            var bath = position[10];
            var feet = position[11];
            var price = position[12];

	    var community = "<a href='http://www.peachgrovevillas.com' target='blank'>Peach Grove Villas</a>";
	    
	    if (ID == 102513)
		community = "<a href='http://www.lexingtonvillas.com' target='blank'>Lexington Villas</a>";
            

            html = html + "<span style='font-family:Verdana;font-size:12px;text-align:left;'>";
            html = html + community + "<br>" + address + "<br>";

            //if (bed != null & bath != null && bed != "" && bath != "")
            //    html = html + bed + " Bedrooms, " + bath + " Bathrooms <br>";
            //else if ((bed != "" || bed == null) && bath == "")
            //    html = html + bed + " Bedrooms<br>";
            //else if ((bed == "" || bed == null) && bath != "")
            //    html = html + bath + " Bathrooms <br>";

            //if (feet != "" && feet != "0" && feet != null)	
            //    html = html + feet + " sq. ft.<br>";

            //if (price != "")
            //    html = html + price + "<br>";

            if (phone != "" && phone != null)
                html = html + "Community Phone - " + phone + "<br /><br />";

            html = html + "</span>"
            communityAdded = true;

            if (communityLength > (i + 1)) {
                if (commLatitude == communityData[i + 1][1] && commLongitude == communityData[i + 1][2]) {
                    sharedCommunity = true;
                    html2 = html;
                }
            }

            if (!sharedCommunity) {
                //Create our marker based upon lat/long and icon
                var latlng = new GLatLng(commLatitude, commLongitude);
                var marker = new GMarker(latlng, baseIcon);

                GEvent.addListener(marker, 'mouseover', function () {
                    marker.openInfoWindowHtml(html);
                });
                gmarkers[ZipCodeStore] = marker;
                map.addOverlay(marker);
            }
            i++;
        });

    }
}


function hidediv(id) {
    //safe function to hide an element with a specified id
    if (document.getElementById) { // DOM3 = IE5, NS6
        document.getElementById(id).style.display = 'none';
    }
    else {
        if (document.layers) { // Netscape 4
            document.id.display = 'none';
        }
        else { // IE 4
            document.all.id.style.display = 'none';
        }
    }
}

function showdiv(id) {
    //safe function to show an element with a specified id
    if (document.getElementById) { // DOM3 = IE5, NS6
        document.getElementById(id).style.display = 'block';
    }
    else {
        if (document.layers) { // Netscape 4
            document.id.display = 'block';
        }
        else { // IE 4
            document.all.id.style.display = 'block';
        }
    }
}
