﻿var Map = {
    CanvasID: "mapCanvas",
    ShowAddress: function(address) {
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode({ 'address': address }, function(results, status) {

            if (status == google.maps.GeocoderStatus.OK) {
                var map = new google.maps.Map(document.getElementById(Map.CanvasID), {
                    zoom: 13,
                    center: results[0].geometry.location,
                    mapTypeId: google.maps.MapTypeId.ROADMAP

                });

                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location
                });
            } else {
                //alert("Geocode was not successful for the following reason: " + status);
            }
        });
    },
    ShowMarker: function(lat, lng) {
        var latLng = new google.maps.LatLng(lat, lng);

        var map = new google.maps.Map(document.getElementById(Map.CanvasID), {
            zoom: 13,
            center: latLng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        var marker = new google.maps.Marker({
            position: latLng,
            title: 'Property',
            map: map,
            draggable: false
        });
    },
    InitInteractiveMap: function(cityID) {
        var properties = Async.GetPropertiesAssociatedWithCity(
            cityID,
            function(data) {
                var latLng = new google.maps.LatLng(1, 1);
                var map = new google.maps.Map(document.getElementById(Map.CanvasID), {
                    zoom: 6,

                    center: latLng,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                });


                var bounds = new google.maps.LatLngBounds();              
                

//                var maxLat = 0;
//                var minLat = 0;
//                var maxLng = 0;
//                var minLng = 0;

                $(data).each(function(index, item) {
//                    maxLat = item.Latitude;
//                    minLat = item.Latitude;
//                    maxLng = item.Longitude;
//                    minLng = item.Longitude;

//                    maxLat = Math.max(maxLat, item.Latitude);
//                    minLat = Math.min(maxLat, item.Latitude);
//                    maxLng = Math.max(maxLng, item.Longitude);
//                    minLng = Math.min(maxLng, item.Longitude);
                    
                    var position = new google.maps.LatLng(item.Latitude, item.Longitude);
                    bounds.extend(position);
                    
                    var marker = new google.maps.Marker({
                    position: position,
                        title: item.Name,
                        map: map,
                        draggable: false,
                        icon: '/layout/cnc/img/' + (item.IsCondo ? 'condo' : 'house') + '-icon.png'
                    });

                    var infowindow = new google.maps.InfoWindow({
                        content: Map.FillInfoWindow(item)
                    });

                    google.maps.event.addListener(marker, 'click', function() {
                        infowindow.open(map, marker);
                    });

                });
                //.getCenter()
                map.fitBounds(bounds);
                
                //var center = new google.maps.LatLng((maxLat + minLat) / 2, (maxLng + minLng) / 2);
                //map.setCenter(center);
            },
            function(error) {
                alert(error);
            }
        );
    },
    FillInfoWindow: function(p) {
        return "<table class='property-brick'>" +
                    "<tr>" +
                        "<td class='image'>" +
                            "<img src=" + p.Photo + " alt=" + p.Name + " />" +
                        "</td>" +
                        "<td class='info'>" +
                            "<h3>" + p.Name + " </h3>" +
                            "<div><b>Prices starting from:</b> " + p.MinPrice + " </div>" +
                            "<div><b>Location:</b> " + p.Location + " </div>" +
                            "<div><b>Year Built:</b> " + p.YearBuilt + " </div>" +
                        "</td>" +
                    "</tr>" +
                    "<tr>" +
                        "<td colspan='2' class='description'>" +
                            p.Description +
                        "</td>" +
                    "</tr>" +
                    "<tr>" +
                        "<td colspan='2' class='footer'>" +
                            "<a href='/forsale/florida/" + p.UniqueUrl + ".aspx'>View " + p.Name + " " + (p.IsCondo ? "Condos" : "Homes") + " FOR SALE</a>" +
                        "</td>" +
                    "</tr>" +
                "<table>";
    }
}

