
 
 function _labelLayer( o ) {
//	var map = myKaMap.getCurrentMap();

	this.name = (typeof(o.name) != 'undefined') ? o.name : 'unnamed';
	this.id = (typeof(o.id) != 'undefined') ? o.id : '';
	this.guid = (typeof(o.guid) != 'undefined') ? o.guid : '';
    this.group = (typeof(o.group) != 'undefined') ? o.group : '';
	this.type = (typeof(o.type) != 'undefined') ? o.type : '';
	this.highlight = (typeof(o.highlight) != 'undefined') ? o.highlight : '';
	this.visible = (typeof(o.visible) != 'undefined') ? o.visible : true;
	this.opacity = (typeof(o.opacity) != 'undefined') ? o.opacity : 100;
	this.imageformat = (typeof(o.imageformat) != 'undefined') ? o.imageformat : 'dithered';
    this.imageext = (typeof(o.imageext) != 'undefined') ? o.imageext : '.png';
	this.queryable = (typeof(o.queryable) != 'undefined') ? o.queryable : false;
    this.queryState = (typeof(o.queryable) != 'undefined') ? o.queryable : false;
	this.tileSource = (typeof(o.tileSource) != 'undefined') ? o.tileSource : 'cache';
	this.extent = (typeof(o.extent) != 'undefined') ? {minx:o.extent.minx,miny:o.extent.miny,maxx:o.extent.maxx,maxy:o.extent.maxy} : {minx:0,miny:0,maxx:0,maxy:0};
//	this.extent = {minx:0,miny:0,maxx:0,maxy:0};
//	this.units = (typeof(o.units) != 'undefined') ? o.units : map.units;
	this.scales = (typeof(o.scales) != 'undefined') ? o.scales : new Array();
    this.redrawInterval = (typeof(o.redrawInterval) != 'undefined') ? o.redrawInterval : -1;
    this.refreshInterval = (typeof(o.refreshInterval) != 'undefined') ? o.refreshInterval : -1;

    this.viewLegend = (typeof(o.legend) != 'undefined') ? o.legend : 0;
//    this.metaWidth = (typeof(o.metaWidth) != 'undefined') ? o.metaWidth : map.kaMap.metaWidth;
//    this.metaHeight = (typeof(o.metaHeight) != 'undefined') ? o.metaHeight : map.kaMap.metaHeight;

//    this.minScale = (typeof(o.minScale) != 'undefined') ? parseFloat(o.minScale) : 0;
//    this.maxScale = (typeof(o.maxScale) != 'undefined') ? parseFloat(o.maxScale) : 0;

//Debug('this.minScale: '+this.minScale);
//Debug('this.maxScale: '+this.maxScale);
	this.Labels = this.getLayerForLabel()


	for (i=0;i<aszScales.length;i++){this.scales[i] = 1;}

	_layer.apply(this,[{ name:this.name
						,id:this.id
						,guid:this.guid
						,group:this.group
						,type:this.type
						,highlight:this.highlight
						,visible:this.visible
						,opacity:this.opacity
						,imageformat:this.imageformat
						,imageext:this.imageext
						,queryable:this.queryable
						,queryState:this.queryState
						,tileSource:this.tileSource
						,extent:this.extent
						,scales:this.scales
						,redrawInterval:this.redrawInterval
						,refreshInterval:this.refreshInterval
					   }]);


	for (var p in _layer.prototype) {
        if (!_labelLayer.prototype[p])
            _labelLayer.prototype[p]= _layer.prototype[p];
    }

//	myKaMap.registerForEvent( KAMAP_LAYER_STATUS_CHANGED, this, this.refres );

};
 
_labelLayer.prototype.setTile = function(img) {
    var l = safeParseInt(img.style.left) + this._map.kaMap.xOrigin;
    var t = safeParseInt(img.style.top) + this._map.kaMap.yOrigin;

	var bChk = this.CheckTile(l,t);
	if ( bChk ) return;

	// dynamic imageformat
    var szImageformat = '';
    var image_format = '';
    if (this.imageformat && this.imageformat != '') {
        image_format = this.imageformat;
        szImageformat = '&i='+image_format;
    }

	var szScale = '&s='+this._map.aScales[this._map.currentScale];
	var szLayers = '&layers='+this.Labels.join(',');//this.getLayerForLabel().join(',')

	var src = this._map.kaMap.server
		+ this._map.kaMap.labelURL + '?'
		+ 'map=' + this._map.name
		+ '&t=' + t
		+ '&l=' + l
		+ '&epsgMap=' + this._map.epsg
		+ szLayers
		+ szScale
		+ szImageformat

//Debug('_labelLayer.prototype.setTile: '+img.layer.zIndex+', '+img.id+', <> '+img.src )
	if (img.src != src  && !(img.ie_hack && img.srcAlpha == src) || arguments[1] ) {
//Debug('_labelLayer.prototype.setTile: '+src )
		//this._map.kaMap.nImg++;
        img.style.visibility = 'hidden';
        img.src = src;
    }
}

_labelLayer.prototype.getLayerForLabel = function(  ) {
	var oMap = myKaMap.getCurrentMap();
	var layers = oMap.getLayers();
	var aLayer = new Array();
	for( var k=0; k<layers.length; k++) {
		var l = layers[k];
		if ( l.label && l.isVisible() ) aLayer.push(l.id);
	}
//Debug('_labelLayer.prototype.getLayerForLabel: '+aLayer.length )
	return aLayer
}

_labelLayer.prototype.SetLayerForLabel = function(  ) {
	this.Labels = this.getLayerForLabel()
}

_labelLayer.prototype.refresh = function() {
//Debug('_labelLayer.prototype.refresh');
	this.SetLayerForLabel();
	this.setTileLayer(true);
};


_labelLayer.prototype.CheckTile = function( l , t ) {
	var oMap = this._map;
	var oKaMap = this._map.kaMap;

	var ret = false;
//Debug('CheckTile l:'+l+' t:'+t);

	var scale = oKaMap.getCurrentScale()
	var minx = (l * oMap.geoWidth) - oMap.geoBuffer;
	var maxx = minx + oMap.geoWidth * oKaMap.tileWidth;
	var maxy = (-1 * t * oMap.geoHeight) + oMap.geoBuffer;
	var miny = maxy - oMap.geoHeight * oKaMap.tileHeight;

	var Tollerance = oMap.CheckTileTolerance * scale
	// controllo immagini visibili
	var ViewPortExt = oKaMap.getGeoExtents()
	if (    maxx < (ViewPortExt[0])
		 || maxy < (ViewPortExt[1])
		 || minx > (ViewPortExt[2])
		 || miny > (ViewPortExt[3])
		)
		ret = true;
//Debug('CheckTile l:'+l+' t:'+t+' -> '+ret);

	return ret;
}



