/**
 * @requires OpenLayers/OpenLayers.js
 */

/**
 * Class: GMES.LayerList 
 */
GMES.LayerList = OpenLayers.Class({
	
	map : null, 	
	legendVisibilities: [],
	pagerIndex : 1,
	filters : null,
		
	initialize : function(map) {
		this.map = map;
	},
	
	getLayerNames : function() {
		var overlayLayers = this.map.getLayersBy("isBaseLayer",false)		
		overlayLayers = overlayLayers.collect(function(layer) {return layer.name; });
		return overlayLayers.without("Boxes");
	},
	
	getLayersAsJSON: function(){	
		var that = this;
		return this.getLayerNames().collect(function(layerName){
			return that.getLayer(layerName).toJSON();			
		});
	},		
	
	getLayer : function(layerName) {		
		var layerList = this.map.getLayersByName(layerName);
		if(layerList && layerList.size()==1){
			return layerList[0];
		}else{
			return null;
		}
	},
	
	addLayer : function(layer) {		
		this.map.addLayer(layer);		
		if(GMES.bbox!=undefined){
			GMES.bbox.setZIndex(layer.getZIndex()+1);
		}
	},
	
	removeLayer : function(layerName) {
		var layer = this.getLayer(layerName);
		this.map.removeLayer(layer);				
	},
	
	getVisibility : function(layerName) {
		return this.getLayer(layerName).getVisibility();
	},
	
	setVisibility : function(layerName, visible) {		
		var layer = this.getLayer(layerName);
		layer.setVisibility(visible);		
	},
		
	joinByProperty : function(propertyGetter){
		var layerList = this;
		var result = this.getLayerNames().collect(function(name){ return propertyGetter.apply(layerList,[name]);}).join(",");
		if(result.startsWith(",")) result = "";
		return result;
	},
	
	getVisibilities : function() {				
		return this.joinByProperty(this.getVisibility);		
	},
	
	toggleVisibility : function(layerName) {
		this.setVisibility(layerName, !this.getVisibility(layerName));
	},
	
	getOpacity : function(layerName) {		
		var layer = this.getLayer(layerName);
		if(layer==null || layer==undefined) return 0.0;
		var opacity = layer.opacity
		if(opacity==null || opacity==undefined) return 1.0
		GMES.debug("Opacity: "+opacity);		
		return opacity;	
	},
	
	setOpacity : function(layerName, opacity) {		
		var layer = this.getLayer(layerName);
		layer.setOpacity(opacity);			
	},
	
	getOpacities : function() {
		return this.joinByProperty(this.getOpacity);	
	},
	    
    getLegendVisibility: function(layerName) {		
    	return (this.legendVisibilities[layerName] == null || this.legendVisibilities[layerName] == undefined) ? false : this.legendVisibilities[layerName];
    },
    
    setLegendVisibility: function(layerName,legendVisibility) {		
    	this.legendVisibilities[layerName] = legendVisibility;
    },
    
    toggleLegendVisibility: function(layerName) {
    	this.legendVisibilities[layerName] = !this.getLegendVisibility(layerName);
    },
    
    getLegendVisibilities : function() {
    	return this.joinByProperty(this.getLegendVisibility);			
	},		
		
	pullUp : function(layerName) {
		var layer = this.getLayer(layerName);
		this.map.raiseLayer(layer,+1);		
	},	
		
	pushDown : function(layerName) {
		var layer = this.getLayer(layerName);
		this.map.raiseLayer(layer,-1);		
	},				
	
	getPagerIndex : function() {
		return this.pagerIndex;
	},
	
	setPagerIndex : function(currentPage) {
		this.pagerIndex = currentPage;
	},
	
	getFilterItems : function() {
		return this.filters;
	},
	
	setFilterItems : function(filters) {
		this.filters = filters;
	}	
});	
