﻿dojo.require("esri.map");
dojo.require("esri.tasks.identify");
dojo.require("esri.tasks.find");
dojo.require("esri.tasks.query");

var map, findTask;

function init() {
    var layersLoaded = 0; 
    var loading = dojo.byId("loadingImg");  

    esriConfig.defaults.map.zoomDuration = 750; 
    esriConfig.defaults.map.zoomRate = 35;


    map = new esri.Map("map");

    dojo.connect(map, "onLoad", showLoading);
    dojo.connect(map, "onZoomStart", showLoading);
    dojo.connect(map, "onPanStart", showLoading);


    var imageParameters = new esri.layers.ImageParameters();
    imageParameters.format = "png24";  
    
    //var imagery = new esri.layers.ArcGISDynamicMapServiceLayer("http://maps.sccommerce.com/ArcGIS/rest/services/Airport_Orthos/MapServer", {"imageParameters":imageParameters});
    //http://vmrevproxy.state.sc.us/ArcGIS/rest/services/Airport_Orthos/MapServer?token=XtUoeWE1b5ouR8dYc9GXqMCXGwwmEF4-n_3LbZrQ5H4.

    var imagery = new esri.layers.ArcGISDynamicMapServiceLayer("http://vmrevproxy.state.sc.us/ArcGIS/rest/services/Airport_Orthos/MapServer?token=XtUoeWE1b5ouR8dYc9GXqMCXGwwmEF4-n_3LbZrQ5H4.", {"imageParameters":imageParameters});

    map.addLayer(imagery);
    dojo.connect(imagery, "onUpdate", hideLoading);

    var faaid = extractID();

    // if this isn't called with the FAAID we'll just see the state
    // which shouldn't happen unless it's being referenced directly
    if (faaid != undefined) {
        zoomToAirport(faaid);
    }

    function showLoading() {
        esri.show(loading);
        map.disableMapNavigation();
        map.hideZoomSlider();
    }

    function hideLoading() {
        layersLoaded++;
        if (layersLoaded === map.layerIds.length) {
            esri.hide(loading);
            map.enableMapNavigation();
            map.showZoomSlider();
            layersLoaded = 0;
        }
    }


      

}

function extractID() {

    var GETDATA = new Array();

    var sGet = window.location.search;
    if (sGet) // if has a value...
    {
        // Drop the leading "?"
        sGet = sGet.substr(1);

        // Generate a string array of the name value pairs.
        // Each array element will have the form "foo=bar"
        var sNVPairs = sGet.split("&");

        // Now, for each name-value pair, we need to extract
        // the name and value.
        for (var i = 0; i < sNVPairs.length; i++) {
            // So, sNVPairs[i] contains the current element...
            // Split it at the equals sign.
            var sNV = sNVPairs[i].split("=");

            // Assign the pair to the GETDATA array.
            var sName = sNV[0];
            var sValue = sNV[1];
            GETDATA[sName] = sValue;
        }
    }

    if (GETDATA["faaid"] != undefined) {
        return GETDATA["faaid"];
    }   

}


function zoomToAirport(faa_id) {
    
    var findParams = new esri.tasks.FindParameters();
    findParams.returnGeometry = true;
    findParams.layerIds = [1];
    findParams.searchFields = ["FAAID"];
    findParams.searchText = faa_id;

    //findTask = new esri.tasks.FindTask("http://maps.sccommerce.com/ArcGIS/rest/services/Airport_Orthos/MapServer");
    findTask = new esri.tasks.FindTask("http://vmrevproxy.state.sc.us/ArcGIS/rest/services/Airport_Orthos/MapServer");
    findTask.execute(findParams, showResults);


}

function showResults(results) {
    //find results return an array of findResult.
    map.graphics.clear();
    var dataForGrid = [];

    for (var i = 0, il = results.length; i < il; i++) {
        var curFeature = results[i];
        var graphic = curFeature.feature;
        var layerName = curFeature.layerName;
        var layerId = curFeature.layerId;
        var foundFieldName = curFeature.foundFieldName;
        var foundFieldValue = graphic.attributes[foundFieldName];
        var extent = new esri.geometry.Extent(0, 0, 0, 0, 26917);

        var attValues = [layerName, layerId, foundFieldName, foundFieldValue];
        dataForGrid.push(attValues);

        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]));
                var text = new esri.symbol.TextSymbol("", "Arial", new dojo.Color([255, 0, 0]));
                var pointPosX = graphic.geometry.x;
                var pointPosY = graphic.geometry.y;
                // wkid 26917 - UTM17N
                extent.xmax = pointPosX + 2050;
                extent.xmin = pointPosX - 2050;
                extent.ymax = pointPosY + 2050;
                extent.ymin = pointPosY - 2050;
                // extent

                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_NONE, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASHDOT, new dojo.Color([255, 0, 0]), 2), new dojo.Color([255, 255, 0, 0.25]));
                extent = graphic.geometry.getExtent()
                extent = extent.expand(1.5);
                break;
        }
        graphic.setSymbol(symbol);
        if (graphic.geometry.type == "point") {
            graphic.setSymbol(text);
        }
        map.graphics.add(graphic);
        map.setExtent(extent);
    }
}