﻿// Classe gerenciado dos marcadores em JavaScript

function TIMapManager(mapContainer)
{
    this.map = InitializeMap(mapContainer);
    this.items = new Hashtable();

    // Remove todos os itens pesquisados
    this.Clear = function()
    {
        this.items.Clear();

        if (this.IsAvailableMap())
        {
            this.map.clearMarkers();
        }
    }

    // Adciona um resultado de Análise
    this.Add = function(resultado)
    {
        this.items.Add(resultado.traID, resultado);

        if (this.IsAvailableMap())
        {
            var imgUrl = "./images/push/push_S_{0}.png".replace("{0}", resultado.IQAScale.toString());
            var marker = createMarker(this.map, new TISolution.MapPoint(resultado.Longitude, resultado.Latitude),
                imgUrl, resultado.Info2);

            resultado.Marker = marker;
        }
    }

    // Localiza o Marcador no Mapa
    this.FindByID = function(traID)
    {
        var resultado = this.GetByID(traID);
        if ((resultado != null) && this.IsAvailableMap())
        {
            var marker = resultado.Marker;
            this.map.animatePan(resultado.Longitude, resultado.Latitude);
            marker.openInfoWindow(resultado.Info);
        }
    }

    // Retorna o Resultado pela chave # traID #
    this.GetByID = function(traID)
    {
        if (this.items.ContainsKey(traID))
            return this.items.Get(traID);
        else
            return null;
    }

    //  Determina se está disponível uma instância do GMap2
    this.IsAvailableMap = function()
    {
        return typeof this.map != "undefined";
    }

}

// Creates a marker
function createMarker(map, point, imageURL, openInfoHTML)
{
    var icon = new OpenLayers.Marker.defaultIcon();
    icon.size = new OpenLayers.Size(24, 24);
    icon.url = imageURL;

    return map.createMarker(point, icon, openInfoHTML);
}

var screenOverlay;

function InitializeMap(mapContainer)
{
    var map = new TISolution.Map(mapContainer, { mapversion: "geo" });

    var layerRaster = new OpenLayers.Layer.WMS("Hibrido", "http://sistemas.tisolution.com:8085/mapsolution/wms",
        {
            layers: "geo_hibrido",
            format: "image/png",
            transparent: 'true',
            tiled: 'true'
        });
    layerRaster.setVisibility(false);
    map.addLayer(layerRaster);

    var layerBH = new OpenLayers.Layer.WMS("Bacia Hidrografica", "http://sistemas.tisolution.com:8085/mapsolution/wms",
        {
            layers: "bacia_hidrografica",
            format: "image/png",
            transparent: 'true',
            tiled: 'true'
        });
    layerBH.setVisibility(false);

    map.addLayer(layerBH);

    map.setLayerIndex(layerRaster, 0);
    map.setLayerIndex(layerBH, 1);

    map.addControl(new OpenLayers.Control.MousePosition({ div: document.getElementById('mapMousePosition'), numdigits: 5 }));
    map.addControl(new OpenLayers.Control.Scale('mapScale'));
    map.addControl(new OpenLayers.Control.LayerSwitcher());
    var control = new TISolution.Control.OverviewMap();
    map.addControl(control);
    control.maximizeControl();
    map.setCenter(new TISolution.MapPoint(-51.1619, -23.3119), 12);

    var measureControl = new TISolution.Control.MeasureControl();
    map.addControl(measureControl);
    measureControl.setOutputElement(document.getElementById('mapOutput'));

    return map;
}
