﻿/**
 * Start searching for layers in layer model
 * Different top level therefore excluded from recursion
 * @param {Object} layerNames
 * @param {Object} operation Operation can be 'checkLayers' or 'collectLocatable'
 */
function findNodeByLayerName(layerNames, operation){
    if (operation == "collectLocatable") {
        locatableLayers = [];
    }
    for (k = 0; k < model.length; k++) {
        findInChildren(layerNames, model[k], operation);
    }
}

/**
 * Recursively find layer to turn on in tree
 * @param {Object} layerNames
 * @param {Object} subModel
 */
function findInChildren(layerNames, subModel, operation){
    if (subModel.children) {
        for (var i = 0; i < subModel.children.length; i++) {
            findInChildren(layerNames, subModel.children[i], operation);
        }
    }
    else {
        //We found a leaf in the tree
        if (operation == "checkLayers") {
            //checkChanged indicates whether anything happened to layer's checked property
            var checkChanged = false;
            //Go through layers we are searching for
            for (var j = 0; j < layerNames.length; j++) {
                if (layerNames[j] == subModel.layerNames[0]) {
                    subModel.checked = true;
                    checkChanged = true;
                }
                
                
            }
            if (!checkChanged) {
                subModel.checked = false;
                checkChanged = false;
            }
            return;
        }
        else 
            if (operation = "collectLocatable") {
                //Go through layers we are searching for and collect locatable and visible layers
                for (var j = 0; j < layerNames.length; j++) {
                    if ("ujbudaVectorWMS:" + layerNames[j] == subModel.layerNames[0]) {
                        if (subModel.locatable && subModel.checked) {
                            locatableLayers.push(layerNames[j]);
                        }
                    }
                }
            }
        
    }
}
/**
 * Collecting locatable layers
 * @param {Object} currVisibleLayers
 */
function collectLocatable(currVisibleLayers){
    findNodeByLayerName(currVisibleLayers, "collectLocatable");
    return locatableLayers;
}

/**
 * Handler for layer switcher
 * @param {Object} btn
 */
function switchPreDefLegend(btn){
   	findNodeByLayerName(btn.children, "checkLayers");
	
	layerTree = new mapfish.widgets.LayerTree({
        map: map,		
		model: model,
		el: 'retegek'
    });	
	
	var harmonikaVar = Ext.getCmp("harmonika");
	//TODO: a getComponent-et ID alapon megcsinalni
	//UPDATE: lehet, h nincs ilyen fv?
	var layerTreeVar = harmonikaVar.getComponent(2);
	harmonikaVar.remove(layerTreeVar, true);
	harmonikaVar.add({
		title: 'Rétegválasztó',
		xtype: 'layertree',
		layerTree: layerTree,
		map: map,
		model: model
	});
	harmonikaVar.doLayout();
}
