﻿/**
@class A Layer backed by raster tiles
@constructor
@augments TNRIS.LayerBase
*/
TNRIS.TileLayer = function(config) {
    TNRIS.TileLayer.superclass.constructor.call(this, config );
    
    Ext.applyIf(this.config, {
        'opacity': 1.0
    });
    this.defaultMinZoom = 6;
    this.defaultMaxZoom = 19;

};

Ext.extend(TNRIS.TileLayer, TNRIS.LayerBase, 
    /** @scope TNRIS.TileLayer */
    {
    /** 
    sets or gets the order this layer is drawn in relation to other tiled layers
    @param {integer} [value] the draw order this layer should use
    */
    index: function(value) {
        var current = this.defaultMethod('zindex');
        if (typeof(value) != 'undefined' && value != current) {
            if (this.tileSource()) {
                this.tileSource().ZIndex = value;
                this.redraw();
            }
        }
        return this.defaultMethod('zindex', value);
    },
    
    tileSource: function(value) {
        return this.defaultMethod('tileSource', value);
    },
    
    virtualEarthUrl: function(value) {
        return this.defaultMethod('virtual_earth_url', value);
    },
    
    /**
    sets or gets the opacity of the tile layer.  A redraw is performed if the opacity level is changed
    @param {integer} [value] the opacity setting to use (between 0 and 1.0)
    @param {boolean} [temporary] whether or not to persist this opacity change - defaults to true
    */
    opacity: function(value, temporary) {
        if (typeof(value) != 'undefined') {
            if (this.tileSource()) {
                this.tileSource().Opacity = value;
            }
            if (typeof(temporary) == 'undefined' || !temporary) {
                this.set('opacity', value);
                this.mapPanel() && this.mapPanel().fireEvent('state-change');
            }
            this.redraw();
        }
        return this.get('opacity');
    },
    
    redraw: function() {
        if (this.map() != null) {
            if (this.visible()) {
                this.map().ShowTileLayer(this.mapId());
            } else {
                this.map().HideTileLayer(this.mapId());
            }
        }
    },
    
    footprintAsLongRectangle: function() {
        var shape = this.footprintAsVEShape();
        if (shape == null) {
            return new VELatLongRectangle(new VELatLong(36,-106),new VELatLong(25,-93), new VELatLong(36, -93), new VELatLong(25, -106));
        }
        var shapeLayer = new VEShapeLayer();
        shapeLayer.AddShape(shape);
        var rect = shapeLayer.GetBoundingRectangle();
        shapeLayer.DeleteAllShapes();
        return rect;
    },
    
    footprintAsVEShape: function(){
        var pointList = this.footprintAsPointList();
        if (null != pointList) {
            return new VEShape(VEShapeType.Polygon, pointList);
        } else {
            return null;
        }
    }, 
    
    footprintAsPointList: function() {
        var pointArray = this.get('footprint');
        if (pointArray == null) {
            return null;
        }
        
        var pointList = [];
        for(var i = 0; i < pointArray.length; i++) {
            var latitude = pointArray[i][0];
            var longitude = pointArray[i][1];
            pointList.push(new VELatLong(latitude, longitude));
        }
        return pointList;
    },
    
    addToMap: function(id, map, index, mapPanel) {
        this.mapId(id);
        this.map(map);
        this.mapPanel(mapPanel);
        var bounds = [this.footprintAsLongRectangle()];
        if (this.virtualEarthUrl() != null) {
            this.tileSource(this.addOverlay(id, this.virtualEarthUrl(), bounds, this.get('minZoomLevel'), this.get('maxZoomLevel'), 1, this.opacity(), index, this.visible()));
        }
        /**
        Custom Event fired when the layer has successfully loaded
        @event load
        */
        this.fireEvent('loadmap');
    },
    
    removeFromMap: function() {
        this.map().DeleteTileLayer(this.mapId());
        TNRIS.TileLayer.superclass.removeFromMap.call(this);
    },
    
    addOverlay: function(mapID, source, bounds, minZoom, maxZoom, numServers, opacity, zIndex, isVisible)
    {
        var tileSourceSpec = new VETileSourceSpecification(mapID, source);
        tileSourceSpec.Bounds = bounds;
        tileSourceSpec.MinZoomLevel = minZoom;
        tileSourceSpec.MaxZoomLevel = maxZoom;
        tileSourceSpec.NumServers = numServers;
        tileSourceSpec.Opacity = opacity;
        tileSourceSpec.ZIndex = zIndex;
        
        try {
            this.map().AddTileLayer(tileSourceSpec, isVisible);
        } catch (e) {
            Ext.Msg.alert("Exception", e.message);
        }
        return tileSourceSpec;
    }
});

if (typeof(Sys) !== "undefined") { Sys.Application.notifyScriptLoaded(); }
