﻿function GLoad() {
    if (GBrowserIsCompatible()) {
        // Create Map & Controls
        var map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl()); //GSmallMapControl
        map.addControl(new GMapTypeControl());

        // Center Map On Position
        map.setCenter(new GLatLng(document.getElementById("hdCenterLat").value, document.getElementById("hdCenterLong").value), 12);
        // Create Icons
        var icon = new GIcon();
        icon.shadow = '../images/gmap_shadow.png';
        icon.iconSize = new GSize(12, 20);
        icon.shadowSize = new GSize(22, 20);
        icon.iconAnchor = new GPoint(6, 20);
        icon.infoWindowAnchor = new GPoint(5, 1);
        //icon.infoShadowAnchor = new GPoint(18, 25);
        var icon_1 = new GIcon(icon); icon_1.image = '../images/gmap_red.png';
        var icon_2 = new GIcon(icon); icon_2.image = '../images/gmap_blue.png';
        var icon_3 = new GIcon(icon); icon_3.image = '../images/gmap_green.png';
        var icon_4 = new GIcon(icon); icon_4.image = '../images/gmap_yellow.png';
        var icon_5 = new GIcon(icon); icon_5.image = '../images/gmap_purple.png';
        var icon_6 = new GIcon(icon); icon_6.image = '../images/gmap_orange.png';
        var icon_7 = new GIcon(icon); icon_7.image = '../images/gmap_brown.png';
        var icon_8 = new GIcon(icon); icon_8.image = '../images/gmap_lblue.png';
        // Capture Map Bounds
        var bounds = new GLatLngBounds();
        var d = new Date();
        GDownloadUrl("../listing/SearchListingHelper.aspx?method=getListingMarkers&city=" + document.getElementById('cbCity').value +
                     "&minRent=" + document.getElementById('min_rent').value + "&maxRent=" + document.getElementById('max_rent').value +
                     "&minSqFeet=" + document.getElementById('minSqFt').value + "&maxSqFeet=" + document.getElementById('maxSqFt').value +
                     "&bedrooms=" + document.getElementById('cbBedrooms').value + "&bathrooms=" + document.getElementById('cbBathrooms').value +
                     "&propertytype=" + document.getElementById('cbproperty').value, function(p) {
                         //alert(p);
                         if (p != null || p != undefined) {
                             var json = eval('(' + p + ')');
                             // Plot Points
                             for (var i = 0; i < json.markers.length; i++) {
                                 var marker = new GMarker(json.markers[i].point, eval('icon_' + json.markers[i].type));
                                 marker.property_id = json.markers[i].id;
                                 map.addOverlay(marker);
                                 bounds.extend(json.markers[i].point);
                             }

                             if (json.markers.length > 0) {
                                 // Center & Zoom Map To Bounds
                                 map.setZoom(map.getBoundsZoomLevel(bounds));
                                 var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) / 2;
                                 var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) / 2;
                                 map.setCenter(new GLatLng(clat, clng));
                             }
                         }

                     });
        GEvent.addListener(map, "click", function(overlay, point) {
            if (overlay && overlay.property_id) { // marker clicked
                var d = new Date();
                GDownloadUrl("../listing/SearchListingHelper.aspx?method=getListingDetailByMap&listingId=" + overlay.property_id, function(a) {
                    overlay.openInfoWindowHtml(a); // open InfoWindow
                });
            } else if (point) {	// background clicked
                map.getInfoWindow().hide();
            }
        });
    } else {
        alert('Sorry, Google Maps are not compatible with this browser.');
    }
}