﻿// JScript File
var zoomToXYMethod = "";
var zoomToX;
var zoomToY;
var inputX = new String();
var inputY = new String();

function setupDialog() {
    //if the zoomToXYMehtod has not been set then set it and
    //show the correct entry boxes - this should only occur
    //on the first time the dialog box is used.
    if (zoomToXYMethod == "") {
        zoomToXYMethod = "XY";
        setZoomMethod("XY");
    }
}

function setZoomMethod(searchType) {
    //first hide all search types
    dojo.byId('cpZoomStatePlane').style.visiblity = "hidden";
    dojo.byId('cpZoomStatePlane').style.display = "none";
    dojo.byId('cpZoomDMS').style.visiblity = "hidden";
    dojo.byId('cpZoomDMS').style.display = "none";
    dojo.byId('cpZoomDD').style.visiblity = "hidden";
    dojo.byId('cpZoomDD').style.display = "none";
    
    switch (searchType.toUpperCase())
    {
        case 'XY':
            dojo.byId('cpZoomStatePlane').style.visiblity = "visible";
            dojo.byId('cpZoomStatePlane').style.display = "block";
            zoomToXYMethod = "XY";
            break;
        case 'DMS':
            dojo.byId('cpZoomDMS').style.visiblity = "visible";
            dojo.byId('cpZoomDMS').style.display = "block";
            zoomToXYMethod = "DMS";
            break;
        case 'DD':
            dojo.byId('cpZoomDD').style.visiblity = "visible";
            dojo.byId('cpZoomDD').style.display = "block";
            zoomToXYMethod = "DD";
            break;
    }   
}

function processXY() {
    //items needed for projecting coordinate from one projection to another
    var point;
    var graphicPoint;    
    var symbol = new esri.symbol.SimpleMarkerSymbol().setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_DIAMOND);
    var zoomPoint;
    
    //longitude and latitude in decimal degrees
    var lonDD = 0;
    var latDD = 0;
    
    //get a reference to the geometry service for projecting points
    var gsvc = new esri.tasks.GeometryService('http://new.kgis.net/ArcGIS/rest/services/Geometry/GeometryServer');
    
    //set the output spatial reference for point projections
    var outSR = new esri.SpatialReference({ wkid: 2915});

    //besure we have a method set
    if (zoomToXYMethod == "") {
        alert('Missing Zoom To Method');
        return false;
    }
    
    //if we aren't using state plane coordinates then do the conversion
    if (zoomToXYMethod == "DMS") {
        //longitude
        var lonDeg = parseInt(dojo.byId("txtLonDeg").value);
        var lonMin = parseInt(dojo.byId("txtLonMin").value);
        var lonSec = parseFloat(dojo.byId("txtLonSec").value);
        //latitude
        var latDeg = parseInt(dojo.byId("txtLatDeg").value);
        var latMin = parseInt(dojo.byId("txtLatMin").value);
        var latSec = parseFloat(dojo.byId("txtLatSec").value);
        
        inputX = lonDeg.toString() + "\u00B0 " + lonMin.toString() + "\' " + lonSec.toString() + "\"";
        inputY = latDeg.toString() + "\u00B0 " + latMin.toString() + "\' " + latSec.toString() + "\"";

        //convert from degrees minutes seconds to decimal degrees
        lonDD = lonDeg + ((lonMin + (lonSec / 60)) / 60);
        latDD = latDeg + ((latMin + (latSec / 60)) / 60);
        
        //create the graphics for the map and project it to the correct coordinate system
        point =  new esri.geometry.Point(lonDD, latDD, new esri.SpatialReference({ wkid: 4326 }));
        graphicPoint = new esri.Graphic(point, symbol);
        
        //project the point
        gsvc.project([ graphicPoint ], outSR, getProjectedPoint)
            
    } else if (zoomToXYMethod == "DD") {
        lonDD = parseFloat(dojo.byId("txtLonDD").value);
        latDD = parseFloat(dojo.byId("txtLatDD").value);

        inputX = lonDD.toString() + "\u00B0";
        inputY = latDD.toString() + "\u00B0";

        //create the graphics for the map
        point =  new esri.geometry.Point(lonDD, latDD, new esri.SpatialReference({ wkid: 4326 }));
        graphicPoint = new esri.Graphic(point, symbol);

        //project the point
        gsvc.project([ graphicPoint ], outSR, getProjectedPoint)
        
    } else {
        //already using state plane coordinates so just load the global variables and continue
        zoomToX = parseInt(dojo.byId("txtEasting").value);
        zoomToY = parseInt(dojo.byId("txtNorthing").value);

        //create the text string to put on the map
        inputX = zoomToX.toString();
        inputY = zoomToY.toString();

        //call the zoom function
        zoomToXY();
    }        
}

function getProjectedPoint(features) {
    //get the newly projected point
    var pt = features[0].geometry;

    //load the gloabal variables and continue
    zoomToX = pt.x;
    zoomToY = pt.y;
    zoomToXY();
}

function zoomToXY() {
    //clear any existing graphics
    map.graphics.clear();
    
    //create the point for zooming
    zoomPoint = new esri.geometry.Point(zoomToX, zoomToY, map.spatialReference);
    var havePoint = false;
    
    //make sure we have data within this range by checking the full extent of all map layers
    //if we have at least one map layer within range the zoom the map
    for(var j = 0; j < map.layerIds.length; j++) {
        var layer = map.getLayer(map.layerIds[j]);
        var fullExt = layer.fullExtent;
        if (fullExt.contains(zoomPoint) == true) {
            havePoint = true;
            j = map.layerIds.length;
        }
    }
    
    //if the point is outside of the map extent let the user know
    if (havePoint != true) {
        alert("The point you entered in outside of the map extents.");
        return false;
    }
    
    //create the point graphics for the map
    var lineSymbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255,0,0,1]), 2);
    var pointSymbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_X, 10, lineSymbol, new dojo.Color([255,0,0,1]))
    var pointGraphic = new esri.Graphic(zoomPoint, pointSymbol);
    map.graphics.add(pointGraphic);
            
    //create the text graphics for the map
    var txtString = "X: " + inputX + "\nY: " + inputY;
    var pointFont = new esri.symbol.Font(12, esri.symbol.Font.STYLE_NORMAL, esri.symbol.Font.VARIANT_NORMAL, esri.symbol.Font.WEIGHT_BOLD, "Tahoma")
    var pointText = new esri.symbol.TextSymbol(txtString, pointFont, new dojo.Color([255,0,0,1]));
    pointText.setOffset(12,8);
    pointText.setAlign(esri.symbol.TextSymbol.ALIGN_START);
    map.graphics.add(new esri.Graphic(zoomPoint, pointText));
    
    //set the map extent
    var env = new esri.geometry.Extent(zoomToX - 500, zoomToY - 500, zoomToX + 500, zoomToY + 500, map.spatialReference);
    map.setExtent(env);
}

