 /***********************************************************************
 * Class: Fusion.Tool.sneLayerManager
 *
 *
 * **********************************************************************/
Fusion.Tool.sneLayerManager = OpenLayers.Class(Fusion.Widget, {
    print : false,  	// Wether or not to log.
    activePanel : [],  	// Array of active panels
    initialize : function() {
		this.activeSender = '';
		activePanel = [];
    },
 
	/* Function: setActiveLayers 
	  *
	  * This function is called by panel widgets to hold information about what layers are currently
	  * active in the map. If bAddLayers parameter is set to true, layers are added to the array. 
	  * If it is false, only those layers that are inside the layerString (split with ,) are removed from 
	  * array. Senders (i.e. panels) are also kept track off so that if one panel is active and sends layers
	  * and then another one sends other layers, the previous ones are reset so user can only select 
	  * by layers that are active according to the panel he is viewing.
	  *
	  */
	  

	setActiveLayers : function (layerString,bAddLayers,sender) {
		if (this.activeSender != sender) {
			activePanel = [];		
		}
		this.activeSender = sender;
		var newLayersArray = layerString.split(',');
		var bExistsInActivePanel = false;
		if (bAddLayers == true) {
			// Adding layers
			for (var i = 0; i< newLayersArray.length; i++)  {
				bExistsInActivePanel = false;
				for ( var j = 0; j < activePanel.length; j++) {
					if (newLayersArray[i] == activePanel[j]) {
						bExistsInActivePanel = true;
						break;
					}
				}
				if (!bExistsInActivePanel)
					activePanel.push(newLayersArray[i]);
			}
		} else {
			// Removing layers.
			for(var i = 0; i < newLayersArray.length; i++) {
				for(var j = 0; j < activePanel.length; j++) {
					if (newLayersArray[i] == activePanel[j]) 
						activePanel.splice(j,1);
				}
			}
		}
		if(this.print)
			console.log('SneLayerManager setActiveLayers: ',activePanel);
	},
	
	/* Function : getActiveLayers
	  *
	  * GetActiveLayers returns a string with all the elements in the active layers array
	  * seperated by a comma so that it can be used as a filter for map queries when a 
	  * user clicks the map and performs a select (See widgets/select.js)
	  *
	  */
	getActiveLayers : function() {
		var strActLay = '';
		for (var i = 0; i < activePanel.length; i++)
		{
			strActLay = strActLay + activePanel[i];
			if (!(activePanel.length-1 == i))
				strActLay = strActLay + ',';
		}
		if(this.print)
			console.log('SneLayerManager getActiveLayers :',activePanel);
		return strActLay;
	},
	
	addActiveLayers : function (layers,sender) {
		this.setActiveLayers(layers,true,sender);
	},
	
	removeActiveLayers : function(layers,sender) {
		this.setActiveLayers(layers,false,sender);
	},
	
	clearLayers : function (sender) {
		activePanel = [];
		this.activeSender = sender;
	}
});


