﻿/// <reference path="OpenLayers/OpenLayers.js" />
/// <reference path="GbgMap.API.js" />
/// <reference path="../GbgMap.Base.js" />

GbgMap.API = {

    map: null,

    activeLayers: [],

    selectControl: null,

    selectFeatureCallback: null,

    initializeMap: function (config, mapInitializedCallback) {

        this.configuration = config;

        //Set basic map settings
        map = new OpenLayers.Map(config.mapDiv, config.mapSettings);

        //Set background layers
        for (var i = 0; i < config.bgLayers.length; i++) {
            map.addLayer(new OpenLayers.Layer.TileCache(config.bgLayers[i].name, config.bgLayers[i].url, config.bgLayers[i].layerName));
        }

        //Set controls
        if (config.mouseMapControls) {
            map.addControl(new OpenLayers.Control.MouseDefaults());
        }

        //map.addControl(new OpenLayers.Control.MousePosition());

        //Set start map position
        if (config.startXY && config.startXY != "") {
            map.setCenter(new OpenLayers.LonLat(config.startXY.split(',')[0], config.startXY.split(',')[1]), config.startZoomLevel);
        }
        else {
            //Default map position in case nothing is specified in config
            map.setCenter(new OpenLayers.LonLat(156000, 6400000), 0);
        }

        //Set right background layer
        var startBgLayer = map.getLayersByName(config.startBgLayer)[0];
        if (startBgLayer) {
            map.setBaseLayer(startBgLayer);
        }

        if (mapInitializedCallback) {
            mapInitializedCallback();
            map.events.triggerEvent("zoomend");
        }
    },

    getMapInstance: function () {
        return this.map;
    },

    getCurrentZoomLevel: function () {
        return map.getZoom();
    },

    showPopup: function (html, feature, closeBoxCallback) {

        var popup = new GbgMap.API.Popup("gbgmap.popup",
                                     new OpenLayers.LonLat(feature.x, feature.y),
                                     null,
                                     html,
                                     null,
                                     false,
                                     null);

        popup.panMapIfOutOfView = true;
        popup.autoSize = true;
        popup.backgroundColor = "";

        if (map.popups.length > 0)
            map.removePopup(map.popups[0]);

        map.updateSize();
        popup.hide();
        map.addPopup(popup);
        popup.updatePosition();
        popup.show();

        if (closeBoxCallback) {
            closeBoxCallback();
        }
    },

    hidePopup: function () {
        if (map.popups.length > 0)
            map.removePopup(map.popups[0]);

        GbgMap.API.selectControl.unselectAll();
    },

    addActiveLayer: function (activeLayer) {

        if (activeLayer instanceof GbgMap.API.ActiveLayer) {

            //Check first if layer already exists to avoid duplicates
            if (GbgMap.API.getActiveLayer(activeLayer.name) != null) {
                return;
            }

            //Style for features layer. Replace with SLD in future releases
            var style = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']);
            style.externalGraphic = activeLayer.iconUrl;
            style.graphicWidth = 25;
            style.graphicHeight = 40;
            style.fillOpacity = 1;
            style.cursor = "pointer";

            var options = {};
            options.style = style;

            //Add vectorLayer for features
            var layer = new OpenLayers.Layer.Vector(activeLayer.name, options);

            map.addLayer(layer);

            if (this.selectControl == null) {
                this.selectControl = new OpenLayers.Control.SelectFeature(layer,
                    {
                        onSelect: function (feature) {
                            GbgMap.API.selectFeatureCallback(feature.attributes.featureId);
                        }
                    });

                map.addControl(this.selectControl);
                this.selectControl.activate();
            }
            else {
                var attachedLayers = this.selectControl.layers;
                attachedLayers.push(layer);
                this.selectControl.setLayer(attachedLayers);
            }

            GbgMap.API.activeLayers.push(activeLayer);
        }
    },

    getActiveLayer: function (layerName) {

        var foundLayer = null;

        for (var i = 0; i < GbgMap.API.activeLayers.length; i++) {
            if (GbgMap.API.activeLayers[i].name == layerName) {
                foundLayer = GbgMap.API.activeLayers[i];
            }
        }

        return foundLayer;
    }
}



