﻿function createLayerNode(layer, id) {
    var nodeType;
    // Determine if base layer or not.
    if (layer.isBaseLayer) {
        nodeType = "gx_baselayercontainer";
    } else {
        nodeType = "gx_layer";
    }

    // Create a tree node for this layer.
    var node = new GeoExt.tree.LayerNode(
    {
        nodeType: nodeType,
        leaf: true,
        id: id,
        icon: layer.options["legend"],
        text: layer.name,        
        layer: layer
    });

    return node;
}

function toggleLayerAccessability(e) {
    // Get the current zoom resolution.
    var currentZoomID = map.zoom;
    //var currentZoomValue = zoomResolutions[currentZoomID];
    var currentZoomValue = map.getResolutionForZoom(currentZoomID);
    // Go through the list of layers.
    for (var i = 0; i < layerCollection.length; i++) {
        var child = layerCollection[i].name;

        // Get the corresponding node.
        var currentLayerNode = layerNodeCollection[i];

        // If a node was found, work on it.
        if (currentLayerNode != null) {
            // If the current zoom level is greater than the one for the current layer,
            // then hide the layer and disable it from the list.
            var layerZoom = layerCollection[i].options["maxResolution"];

            if (currentZoomValue > layerZoom) {
                currentLayerNode.disable();
            } else {
                currentLayerNode.enable();
            }
        }
    }
}

function createLayerNodeCollection(layers) {
    var nodeCollection = [];
    // Create all the tree nodes.
    for (var currentLayerID = 0; currentLayerID < layers.length; currentLayerID++) {
        nodeCollection[currentLayerID] = new Ext.tree.TreeNode();
        nodeCollection[currentLayerID] = createLayerNode(layers[currentLayerID], currentLayerID);
    }

    return nodeCollection;
}
