﻿function MapManager(mapOfManager) {
    var map = mapOfManager;

    this.mapControlsManager = null;
    this.mapLayersManager = null;

    map.div.oncontextmenu = onMouseRightClicked_Map_ASPX;


    this.getMap = function() {
        return map;
    }

    this.registerEvent = function(eventName, functionDelegate) {
        map.events.register(eventName, map, functionDelegate);
    }

    this.clearMap = function() {
        mapLayersManager.clearLayer(initializator.layerKeys['wfsLayer']);
        mapLayersManager.getLayer(initializator.layerKeys['markerLayer']).clearMarkers();
    }

    this.zoomToStartUpView = function() {
        map.zoomToExtent(initializator.startUpView);
    }
    this.refresh = function() {
        var selectTool = mapControlsManager.getControl('selectByPoint');
        selectTool.unselectAll();
    }

    // Haritanın zoom seviyesini bir azaltır
    this.zoomOut = function() {
        var zoom = map.getZoom();
        map.zoomTo(zoom - 1);
    }
    
    // Haritanın zoom seviyesini bir artırır
    this.zoomIn = function() {
        var zoom = map.getZoom();
        map.zoomTo(zoom + 1);
    }

    this.setCenter = function(lonlat) {
        map.setCenter(lonlat, 8, false, false);
    }

    this.zoomToLayerByKey = function(layerKey) {
        var layer = mapLayersManager.getLayer(layerKey);
        if (layer)
            this.zoomToLayer(layer);
    }

    this.zoomToLayer = function(layerToZoom) {
        var dataExtent = layerToZoom.getDataExtent();
        if (dataExtent != null)
            map.zoomToExtent(dataExtent, false);
    }

    this.zoomToFeature = function(feature) {
        map.zoomToExtent(feature.geometry.getBounds());
    }

    this.zoomToLayerByKey = function(keyOfLayer) {
        var layerToZoom = this.mapLayersManager.getLayer(keyOfLayer);
        if (layerToZoom)
            this.zoomToLayer(layerToZoom);
    }

    this.addMarker = function(lonlat) {
        var size = new OpenLayers.Size(24, 24);
        var offset = new OpenLayers.Pixel(-12, -12);

        var icon = new OpenLayers.Icon("../../Images/BrightEarth/Icons/arrow_down.gif", size, offset);
        mapLayersManager.getLayer(initializator.layerKeys['markerLayer']).clearMarkers();
        marker = new OpenLayers.Marker(lonlat, icon);
        mapLayersManager.getLayer(initializator.layerKeys['markerLayer']).addMarker(marker);
    }
    
    this.addPopup = function(feature, html) {
        var popup = new OpenLayers.Popup.FramedCloud("chicken", feature.geometry.getBounds().getCenterLonLat(), null, html, null, true);
        feature.popup = popup;
        map.addPopup(popup, true);
    }

}
