﻿Vtab.Map.init = function() {

    var config = Vtab.Map.Configuration;
    //    var provider = new WebGISTileServer.Provider.LMVSESweref99();

    var options = config.map.options;
    options.maxExtent = new OpenLayers.Bounds.fromString(config.map.bounds);
    options.tileSize = new OpenLayers.Size(250, 250);
    options.fallThrough = true;
    options.controls = [];
    map = new OpenLayers.Map('mapContainer', options);
    var token = GetToken();

    for (var i = 0; i < config.layers.length; i++) {

        if (config.layers[i].type == "WMS") {
            var layerToAdd = new OpenLayers.Layer.WMS(config.layers[i].title, config.layers[i].url, config.layers[i].params, config.layers[i].options);
            map.addLayer(layerToAdd);
            //Sets visibility on layer
            layerToAdd.setVisibility(config.layers[i].options.visible);
        }
//        if (config.layers[i].type == "Google") {
//            map.addLayer(
//                new OpenLayers.Layer.Google(
//                    config.layers[i].Settings.GoogleType,
//                    { numZoomLevels: config.layers[i].Settings.numZoomLevels, 'sphericalMercator': config.layers[i].Settings.sphericalMercator },
//                    {
//                        isBaseLayer: true,
//                        buffer: 1
//                    }
//                )
//            );
//        }
        if (config.layers[i].type == 'KML') {
            var kmlLayer = new OpenLayers.Layer.Vector("kmlLayer", {
                //                projection: map.displayProjection,
                rendererOptions: { yOrdering: true },
                type: "KML",
                strategies: [new OpenLayers.Strategy.Fixed()],
                protocol: new OpenLayers.Protocol.HTTP({
                    url: "services/getKMLdata.aspx?left=" +0 + "&right=" + 0 + "&top=" + 0 + "&bottom=" + 0,
                    //                url: "hpl.kml",
                    format: new OpenLayers.Format.KML({
                        extractStyles: true,
                        extractAttributes: true
                    })
                })
            });
            map.addLayer(kmlLayer);
        }
        if (config.layers[i].type == 'Vector') {
            var layerToAdd = new OpenLayers.Layer.Vector(config.layers[i].title, config.layers[i].options);
            layerToAdd.displayInLayerSwitcher = false;
            map.addLayer(layerToAdd);
        }
        if (config.layers[i].type == 'Xepto') {
            var layerToAdd = new OpenLayers.Layer.TMS(config.layers[i].title, config.layers[i].url, { layername: token + '/5', type: 'png' });
            map.addLayer(layerToAdd);
        }

        if (config.layers[i].type == 'Markers') {
            var layerToAdd = new OpenLayers.Layer.Markers(config.layers[i].title, config.layers[i].options);
            layerToAdd.displayInLayerSwitcher = false;
            map.addLayer(layerToAdd);
        }
    }
}
Vtab.Map.setMapOptions = function() {
//    OpenLayers.IMAGE_RELOAD_ATTEMPTS = 2;
//    OpenLayers.Util.onImageLoadErrorColor = "transparent";

    map.setCenter(new OpenLayers.LonLat(415555, 6626027), 5);
//    map.setOptions({ restrictedExtent: map.maxExtent });
}
Vtab.Map.setMapControls = function() {
    var vectorLayer = map.getLayersByName("vectorLayer")[0];

    map.addControl(new OpenLayers.Control.Navigation());
    map.addControl(new Epos.MeasureToolbar(vectorLayer, { div: document.getElementById('measureDiv'), paneldiv: document.getElementById('measureOutput') }));
    map.addControl(new OpenLayers.Control.ZoomBox());
//    map.addControl(new OpenLayers.Control.MousePosition());
    map.addControl(new OpenLayers.Control.PanZoomBar({ slideFactor: 100 }));
}



