$(document).ready(function() {
    mapsdash.ShowGoogleDashboard('sale');
});

var mapsdash = 
{
    _searchResultPropertiesForSalePageKey : 0,
    _searchResultPropertiesForRentPageKey : 0,
    _searchResultProjectsForSalePageKey : 0,
    _searchResultProjectsForRentPageKey : 0,
    _nvmNumbers : '',
    _searchType : '', 
    
    ShowGoogleDashboard : function (searchType)
    {
        if((mapsdash._searchType == searchType) || ((searchType != 'sale') && (searchType != 'rent') && (searchType != 'project') && (searchType != 'project_rent')))
        {    
            return;
        }
            
        mapsdash._searchType = searchType;
        
        $.get('/Modules/FES_GOOGLEMAPS_DASHBOARD/callback.ashx?nvmNumbers=' + mapsdash._nvmNumbers + '&searchType=' + searchType,
        function(response)
        {
            var markerArray = [];			
            markers = response.documentElement.getElementsByTagName('marker');

            if (!markers)  return;

            var destinationPageKey;
            if(mapsdash._searchType == 'sale')
            { 
                destinationPageKey = mapsdash._searchResultPropertiesForSalePageKey;
            }
            else if(mapsdash._searchType == 'rent')
            {
                destinationPageKey = mapsdash._searchResultPropertiesForRentPageKey;
            }
            else if(mapsdash._searchType == 'project_rent')
            {
                destinationPageKey = mapsdash._searchResultProjectsForRentPageKey;
            }
            else
            {
                destinationPageKey = mapsdash._searchResultProjectsForSalePageKey;
            }
            
            var markerImage = new google.maps.MarkerImage('/Modules/FES_GOOGLEMAPS_DASHBOARD/Images/huisclusterone46.png',
                        new google.maps.Size(34, 46));  
            var markerImageUnavailable = new google.maps.MarkerImage('/Modules/FES_GOOGLEMAPS_DASHBOARD/Images/huisclusterone46_unavailable.png',
                        new google.maps.Size(34, 46));  

            var lat_min = 180.000000;
            var lat_max = -180.000000;
            var lng_min = 90.000000;
            var lng_max = -90.000000;
            for (var i = 0; ((i < markers.length) && (i<2500)); i++) 
            { 
	            var lng = markers[i].getAttribute('lng');
	            var lat = markers[i].getAttribute('lat');
        	
	            if(lat < lat_min){lat_min = lat;}
	            if(lng < lng_min){lng_min = lng;}
	            if(lat > lat_max){lat_max = lat;}
	            if(lng > lng_max){lng_max = lng;}
        		
        		var image = markerImage;
        		if(markers[i].getAttribute('available') == "0")
        		{
        		    image = markerImageUnavailable;
        		}        		
        		
	            var latLng = new google.maps.LatLng(lat, lng)
	            var marker = new google.maps.Marker({position: latLng, draggable: true, icon: image});
        		
	            google.maps.event.addListener(marker, 'click', function(marker) {
                    var bounds = 'neLat=' + marker.latLng.lat() + '&neLng=' +  marker.latLng.lng() + '&swLat=' +  marker.latLng.lat() + '&swLng=' + marker.latLng.lng();
                    document.location.href = '/default.aspx?pageKey='+ destinationPageKey + '&type=' + mapsdash._searchType + '&SearchInputPage=' + mapsdash._currentPageKey + '&' + bounds + '&Display=map';
                });
              
	            markerArray.push(marker);
            }
        	
            var options = 
            {
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                disableDefaultUI: false,
                mapTypeControl: false,
                navigationControl: false,
                keyboardShortcuts: false,
                disableDoubleClickZoom: true,
                draggable: false,
                scrollwheel: false,
                streetViewControl: false
            };

            var stles = [{
            url: '/Modules/FES_GOOGLEMAPS_DASHBOARD/Images/huiscluster46.png',
            width: 34, height: 46,
            opt_anchor: [0, 0],
            opt_textColor: '#FF0000',    
            opt_textSize: 11
            }];
                        
            var map = new google.maps.Map(document.getElementById('sys_fes_googlemaps_dashboard_map'), options);
            var mcOptions = {gridSize: 32, maxZoom: 15, zoomOnClick: false, styles:stles};
            var markerclusterer = new MarkerClusterer(map, markerArray, mcOptions);

            google.maps.event.addListener(markerclusterer, 'clusterclick', function(cluster) {
                var ne = cluster.getBounds().getNorthEast();
                var sw = cluster.getBounds().getSouthWest();
                var bounds = 'neLat=' + ne.lat() + '&neLng=' + ne.lng() + '&swLat=' + sw.lat() + '&swLng=' + sw.lng();
                document.location.href = '/default.aspx?pageKey='+ destinationPageKey + '&type=' + mapsdash._searchType + '&SearchInputPage=' + mapsdash._currentPageKey + '&' + bounds + '&Display=map';
            });
        	
            map.setCenter(new google.maps.LatLng(((lat_max + lat_min) / 2.0), ((lng_max + lng_min) / 2.0)));
            map.fitBounds(new google.maps.LatLngBounds(new google.maps.LatLng(lat_min, lng_min), new google.maps.LatLng(lat_max, lng_max)));
        });
    }
}
