﻿// JScript File

TNRIS.MapStatusbar = function(config) {
    Ext.apply(this, config);

    this.veMap = this.mapPanel.getMap();
    var center = this.veMap.GetCenter();
    this.shapeAttributes = "";
        
    TNRIS.MapStatusbar.superclass.constructor.call(this, {
        items: [

            { xtype : 'tbbutton', id: 'leng-btn'},
            { xtype: 'label', id:'leng-lbl'},
            
            { xtype : 'tbbutton', id: 'union_length-btn'},
            { xtype: 'label', id:'union_length-lbl'},
            
            { xtype : 'tbbutton', id: 'intersec_length-btn'},
            { xtype: 'label', id:'intersec_length-lbl'},

            { xtype : 'tbbutton', id: 'area-btn'},
            { xtype: 'label', id:'area-lbl'},
            
            { xtype : 'tbbutton', id: 'union_area-btn'},
            { xtype: 'label', id:'union_area-lbl'},

            { xtype : 'tbbutton', id: 'intersec_area-btn'},
            { xtype: 'label', id:'intersec_area-lbl'},

            { xtype : 'tbbutton', id: 'radi-btn'},
            { xtype: 'label', id:'radi-lbl'},
            
            {
                xtype: 'combo',
                id: 'unitSelector',
                editable: false,
                fieldLabel: 'Unit',
                mode: 'local',
                triggerAction: 'all',
                selectFocus: 'true',
                displayField : 'display',
                valueField: 'value',
                width: 60,
                hidden: true,
                value: 'mile',
                store: new Ext.data.SimpleStore({
                    id: 'unitSelectCombo',
                        fields: ['value', 'display'],
                        data: [['mile', 'Mile'], ['km', 'Km'],['meter','Meter']]
                }),
                listeners: {
                    'select' : {
                        scope: this,
                        fn: function(combo, record,index) {
                            this.refreshShapeStatus();
                        }
                    }
                }
            },
            
            {text: 'LatLong:'}, 
            '<div id="latlongLbl">' + center.Latitude.toFixed(8) + ', ' + center.Longitude.toFixed(8) + '</div>',
            '-',
            {text: 'ZoomLevel:'},
            '<span id="zoomLevelLbl" class="x-btn button">' + this.veMap.GetZoomLevel() + '</span>',
            '-',
            '<div id="mapStateStatus"></div>',
            {
                xtype: 'tbbutton',
                text: '.',
                scope: this,
                handler: function() {
                    var map = this.mapPanel.getMap();
                    if (map.GetMapMode() == VEMapMode.Mode2D) {
                        map.SetMapMode(VEMapMode.Mode3D);
                    } else {
                        map.SetMapMode(VEMapMode.Mode2D);
                    }
                }
            }
        ]
    });

    var state = Ext.state.Manager.getProvider();
    state.on('statechange', function() {
        Ext.fly('mapStateStatus').addClass('dirty');
    }, this);
    state.on('statesaved', function() {
        Ext.fly('mapStateStatus').removeClass('dirty');
    }, this);

    this.mapPanel.getMap().AttachEvent("onendzoom", this.onEndZoom.bind(this));
    this.mapPanel.getMap().AttachEvent("onmousemove", this.onMove.bind(this));
};

Ext.extend(TNRIS.MapStatusbar, Ext.StatusBar,{

    onEndZoom: function(e) {
        Ext.fly('zoomLevelLbl').update("" + e.zoomLevel);
    },

    onMove: function(e) {
        var latlong = this.veMap.PixelToLatLong(new VEPixel(e.mapX,e.mapY));
        Ext.fly('latlongLbl').update("" +  latlong.Latitude.toFixed(8) + " , " + latlong.Longitude.toFixed(8) );
    },
    
    setShapeStatus : function(shapeAttributes) {
        this.shapeAttributes = shapeAttributes;
        this.clearShapeStatus();
        this.refreshShapeStatus();
    },
    
    refreshShapeStatus : function() {
        if (null == this.shapeAttributes) {
            return;
        }

        var precision = 2;
        var result = null;
        var selector = Ext.getCmp('unitSelector');
        var unit = selector.getValue();
        
        switch(unit) {
            case "km":
                result = this.convertValues('mile-km');
                break;
            case "mile":
                result = this.shapeAttributes;
                break;
            case "meter":
                result = this.convertValues('mile-meter');
                break;
            default: break;
        }

        var object =result;
        selector.setVisible(true);
        
        if (object.length && object.length != 0 ) 
        { 
            Ext.getCmp('leng-btn').setVisible(true);
            Ext.getCmp('leng-btn').setText('Length:'); 
            Ext.get('leng-lbl').update(object.length.toFixed(precision) + " " +unit); 
        } 
        
        if (object.area && object.area !=0 ) 
        { 
            Ext.getCmp('area-btn').setVisible(true);
            Ext.getCmp('area-btn').setText('Area:'); 
            Ext.get('area-lbl').update(object.area.toFixed(precision)+ " sq."+unit); 
        } 

        if (object.radius && object.radius !=0 ) 
        { 
            Ext.getCmp('radi-btn').setVisible(true);
            Ext.getCmp('radi-btn').setText('Radius:'); 
            Ext.get('radi-lbl').update(object.radius.toFixed(precision)+ " " +unit); 
        } 
        
        if (object.union_length && object.union_length !=0 ) 
        { 
            Ext.getCmp('union_length-btn').setVisible(true);
            Ext.getCmp('union_length-btn').setText('U:'); 
            Ext.get('union_length-lbl').update(object.union_length.toFixed(precision)+ " " +unit); 
        } 


        if (object.union_area && object.union_area !=0 ) 
        { 
            Ext.getCmp('union_area-btn').setVisible(true);
            Ext.getCmp('union_area-btn').setText('U:'); 
            Ext.get('union_area-lbl').update(object.union_area.toFixed(precision)+ " sq." +unit); 
        } 
        
        if (object.intersec_length && object.intersec_length !=0 ) 
        { 
            Ext.getCmp('intersec_length-btn').setVisible(true);
            Ext.getCmp('intersec_length-btn').setText('I:'); 
            Ext.get('intersec_length-lbl').update(object.intersec_length.toFixed(precision)+ " " +unit); 
        } 

        if (object.intersec_area && object.intersec_area !=0 ) 
        { 
            Ext.getCmp('intersec_area-btn').setVisible(true);
            Ext.getCmp('intersec_area-btn').setText('I:'); 
            Ext.get('intersec_area-lbl').update(object.intersec_area.toFixed(precision)+ " sq." +unit); 
        } 
    },
    
    clearShapeStatus: function() {
        Ext.getCmp('unitSelector').setVisible(false);

        Ext.getCmp('leng-btn').setVisible(false);
        Ext.getCmp('area-btn').setVisible(false);
        Ext.getCmp('radi-btn').setVisible(false);
        Ext.getCmp('union_length-btn').setVisible(false);
        Ext.getCmp('union_area-btn').setVisible(false);
        Ext.getCmp('intersec_length-btn').setVisible(false);
        Ext.getCmp('intersec_area-btn').setVisible(false);

        Ext.get('leng-lbl').update("");
        Ext.get('area-lbl').update("");
        Ext.get('radi-lbl').update("");
        Ext.get('union_length-lbl').update("");
        Ext.get('union_area-lbl').update("");
        Ext.get('intersec_length-lbl').update("");
        Ext.get('intersec_area-lbl').update("");


    },
    
    convertValues: function(operation) {
        var conversionFactor=0;
        
        switch(operation) {
            case 'mile-km':
                conversionFactor = 1.609344;
                break;
            case 'mile-meter':
                conversionFactor = 1609.344;
                break;
            default: break;
        }
        
        var object={};
        
        if (this.shapeAttributes.length) 
        {   object.length = this.shapeAttributes.length * (conversionFactor);   }

        if (this.shapeAttributes.area)
        {   object.area = this.shapeAttributes.area * (conversionFactor);   }
        
        if (this.shapeAttributes.radius) 
        {   object.radius = this.shapeAttributes.radius * (conversionFactor);   }
        
        if (this.shapeAttributes.union_area) 
        {   object.union_area = this.shapeAttributes.union_area * (conversionFactor);   }
        
        if (this.shapeAttributes.union_length) 
        {   object.union_length = this.shapeAttributes.union_length * (conversionFactor);   }

        if (this.shapeAttributes.intersec_area) 
        {   object.intersec_area = this.shapeAttributes.intersec_area * (conversionFactor);   }

        if (this.shapeAttributes.intersec_length) 
        {   object.intersec_length = this.shapeAttributes.intersec_length * (conversionFactor);   }
        
        return object;
    }
});

if (typeof(Sys) !== "undefined") { Sys.Application.notifyScriptLoaded(); }
