/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var routeLayers = [];

function initSecuredMTBRoutes() {
    vectorLayers.push(new OpenLayers.Layer.Vector($.r3msgs.unofficial_routes, {
        layerType: 'unofficial',
        styleMap: styleMapRoute,
        rendererOptions: {
            zIndexing: true
        }
    }));
    // TODO: only if has owner
    vectorLayers.push(new OpenLayers.Layer.Vector($.r3msgs.partner_routes, {
        layerType: 'partner',
        styleMap: styleMapRoute,
        rendererOptions: {
            zIndexing: true
        }
    }));
    vectorLayers.push(new OpenLayers.Layer.Vector($.r3msgs.my_routes, {
        layerType: 'my',
        styleMap: styleMapRoute,
        rendererOptions: {
            zIndexing: true
        }
    }));
    vectorLayers.push(new OpenLayers.Layer.Vector('tmpRoutes', {
        layerType: 'temporary',
        styleMap: styleMapRoute,
        displayInLayerSwitcher: false,
        rendererOptions: {
            zIndexing: true
        }
    }));
    vectorLayers.push(new OpenLayers.Layer.Vector('tmpRoutes2', {
        layerType: 'temporary2',
        styleMap: styleMapRoute,
        displayInLayerSwitcher: false,
        rendererOptions: {
            zIndexing: true
        }
    }));
    map.addLayers(vectorLayers);

    // reorder layers
    var routingLineLayer = getVectorLayerByType('routingline');
    map.setLayerIndex(routingLineLayer, map.getNumLayers() + 1);
    var startPointLayer = getVectorLayerByType('startpoint');
    map.setLayerIndex(startPointLayer, map.getNumLayers() + 1);

    loadRouteFeatures();
}

function destroySecuredMTBRoutes() {
    for(i in vectorLayers) {
        vectorLayers[i].removeAllFeatures();
    }
    var securedLayerTypes = ['unofficial', 'partner', 'my', 'temporary'];
    while(securedLayerTypes.length > 0) {
        var layerType = securedLayerTypes.pop();
        var tmpLayer = getVectorLayerByType(layerType);
        if (tmpLayer !== null) {
            removeVectorLayerByType(layerType)
            map.removeLayer(tmpLayer);
        }
    }
    loadRouteFeatures();
}

function removeVectorLayerByType(type) {
    var tmpLayer = null;
    for(i in vectorLayers) {
        if (vectorLayers[i].layerType == type) {
            tmpLayer = vectorLayers.splice(i, 1);
            
            break;
        }
    }
}

function getVectorLayerByType(type) {
    var tmpLayer = null;
    for(i in vectorLayers) {
        if (vectorLayers[i].layerType == type)
            tmpLayer = vectorLayers[i];
    }
    return tmpLayer;
}

function getRouteLayerById(id) {
    var tmpLayer = null;
    var i = null;
    for(i in vectorLayers) {
        if (vectorLayers[i].layerType == 'poi' || vectorLayers[i].layerType == 'startpoint' || vectorLayers[i].layerType == 'tmpLayer')
            continue;
        feature = vectorLayers[i].getFeatureById(id);
        if (feature !== null)
            tmpLayer = vectorLayers[i];
    }
    return tmpLayer;
}

function loadRouteFeatures() {
    try {
        var params = {
            'time': (new Date()).getTime(),
            'extent': map.getExtent().toBBOX(),
            'zoom': map.getZoom()
        };
    } catch(e) {
        if ($('#zoom_minx').val() != '' &&
            $('#zoom_miny').val() != '' &&
            $('#zoom_maxx').val() != '' &&
            $('#zoom_maxy').val() != '') {
            params = {
                'time': (new Date()).getTime(),
                'extent': $('#zoom_minx').val()+','+$('#zoom_miny').val()+','+$('#zoom_maxx').val()+','+$('#zoom_maxy').val(),
                'zoom': $('#zoom_level').val()
            }
        } else {
            params = {
                'time': (new Date()).getTime()
            }
        }
    }
        
    
    selectedFeature = null;
    jQuery.getJSON('loadVectorData.php', params, function(data) {
        for(i in data.layers) {
            var res = data.layers[i];
            var tmpLayer = null;
            for(j in vectorLayers) {
                if (vectorLayers[j].layerType == res.layerType)
                    tmpLayer = vectorLayers[j];
            }
            if (tmpLayer !== null) {
                tmpLayer.removeAllFeatures();
                var format = new OpenLayers.Format.WKT();
                var features = [];
                for(index in res.feature) {
                    var feature = format.read(res.feature[index].the_geom);
                    OpenLayers.Util.extend(feature, {
                        id: res.feature[index].id,
                        attributes: res.feature[index].attributes
                    });
                    features.push(feature);
                }
                tmpLayer.addFeatures(features);
            }
        }

        // remove layer if no features
        if ($('#layerPartner').val() == 'false') {
            var tmpLayer = getVectorLayerByType('partner');
            if (tmpLayer !== null) {
                removeVectorLayerByType('partner');
                map.removeLayer(tmpLayer);
            }
        }

        initVectorSelection( map.getLayersByClass("OpenLayers.Layer.Vector"));
        
        // reselect feature
        if (selctedFeatureLayerType !== null) {
            var tmpLayer = getVectorLayerByType(selctedFeatureLayerType);
            if (tmpLayer !== null) {
                var tmpFeature = tmpLayer.getFeatureById(selectedFeatureID);
                if (tmpFeature !== null) {
                    //clickVector.select(tmpFeature);
                    clickVector.clickFeature(tmpFeature);
                    selectedFeature = tmpFeature; // bug fix: reassign selectedFeature, otherwise it's probably that the style is gonna lost (Important: do it after clickFeature, otherwise the popup does not appear)
                }
            }
        }
        

        clearModule2Load();

        stopLoading();
    });
}
