function extentChange(){
    //Change all of the other maps if the main map changes.
    var evt = map.extent.getCenter();
    
    initializeGSV(evt);
    ZoomToVEMap(evt);
    geReZoom(evt);
}

//the purpose of this function is to allow the enter key to 
//point to the correct button to click.
function doClick(e, searchType){
    var key;
    if (e.which) {
        key = e.which;
    }
    else {
        key = e.keyCode;
    }
    if (key == 13) {
        //Get the button the user wants to have clicked
        switch (searchType) {
            case "address":
                searchByAddress();
                break;
                
            case "mgp":
                searchByMGP();
                break;
                
            case "parcel":
                searchByParcelID();
                break;
                
            case "owner":
                searchByOwnerName();
                break;
        }
        if (e.which) {
        
        }
        else {
            e.keyCode = 0;
        }
    }
}

function mapResizeOther(){
    // resize function called when the html body element is resized. Firefox Case
    if (dojo.isIE) {
        // do nothing for IE
    }
    else {
        //Non-IE
        if (map) {
            map.reposition();
            map.resize();
        }
    }
}

function resizeMap(){
    clearTimeout(resizeTimer);
    resizeTimer = setTimeout(function(){
        map.resize();
        map.reposition();
    }, 800);
}

function printMap(){
    // open a new window with Print.html using featureid and extent
    var idString = "";
    var mapDiv = dojo.byId("mapDiv");
    var extString = getExtentString();
    var urlStr = insertIntoHtmlURL("Print.html");
    urlStr += "?featureID=" + idString + "&extent=" + extString + "&appWidth=" + mapDiv.style.width + "&appHeight=" + mapDiv.style.height + "&mapName=" + getMapName();
    window.open(urlStr);
}

function sendMail(){
    // open default email program with URI = featureID and extent
    var emailSubject = "Tax Viewer Map";
    var idString = "";
    var mapStr = "";
    var extString = getExtentString();
    
    if (featureID !== "" && featureID !== undefined && map.infoWindow.isShowing) {
        idString = featureID;
    }
    var url = esri.urlToObject(window.location.toString());
    var urlStr = encodeURI(url.path) + "?featureID=" + featureID + "%26extent=" + extString + "%26mapName=" + getMapName();
    parent.location.href = "mailto:?subject=" + emailSubject + "&body=" + urlStr;
}

function help(){
    // open a new window with appName + Help.pdf
    var urlStr = insertIntoHtmlURL("Help.pdf");
    window.open(urlStr);
}

function insertIntoHtmlURL(addString){
    // insert the supplied string into the html appname URL.
    var urlStr = addString;
    return (urlStr);
}

function getExtentString(){
    // get the extent of the current map and convert to a string xmin,ymin,xmax,ymax
    var extString = map.extent.xmin.toString() + ",";
    extString += map.extent.ymin.toString() + ",";
    extString += map.extent.xmax.toString() + ",";
    extString += map.extent.ymax.toString();
    return (extString);
    
}

function initQueryStringCheck(){
    // on startup check for url query parameters and use extent and optionally featureID
    // executeQueryTask will be called if there is a featureID so the app behaves just like a mouse click
    var url = esri.urlToObject(window.location.toString());
    if (url.query && url.query !== null) {
        featureID = url.query.featureID;
        mapName = url.query.mapName;
        var bounds = url.query.extent.split(",");
        var xmin, ymin, xmax, ymax;
        xmin = parseFloat(bounds[0]);
        ymin = parseFloat(bounds[1]);
        xmax = parseFloat(bounds[2]);
        ymax = parseFloat(bounds[3]);
        //alert("b " + xmin + "," + ymin + "," + xmax + "," + ymax);
        startExtent = new esri.geometry.Extent(xmin, ymin, xmax, ymax, spRef);
    }
}

function getFieldValue(graphic, fieldName){
    // return the value of a named field
    var res = esri.substitute(graphic.attributes, "${" + fieldName + "}");
    return (res);
}

function getMapName(){
    // get the short name of the map service using the url for mapName
    var strArray = mapName.url.split("/");
    if (strArray.length >= 2) {
        return (strArray[strArray.length - 2]);
    }
    else {
        return (null);
    }
}

function changeMap(layers, bmImage){
    //Disable thumb
    if (bmImage) {
        dojo.query(".bmThumb.active").removeClass("active");
        
        dojo.query(".bmThumb." + bmImage).addClass("active");
    }
    
    // show the map for the layers provided in the layers array
    hideImageTiledLayers(layers);
    var hideFlag = false;
    
    for (var i = 0; i < layers.length; i++) {
        layers[i].show();
        //Check to see if we're zoomed in further than the min zoom level for this layer.  If so, zoom back out to the min.
        if (map.__LOD && layers[i].scales) 
        {
            var minLevel;
            if (layers[i].id == "esriAerial") 
            {
                if (map.__LOD.scale < layers[i].scales[layers[i].scales.length - 2]) 
                {
                    minLevel = findMinZoomLevelFromScale(layers[i].scales[layers[i].scales.length - 2]); //Esri Aerial needs to use the 2nd to the last level for its min.
                }
            }
            else if (layers[i].id == "esriTopo") 
            {
                if (map.__LOD.scale < layers[i].scales[layers[i].scales.length - 4]) 
                {
                  minLevel = findMinZoomLevelFromScale(layers[i].scales[layers[i].scales.length - 4]); //Esri Topo needs to use the 4th to the last level for its min.
                }
            }
            else if (layers[i].id == "Base Layers" || layers[i].id == "shadedReliefMap")
            {
                //JW - This was put in to handle the "County Basemap" not zooming way out. Might be able to be removed once this map svc is cached.
            }
            else if (layers[i].id == "TNImageryMap")
            {               
                if (map.__LOD.scale < layers[i].scales[layers[i].scales.length - 2]) 
                {
                    minLevel = findMinZoomLevelFromScale(layers[i].scales[layers[i].scales.length - 2]); //TNMap Aerial needs to use the 2nd to the last level for its min.
                }
            }       
            else
            {
                if (map.__LOD.scale < layers[i].scales[layers[i].scales.length - 1]) 
                {
                    minLevel = findMinZoomLevelFromScale(layers[i].scales[layers[i].scales.length - 1]);
                }
            }       
        
            if (minLevel) {
                map.setLevel(minLevel);
                ///alert("new min set");
            }
        }
        mapName = layers[i];
        if (dojo.indexOf(_roadMaps, layers[i]) > -1) {
            hideFlag = true;
        }
    }
    
    if (hideFlag === true) {
        addressLayer.hide();
    }
    else {
        addressLayer.show();
    }
    
    //For gmaps, resize and reposition.
    gMap._resizeHandler();
}

function hideImageTiledLayers(layers){
    // hide the layers array layers provided
    for (var j = 0, jl = map.layerIds.length; j < jl; j++) {
        var layer = map.getLayer(map.layerIds[j]);
        if (dojo.indexOf(_basemaps, layer) > -1) {
            layer.hide();
            
        }
    }
}

