﻿/*
Map toolbar functions.

Depends on datastructures.js

Dan Monego
CDM
2008
*/
//The dojo link to the last tool set to use
var currentClickTool;

//Clears the map and the query result pane.
function clearAll(map, resultPaneId)
{
    map.graphics.clear();
    document.getElementById(resultPaneId).innerHTML = "";
}

function zoomOutTool(map)
{
    map.setExtent(map.extent.expand(2));
}

function zoomInTool(map)
{
    map.setExtent(map.extent.expand(0.5));
}

function backOneExtent(extentStack, map)
{
    map.setExtent(extentStack.pop());
}

function clearTools()
{
    document.getElementById("tbIdentify").src = "images/id_parcel.gif";
    if(currentClickTool != null)
    {
        dojo.disconnect(currentClickTool);
    }
}

function printMap(map, layer)
{
    function printPage(mapImage)
    {
        var printwindow = window.open("", "_blank", "height=h, width=w, resizable, scrollbars");
        var imageLocation = mapImage.href.replace(configuration['localserver'], configuration['server']);
        var doc = printwindow.document;
        doc.write("<html><head><title>Printable Map</title></head>");
        doc.write("<body>");
        doc.write("<a href=\"#\" onclick=\"window.print(); return false;\" style=\"float:right;\">");
        doc.write("<img src=\"images/print.gif\" alt=\"print\" style=\"border:none;\">");
        doc.write("</a><br />");
        doc.write("<img src='" + imageLocation + "' alt='Printable Map'/>");
        doc.write("</body></html>");
        doc.close();
    }
    var params = new esri.layers.ImageParameters();
    params.format = "png";
    params.bbox = map.extent;
    params.height = map.height;
    params.width = map.width;
    params.layerIds = layer.visibleLayers;
    params.layerOption = esri.layers.ImageParameters.LAYER_OPTION_SHOW;
    layer.exportMapImage(params, printPage);
}

function setClickTool(toolName, map)
{
    var tools = {
        "identify": function(evt)
        {
            var q = new esri.tasks.Query();
            q.geometry = evt.mapPoint;
            q.returnGeometry = true;
            q.spatialRelationship = esri.tasks.Query.SPATIAL_REL_INTERSECTS;
            q.outFields = Datafields.DevelopmentParcel;
            var queryTask = new esri.tasks.QueryTask(serviceURL + "/" + configuration["idxDevParcel"]);
            queryTask.onComplete = function(result)
            {
                var info = new AddressInfo(result.features[0], map);
                info.showOnMap();
            }
            queryTask.execute(q);
        }
    }
    
    dojo.disconnect(currentClickTool);
    currentClickTool = dojo.connect(map, "onClick", tools[toolName]);
    if(toolName == "identify")
    {
        document.getElementById("tbIdentify").src = "images/id_parcel_on.gif";
    }
}