﻿//Handle map themes and layers

//variable to handle when we are in test mode vs. production
//this is important because we need to load services from
//different ArcGIS servers.
var g_bTestMode = false;

//base URL for all map services
var baseMapServiceURL;
if (g_bTestMode == true) {
    baseMapServiceURL = "http://test.kgis.org/ArcGIS/rest/services/";
} else {
    baseMapServiceURL = "http://new.kgis.net/ArcGIS/rest/services/";
}

//this will track the active theme
//should be set to the default application theme
var g_activeTheme = "basemap";

//this is the map service to be used for setting the slider bar labels
var scaleBase = "basemap";

//load themes based on test mode
var mapLayers = new Array();
if (g_bTestMode == true) {
    mapLayers = [
                 {'displayName':'Basemap',
                  'mapID':'basemap',  
                  'mapService':'Maps_Cache/Basemap_cache_png8/MapServer',
                  'mapType':'tiled',
                  'defaultDisplay':'false'},
                 {'displayName':'Aerial 2008',
                  'mapID':'aerial2008',  
                  'mapService':'Maps_Cache/AerialPhoto_cache_png8/MapServer',
                  'mapType':'tiled',
                  'defaultDisplay':'false'},
                 {'displayName':'Address Roads Parcels',
                  'mapID':'parcels',
                  'mapService':'MSD/AddressRoadsParcels_msd/MapServer',
                  'mapType':'dynamic',
                  'defaultDisplay':'false'}
                ];    
} else {
    mapLayers = [
                 {'displayName':'Basemap',
                  'mapID':'basemap',  
                  'mapService':'Maps_Cached/Basemap_cache_png8/MapServer',
                  'mapType':'tiled',
                  'defaultDisplay': 'false'},
                  
                  {'displayName':'County Cache',
                  'mapID':'countycache',  
                  'mapService':'Maps_Cached/CountyBnd_Cache_png8/MapServer',
                  'mapType':'tiled',
                  'defaultDisplay':'false'},

                 {'displayName':'FEMA',
                  'mapID':'fema',  
                  'mapService':'Maps_Cached/FEMA_Flood_Cache/MapServer',
                  'mapType':'tiled',
                  'defaultDisplay':'false'},

                   { 'displayName': 'Aerial 2010',
                       'mapID': 'aerial2010',
                       'mapService': 'Maps_Cached/AerialPhoto2010_Cache/MapServer',
                       'mapType': 'tiled',
                       'defaultDisplay': 'false'},

                 {'displayName':'Aerial 2009',
                  'mapID':'aerial2009',  
                  'mapService':'Maps_Cached/AerialPhoto2009_Cache/MapServer',
                  'mapType':'tiled',
                  'defaultDisplay':'false'},             

                 {'displayName':'Aerial 2008',
                  'mapID':'aerial2008',  
                  'mapService':'Maps_Cached/AerialPhoto2008_Cache/MapServer',
                  'mapType':'tiled',
                  'defaultDisplay':'false'},

                 {'displayName':'Aerial 2007',
                  'mapID':'aerial2007',  
                  'mapService':'Maps_Cached/AerialPhoto2007_Cache/MapServer',
                  'mapType':'tiled',
                  'defaultDisplay': 'false' },


                 { 'displayName': 'MPC Hilltop Protection Areas',
                  'mapID': 'MPChilltop',
                  'mapService': 'Maps_Cached/MPC_HRPA_Plan/MapServer',
                  'mapType': 'tiled',
                  'defaultDisplay': 'false' },
                  
                  {'displayName':'TDC Business Parks Base Cache',
                  'mapID':'businessparksbasecache',  
                  'mapService':'Maps_Cached/TDC_BusinessPark_Cache/MapServer',
                  'mapType':'tiled',
                  'defaultDisplay':'false'},
                  
                  {'displayName':'TDC Business Parks',
                  'mapID':'businessparks',  
                  'mapService':'MSD/TDC_BusinessParks_dynamic_msd/MapServer',
                  'mapType':'dynamic',
                  'defaultDisplay': 'false'},
                  
                  { 'displayName': 'TCA Restricted Zones',
                       'mapID': 'restrictedzones',
                       'mapService': 'MSD/Restriction_Zones_msd/MapServer',
                       'mapType': 'dynamic',
                       'defaultDisplay': 'false'},

                 {'displayName':'Shaded Relief 2003',
                  'mapID':'shadedRelief2003',  
                  'mapService':'Maps_Cached/ShadedRelief2003_Cache/MapServer',
                  'mapType':'tiled',
                  'defaultDisplay':'false'}, 

		{'displayName':'FEMA_labels',
                  'mapID':'femalabels',  
                  'mapService':'MSD/FEMA_Flood_Final_Labels_msd/MapServer',
                  'mapType':'dynamic',
                  'defaultDisplay':'false'},
                 

                  //Address Roads Parcels map service for Aerial Photos
                  {'displayName':'ParcelsAerial',
                  'mapID':'parcelsforAerial',
                  'mapService':'MSD/AddressRoadsParcels_Aerial_msd/MapServer',
                  'mapType':'dynamic',
                  'defaultDisplay':'false'},

                 {'displayName':'Topography',
                  'mapID':'topo',  
                  'mapService':'Maps_Cached/Topo_20ft_jpg/MapServer',
                  'mapType':'tiled',
                  'defaultDisplay':'false'},

//***********************************
//
//  Use this Parcels layer by default
//
//***********************************
                 {'displayName':'Address Roads Parcels',
                  'mapID':'parcels',
                  'mapService':'MSD/AddressRoadsParcels_msd/MapServer',
                  'mapType':'dynamic',
                  'defaultDisplay':'false'},

//***********************************
//
//  Live Parcels are used only when the cache building process fails
//
//***********************************
//                 {'displayName':'Address Roads Parcels',
//                  'mapID':'parcels',
//                  'mapService':'Maps/AddressRoadsParcels_AllLabels/MapServer',
//                  'mapType':'dynamic',
//                  'defaultDisplay':'true'},

                 {'displayName':'Zoning Base',
                  'mapID':'zoningBase',  
                  'mapService':'Maps_Cached/Zoning_Base_Cache_png8/MapServer',
                  'mapType':'tiled',
                  'defaultDisplay':'false'},      

                 {'displayName':'ZoningAddress',
                  'mapID':'zoningAddresses',  
                  'mapService':'MSD/AddressRoads_Zoning_msd/MapServer',
                  'mapType':'dynamic',
                  'defaultDisplay':'false'},

                 {'displayName':'ZoneLabels',
                  'mapID':'zoningLabels',  
                  'mapService':'Maps_MPC/ZoningLabels/MapServer',
                  'mapType':'dynamic',
                  'defaultDisplay':'false'}
            ];
}

//map themes array - load based on test mode
//   IMPORTANT NOTE - values in mapsOn must match mapIDs in mapLayers 
var themes = new Array();
if (g_bTestMode == true) {
    themes = [{'displayName':'Aerial Photography 2008',
               'themeID':'aerial2008',
               'mapsOn':'aerial2008, parcels'},
              {'displayName':'Basemap',
               'themeID':'basemap',
               'mapsOn':'basemap, parcels'}
             ];
} else {
    themes = [{'displayName':'Aerial Photography 2007',
               'themeID':'aerial2007',
               'mapsOn':'aerial2007, parcelsforAerial'},

              {'displayName':'Aerial Photography 2008',
               'themeID':'aerial2008',
               'mapsOn':'aerial2008, parcelsforAerial'},

              {'displayName':'Aerial Photography 2009',
               'themeID':'aerial2009',
               'mapsOn':'aerial2009, parcelsforAerial'},

               { 'displayName': 'Aerial Photography 2010',
                   'themeID': 'aerial2010',
                   'mapsOn': 'aerial2010, parcelsforAerial'},

              {'displayName':'Basemap',
               'themeID':'basemap',
               'mapsOn':'basemap,parcels'},

              {'displayName':'FEMA Flood Map',
               'themeID':'fema',
               'mapsOn': 'fema,femalabels'},

                 { 'displayName': 'Hillside-Ridgetop Protection Area',
                    'themeID': 'MPChilltop',
                    'mapsOn': 'MPChilltop'},

              { 'displayName': 'TCA Restriction Zones',
                  'themeID': 'restrictedzones',
                  'mapsOn': 'countycache, restrictedzones'},

              {'displayName':'TDC Business Parks',
               'themeID':'businessparks', 
               'mapsOn':'businessparksbasecache, businessparks'},

              {'displayName':'Topography',
               'themeID':'topography', 
               'mapsOn':'topo, parcels, shadedRelief2003'},

               {'displayName':'Zoning',
               'themeID':'zoningBase', 
               'mapsOn':'zoningBase,zoningAddresses'}
             ];
}


function createMapLayers() {
    //verify the browser is IE - this is the only fully supported browser
    browserCheck();
    
    for (i=0, lCount=mapLayers.length; i<lCount; i++) {
        var thisLayer = mapLayers[i];
        var layer;
        
        //create the layer based on mapType - dynamic or tiled
        if (thisLayer.mapType == 'dynamic') {
            layer = new esri.layers.ArcGISDynamicMapServiceLayer(baseMapServiceURL + thisLayer.mapService, {id:thisLayer.mapID, useMapImage:true});
        } else {
            //debugger;
            layer = new esri.layers.ArcGISTiledMapServiceLayer(baseMapServiceURL + thisLayer.mapService, {id:thisLayer.mapID});
        }
        
        //set the maps visibility
        if (thisLayer.defaultDisplay == 'true') {
            layer.visible = true;
        } else {
            layer.visible = false;
        }
        
        //add this layer to the map
        map.addLayer(layer);
    }
}

function loadMapTheme() {
    var combo = dojo.byId("selTheme");
    var themeOn = combo.options[combo.selectedIndex].value;
    toggleTheme(themeOn);
}

function toggleTheme(themeOn) {
    //first hide all the layers
    for (i=0, lCount=mapLayers.length; i<lCount; i++) {
        var thisLayer = mapLayers[i];
        var layerID = thisLayer.mapID;        
        var layer = map.getLayer(layerID);
        if (layer!=undefined)
            layer.hide();
    }

    /*********************************
    /   TODO: adjust layers to the order specified in the
    /         mapsOn attribute.
    /********************************/    


    //find the incoming theme
    for (i=0, lCount=themes.length; i<lCount; i++) {
        var thisTheme = themes[i];
        if (thisTheme.themeID == themeOn) {
            //set the active theme
            activeTheme = themeOn;
            //found the correct theme now show the layers
            var myLayers = thisTheme.mapsOn.split(",");
            for (n=0; n < myLayers.length; n++) {
                //alert("turnOn: " + dojo.trim(myLayers[n]) + "|");
                var layer = map.getLayer(dojo.trim(myLayers[n]));
                if (typeof(layer != "undefined")) {
                    if (layer != null) {
                        layer.show();                    
                    }
                }
            }
        }
    }

 }

function toggleTheme2(themeOn) {
    //first hide all the layers
    if (map.layerIds.length > 1)
        map.removeAllLayers();


    /*********************************
    /   TODO: adjust layers to the order specified in the
    /         mapsOn attribute.
    /********************************/    


    //find the incoming theme
    for (i=0, lCount=themes.length; i<lCount; i++) {
        var thisTheme = themes[i];
        if (thisTheme.themeID == themeOn) {
            //set the active theme
            activeTheme = themeOn;            
            //found the correct theme now show the layers
            var myLayers = thisTheme.mapsOn.split(",");

            //loop though the layers that need to be displayed and add them in the defined order
            for (n=0; n < myLayers.length; n++) {
                var layerOn = myLayers[n];
                
                //loop through all the specified layers for this app and find the one to add
                for (i=0, lCount=mapLayers.length; i<lCount; i++) {
                    var thisLayer = mapLayers[i];
                    if (thisLayer.mapID == layerOn) {
                        //found the layer so add it

                        //create the layer based on mapType - dynamic or tiled
                        if (thisLayer.mapType == 'dynamic') {
                            layer = new esri.layers.ArcGISDynamicMapServiceLayer(baseMapServiceURL + thisLayer.mapService, {id:thisLayer.mapID});    
                        } else {
                            layer = new esri.layers.ArcGISTiledMapServiceLayer(baseMapServiceURL + thisLayer.mapService, {id:thisLayer.mapID});
                        }
                        
                        //set the maps visibility
                        if (thisLayer.defaultDisplay == 'true') {
                            layer.visible = true;
                        } else {
                            layer.visible = false;
                        }
                        
                        //add this layer to the map
                        map.addLayer(layer);                                            
                    }
                }
            }
        }
    }
 }


function toggleButton(buttonID) {
    var item = dijit.byId("themeMenu");
    var items = item.getDescendants();
    //debugger;
    for (i=0; i<items.length; i++) {
        var thisItem = items[i];
        if (thisItem.id==buttonID) {
            thisItem.attr("checked", true);        
        } else {
            thisItem.attr("checked", false);
        }
    }
}

function loadThemes() {
    //get the combo box
    var combo = dojo.byId("selTheme");

    //loop through the list of themes and add them to the combo box    
    for (i=0, lCount=themes.length; i<lCount; i++) {
        //get the next theme
        var thisTheme = themes[i];

        //create the option element
        var option = document.createElement("option");   
        option.text = thisTheme.displayName;
        option.value = thisTheme.themeID;
        
        //if this is the default theme then mark it as the selected value
        if (thisTheme.themeID == g_activeTheme)
            option.selected = true;
        
        try {
            combo.add(option,null);
        } catch(ex) {
            combo.add(option);
        }
    }
}


