﻿dojo.provide("Indy.PropertyAssessmentViewer.Identify");
var navigatorVersion = navigator.appVersion;
var navigatorIE6 = navigatorVersion.indexOf("MSIE 6", 0);
var navigatorIE7 = navigatorVersion.indexOf("MSIE 7", 0);
var navigatorIE8 = navigatorVersion.indexOf("MSIE 8", 0);
var identifyHandler;
Indy.PropertyAssessmentViewer.Identify = function(){
    
    var onClick = function(){
        toolUp = 'Identify';
        Indy.PropertyAssessmentViewer.Tools.ZoomInTool.onMouseOut();
        Indy.PropertyAssessmentViewer.Tools.ZoomOutTool.onMouseOut();
        Indy.PropertyAssessmentViewer.Tools.PanTool.onMouseOut();
        Indy.PropertyAssessmentViewer.Identify.onMouseDown();
        //Set cursor type. This is not gonna work after first click (http://forums.esri.com/Thread.asp?c=158&f=2396&t=280276)
        document.getElementById("mapDiv").style.cursor = "url('images/cursors/identify.cur'), pointer";
        if (tb != null){
            tb.deactivate();
        }
		idResultsObjectArray = [];
        //Attach onClick event for the tool
        identifyHandler = dojo.connect(myMap, "onClick", doIdentify);
        //Identify task's settings
		identifyParams = new esri.tasks.IdentifyParameters();
        identifyParams.tolerance = 1;
        identifyParams.returnGeometry = true;
        identifyParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_ALL;
    }
    
    //To execute when onClick event is fired (map was clicked)
    var doIdentify = function(evt){
        document.getElementById("mapDiv").style.cursor = "url('images/cursors/identify.cur'), pointer";
        dojo.byId("PnlLoadingMap").style.visibility = "visible";
        //identifyTask = new esri.tasks.IdentifyTask('http://tritenko09865/arcgis/rest/services/ParcelsCCGIS/MapServer');
        identifyTask = new esri.tasks.IdentifyTask(configDataForMap.parcels.mapServiceURL);
        identifyParams.layerIds = [parseFloat(configDataForMap.parcels.layerId)];
        identifyParams.geometry = evt.mapPoint;
        identifyParams.mapExtent = myMap.extent;
        dojo.connect(identifyTask, 'onError', function(){
            dojo.byId("PnlLoadingMap").style.visibility = "hidden";
            Ext.MessageBox.alert('Identify Tool Error','Selected object has not been identified as parcel.',null);        
        });
        identifyTask.execute(identifyParams, function(idResults) { addToMap(idResults, evt); });             
    }
    
    //To do when the identify task is performed
    function addToMap(idResults, evt) {
        document.getElementById("mapDiv").style.cursor = "url('images/cursors/identify.cur'), pointer";    
        console.log(idResults[0].feature);
        var parcelC;
        if (idResults){
            parcelC = idResults[0].feature.attributes.PARCEL_C;
            console.log(parcelC);
            openParcelDatabaseSite(parcelC, true);
        }
        showResults(idResults);
    }
    
    function showResults(results){
        console.log(results);
        myMap.graphics.clear();
        var curFeature = results[0];
        var graphic = curFeature.feature;
        var layerName = curFeature.layerName;
        var layerId = curFeature.layerId;
        var foundFieldName = curFeature.foundFieldName;
        var foundFieldValue = graphic.attributes[foundFieldName];

        switch (graphic.geometry.type) {
        case "point":
            var symbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE, 10, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255,0,0]), 1), new dojo.Color([0,255,0,0.25]));
            break;
        case "polyline":
            var symbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASH, new dojo.Color([255,0,0]), 1);
            break;
        case "polygon":
            var symbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color("black"), 1), new dojo.Color([0,255,255,0.7]));
            break;
        }
        graphic.setSymbol(symbol);
        
        if (graphic.geometry.type == "point"){
            var pointX = graphic.geometry.x;
            var pointY = graphic.geometry.y;
            xmin = pointX - 1/pointX;
            ymin = pointY - 1/pointY;
            xmax = pointX + 1/pointX;
            ymax = pointY + 1/pointY;
            spatialReference = myMap.spatialReference;
            featureExtent = new esri.geometry.Extent(xmin, ymin, xmax, ymax, spatialReference);
        }else{
            featureExtent = graphic.geometry.getExtent();
        }
        myMap.graphics.add(graphic);
        myMap.setExtent(featureExtent);
        document.getElementById("mapDiv").style.cursor = "url('images/cursors/identify.cur'), pointer";
    }

    var onMouseDown = function(){
        dojo.byId("IdentifyTool").style.backgroundImage = "url('images/identify_ON.gif')";
        document.getElementById("mapDiv").style.cursor = "url('images/cursors/identify.cur'), pointer";
    }
    
    var onMouseOut = function(){
        if (toolUp != 'Identify'){
            dojo.byId("IdentifyTool").style.backgroundImage = "url('images/identify.png')";
        }
    }
    
    var onMouseOver = function(){
        if (toolUp != 'Identify'){
            dojo.byId("IdentifyTool").style.backgroundImage = "url('images/identify_HOVER.gif')";
        }
    }

    return{
        onClick:onClick,
        doIdentify:doIdentify,
        onMouseDown:onMouseDown,
        onMouseOut:onMouseOut,
        onMouseOver:onMouseOver
    }
}();