var gmaps = new Object();

var trafficInfo = null;
var mouseOverTimeoutId = null;

function fMarkers() {
    this.markers = [];
    this.getLength = function() { return this.markers.length; };
    this.pushValue = function(value) { this.markers.push(value); }
    this.getValue = function(index) { return this.markers[index]; }
    this.getLastValue = function() {
        if (this.markers.length > 0) return this.markers[this.markers.length - 1];
        return null;
    }
    this.getValueById = function(id) {
        for (var i = 0; i < this.markers.length; i++) {
            if (this.markers[i].value == id) return this.markers[i];
        }
        return null;
    }
    this.removeValueById = function(id) {
        for (var i = 0; i < this.markers.length; i++) {
            if (this.markers[i].value == id) this.markers.splice(i, 1);
        }
        return null;
    }
}

function CreateMarker(map, point, icon, html) {
    var marker;

    if (icon != null) {
        marker = new GMarker(point, icon);
    }
    else {
        marker = new GMarker(point);
    }
    if (html != '') {

        GEvent.addListener(marker, "click", function() { map.openInfoWindowHtml(point, html); });
        GEvent.addListener(marker, "mouseover", function() {
            mouseOverTimeoutId = setTimeout(function() {
                map.openInfoWindowHtml(point, html); 
                }, 1000);
        });
        GEvent.addListener(marker, "mouseout", function() {
            if (mouseOverTimeoutId) {
                clearTimeout(mouseOverTimeoutId);
            }
            mouseOverTimeoutId = null;
        });
    }
    return marker;
}

function fGetGoogleObject(result, userContext) {

    var gmap = gmaps[userContext.id];
    var map = gmap.map;

    if (result == null ||
        result == 'undefined' ||
        result.MapTypeName == null ||
        result.MapTypeName == 'undefined') {
            return null;
    }

    if (result.MapTypeName == 'NORMAL_MAP')
        map.setMapType(G_NORMAL_MAP);
    else if (result.MapTypeName == 'SATELLITE_MAP')
        map.setMapType(G_SATELLITE_MAP);
    else if (result.MapTypeName == 'HYBRID_MAP')
        map.setMapType(G_HYBRID_MAP);

    if (result.ZoomLevel >= 0)
        map.setCenter(new GLatLng(result.CenterPoint.Latitude, result.CenterPoint.Longitude), result.ZoomLevel);
    else
        map.setCenter(new GLatLng(0, 0), 3);

    if (result.ShowMapTypesControl) {
        map.addControl(new GMapTypeControl());
    }

    if (result.ShowZoomControl) {
        map.addControl(new GLargeMapControl());
    }

    if (markers != null) {
        for (var i = 0; i < markers.getLength(); i++) {
            var cmark = markers.getValue(i);
            if (cmark != null) {
                gmap.map.removeOverlay(cmark);
            }
        }
    }

    var markers = (gmap.markers = new fMarkers());

    var bounds = null;
    var actualPoints = 0;

    for (var i = 0; i < result.Points.length; i++) {
        var myIcon_google = null;
        var myPoint = new GLatLng(result.Points[i].Latitude, result.Points[i].Longitude);

        if (result.ZoomLevel < 0) {
            if (bounds == null)
                bounds = new GLatLngBounds;

            bounds.extend(myPoint);
            ++actualPoints;
        }

        if (result.Points[i].IconImage != '') {

            myIcon_google = new GIcon(G_DEFAULT_ICON);
            markerOptions = { icon: myIcon_google };

            myIcon_google.iconSize = new GSize(result.Points[i].IconImageWidth, result.Points[i].IconImageHeight);
            myIcon_google.image = result.Points[i].IconImage;
        }

        var marker = CreateMarker(map, myPoint, myIcon_google, result.Points[i].Html);
        marker.value = result.Points[i].Id;
        markers.pushValue(marker);
        map.addOverlay(markers.getLastValue());
    }

    if (bounds != null) {
        var zoomLevel; 

        if (result.ZoomLevel == -1) {  // search map
            zoomLevel = 6
            map.setCenter(bounds.getCenter(), zoomLevel);
        }
        else if (result.ZoomLevel == -2) { // tab map
            zoomLevel = map.getBoundsZoomLevel(bounds);
            map.setCenter(bounds.getCenter(), zoomLevel);
        }
        else
            zoomLevel = 10;   // detail map (one object)        
    }

    if (result.ShowTraffic) {
        trafficInfo = new GTrafficOverlay();
        map.addOverlay(trafficInfo);
    }

    
}


