﻿// JScript File

TNRIS.PanMapTool = function(mapPanel, onSuccess) {    
    TNRIS.PanMapTool.superclass.constructor.call(this);
    this.mapPanel = mapPanel;
    this.onSuccess = onSuccess;
    this.activeShape = null;
};

Ext.extend(TNRIS.PanMapTool,TNRIS.ToolBase,{
    activate: function() {
        TNRIS.PanMapTool.superclass.activate.call(this);
        this.mapPanel.on({
            'mapclick': {fn: this.onClick, scope: this},
            'map-change': {scope: this, fn: this.onMapChange},
            'beforemapclick': {scope: this, fn: this.onBeforeMapClick}
        });
        // set custom cursor if necessary
        this.onMapChange();
    },
    
    deactivate: function() {
        TNRIS.PanMapTool.superclass.deactivate.call(this);
        this.mapPanel.removeListener('mapclick', this.onClick, this);
        this.mapPanel.removeListener('map-change', this.onMapChange, this);
        this.mapPanel.removeListener('beforemapclick', this.onMapChange, this);
    },
    
    //override base class method as pan map tool takes care of cursor setting by itself depending on searchable layers on the map
    setCursor: function(value,shiftKeyDragging) {
    },
    
    onBeforeMapClick: function(mapEvent){
        if (null != this.contextClickActivity){
            TNRIS.PanMapTool.superclass.doContextClickActivity.call(this,mapEvent);
            return false;
        }
    },

    onClick: function(mapEvent) {
        var layerList = this.mapPanel.getSearchableLayers();
        if (layerList.length > 0) {
            this.mapPanel.showBusy("Search for nearby features...");
            this.searchPoint = this.eventToLatLong(mapEvent);
            PageMethods.findFeatures(layerList, this.searchPoint.Latitude, this.searchPoint.Longitude, this.mapPanel.getMap().GetZoomLevel(), this.onSearchLayers.bind(this));
        }        
    },
    
    onMapChange: function() {
        if (this.mapPanel.getSearchableLayers().length > 0) {
            this.mapPanel.setCustomCursor('default');
        } else {
            this.mapPanel.clearCustomCursor();
        }
    },
    
    /* Map Searching */
    onSearchLayers: function(result) {
        this.mapPanel.clearStatus();
        if (result.length > 0) {
            var win = new TNRIS.PointSearchWindow({mapPanel: this.mapPanel});
            win.load(this.searchPoint, result);
            win.show();
        }
    }
});

if (typeof(Sys) !== "undefined") { Sys.Application.notifyScriptLoaded(); }
