﻿google.maps.Map.prototype.markers = [];

google.maps.Map.prototype.addMarker = function(marker) {
    this.markers[this.markers.length] = marker;
    marker.setMap(this);
};
google.maps.Map.prototype.clearMarkers = function() {
    for (var i = 0; i < this.markers.length; i++) {
        this.markers[i].setMap(null);
    }
    this.markers = [];
};

var LavvaGeoMap = {
    overlay: null,
    timer: null,
    map: null,
    geocoder: null,
    currentUserMarker: null,
    clientIp: null,
    initialize: function() {
        GeoLocationService.GetCurrentClientIp(function() {
            LavvaGeoMap.clientIp = GeoLocationService.clientIp;
        });
        
        var myOptions = {
            zoom:2,
            
            navigationControl: false,
            disableDefaultUI: true,
            mapTypeControl: false,
            mapTypeId: google.maps.MapTypeId.ROADMAP

        };

        LavvaGeoMap.map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        google.maps.event.addListener(LavvaGeoMap.map, "tilesloaded", function() {
            var mapTag = $("#map_canvas > div");
            var poweredby = mapTag.eq(0).children("div").slice(1, 3);
            poweredby.remove();
        });
    },
    addMarker: function(lat, lng, text, isDefault, markerImageUrl) {
        if (isDefault) {
            
            markerImageUrl = "http://img.lavva.com/Geo/lavva_def_pointer.png";
        }

        var location = new google.maps.LatLng(lat, lng);
        var marker = new google.maps.Marker({
            position: location,
            title: text,
            icon: markerImageUrl
        });
        LavvaGeoMap.map.addMarker(marker);
        return marker;
    },
    putNearestRequestLocation: function() {
        LavvaGeoMap.sendRequest('GETGEOSEARCHERS', function(result) {
            if (result) {
                LavvaGeoMap.markers = [];
                LavvaGeoMap.updateMarkers(result);
            }
        });
    },
    updateMarkers: function(newMarkerItems) {
        LavvaGeoMap.map.clearMarkers();
        for (var i = 0; i < newMarkerItems.length; i++) {
            LavvaGeoMap.addMarker(newMarkerItems[i].Latitude, newMarkerItems[i].Longtitude, newMarkerItems[i].Message, newMarkerItems[i].IsDefault, "http://img.lavva.com/Geo/lavva_neibour_pointer.png");
        }
        LavvaGeoMap.map.addMarker(LavvaGeoMap.currentUserMarker);
    },
    sendRequest: function(requestType, successFunction, failedFunction) {
        var requestString = 'http://www.lavva.com/Services/LavvaService.ashx?meth=' + requestType;
        $.get(requestString, { clientIp: LavvaGeoMap.clientIp }, successFunction, "json");
    },
    putCurrentRequestLocation: function() {
        if (LavvaGeoMap.clientIp == null) {
            setTimeout("LavvaGeoMap.putCurrentRequestLocation()", 1000);
            return;
        }
        LavvaGeoMap.sendRequest('GETCURRENTGEOLOCATION', function(result) {
            LavvaGeoMap.currentUserMarker = LavvaGeoMap.addMarker(result.Latitude, result.Longtitude, result.Message, result.IsDefault, "http://img.lavva.com/Geo/lavva_founded_pointer.png");
            LavvaGeoMap.map.setCenter(LavvaGeoMap.currentUserMarker.position);
            LavvaGeoMap.timer = setInterval('LavvaGeoMap.putNearestRequestLocation()', 10000);
        }, null);
    }
}
