// Following structures support the grouping of Layers.  This should be located
// in the ArcIMSparams.js, but becuase the header loads earlier, it is all located 
// in the script that supports only the group layering.

//group layers (for one set only) list the layers to group together, each being
//a list in the array. groupName is the name to give the group the TOC.

// A reworking of the standard grouplayers implementation to support multiple
// groups of layers (as opposed to a single group).  Simple an array of arrays.
// A little more difficult to work with, but gives greater flexibility.  CAT.
// Structure - there is a top level array, groupManager that has as many entries
//    as there are groups.
//    Then there is a groupLayerInfo array for each of the Groups, including the name, whether it is visible, and what its layers are
//    Finally there is a groupLayers array for each of the layers in a Group.

  // The first 'group' in the array is simply 'default' which lists all the standard layers
  // as outlined in the site AXL file.
  
  var groupManager = new Array(5);    // let's give the system a clue and dimension an array with an exact number of layers.
  var activeGroup = "";

  // The default group - has no real information in it.
  var groupVisible = true;
  var groupName = "Select a Quick Map";
  var groupLayers = new Array();
  var groupLayerInfo = new Array();
  groupLayers[0] = "Roads";
  groupLayers[1] = "City Border";
  groupLayers[2] = "Water Bodies0";
  groupLayers[3] = "Road Centerlines";
  groupLayers[4] = "Route 89";
  groupLayers[5] = "Rivers and Streams";    
  groupLayers[6] = "Highlighted Parcel";
  groupLayerInfo[0] = groupVisible;
  groupLayerInfo[1] = groupName;
  groupLayerInfo[2] = groupLayers;
  groupManager[0] = groupLayerInfo;

  // Now set up each of the 3 group arrays
  //   Group 1 - ???  
  groupVisible = false;
  groupName = "Parcel Mapping";
  groupLayers = new Array();
  groupLayerInfo = new Array();
  groupLayers[0] = "Lowell Boundary";
  groupLayers[1] = "Parcels";
  groupLayers[2] = "Parcel Id";
  groupLayers[3] = "Parcel Text";
  groupLayers[4] = "Dimensions";
  groupLayers[5] = "Buildings";
  groupLayers[6] = "Street Names";  
  groupLayers[7] = "Roadways";
  groupLayers[8] = "Parcels";
  groupLayers[9] = "Water Bodies";
  groupLayers[10] = "Street Centerline";
  groupLayers[11] = "Vegetation";
  groupLayers[12] = "City Display";
  groupLayers[13] = "Selected Parcel";
  groupLayerInfo[0] = groupVisible;
  groupLayerInfo[1] = groupName;
  groupLayerInfo[2] = groupLayers;
  groupManager[1] = groupLayerInfo;

  groupVisible = false;
  groupName = "Base Features";
  groupLayers = new Array();
  groupLayerInfo = new Array();
  groupLayers[0] = "Lowell Boundary";
  groupLayers[1] = "Parcels";
  groupLayers[2] = "Parcels (Buffer)";
  groupLayers[3] = "Addresses";
  groupLayers[4] = "Buildings";
  groupLayers[5] = "Street Names";  
  groupLayers[6] = "Roadways";
  groupLayers[7] = "Major Streets";
  groupLayers[8] = "Railroads";
  groupLayers[9] = "Waterways";
  groupLayers[10] = "Water Bodies";
  groupLayers[11] = "Street Centerline";
  groupLayers[12] = "Selected Parcel";
  groupLayers[13] = "City Display";
  groupLayerInfo[0] = groupVisible;
  groupLayerInfo[1] = groupName;
  groupLayerInfo[2] = groupLayers;
  groupManager[2] = groupLayerInfo;
  

  groupVisible = false;
  groupName = "Zoning Districts";
  groupLayers = new Array();
  groupLayerInfo = new Array();
  groupLayers[0] = "Lowell Boundary";
  groupLayers[1] = "Parcels";
  groupLayers[2] = "Zoning Boundary";
  groupLayers[3] = "Addresses";
  groupLayers[4] = "Buildings";
  groupLayers[5] = "Street Names";  
  groupLayers[6] = "Roadways";
  groupLayers[7] = "Water Bodies";
  groupLayers[8] = "Street Centerline";
  groupLayers[9] = "Zoning";
  groupLayers[10] = "Selected Parcel";
  groupLayerInfo[0] = groupVisible;
  groupLayerInfo[1] = groupName;
  groupLayerInfo[2] = groupLayers;
  groupManager[3] = groupLayerInfo;
  
  //   Group 2 - Street Map - All street information
  // nb - after the first group, do not use VAR to declare variables, they are only declared once.
  groupVisible = true;
  groupName = "Election Wards and Precincts";
  groupLayers = new Array();
  groupLayerInfo = new Array();
  groupLayers[0] = "Lowell Boundary";
  groupLayers[1] = "Parcels";
  groupLayers[2] = "Addresses";
  groupLayers[3] = "Election Polling Locations";
  groupLayers[4] = "Buildings";
  groupLayers[5] = "Street Names";  
  groupLayers[6] = "Roadways";
  groupLayers[7] = "Street Centerline";
  groupLayers[8] = "Election Wards and Precincts";
  groupLayers[9] = "Selected Parcel";
  groupLayerInfo[0] = groupVisible;
  groupLayerInfo[1] = groupName;
  groupLayerInfo[2] = groupLayers;
  groupManager[4] = groupLayerInfo;

  
  
  
  //   Group 3 - Street Map - All street information
  // nb - after the first group, do not use VAR to declare variables, they are only declared once.
  groupVisible = true;
  groupName = "Hybrid Map";
  groupLayers = new Array();
  groupLayerInfo = new Array();
  groupLayers[0] = "Aerial Photo 2004";
  groupLayers[1] = "Lowell Boundary";
  groupLayers[2] = "Building Text";
  groupLayers[3] = "Parcels for Aerial Photo";  
  groupLayers[4] = "Major Streets";
  groupLayers[5] = "Street Names";
  groupLayers[6] = "Parcels";
  groupLayers[7] = "Parcels (Buffer)";
  groupLayers[8] = "Selected Parcel";
  groupLayerInfo[0] = groupVisible;
  groupLayerInfo[1] = groupName;
  groupLayerInfo[2] = groupLayers;
  groupManager[5] = groupLayerInfo;
  


    //   Group 5 - DPW Mapping - All street information
  // nb - after the first group, do not use VAR to declare variables, they are only declared once.
  groupVisible = true;
  groupName = "Trash Day Schedule";
  groupLayers = new Array();
  groupLayerInfo = new Array();
  groupLayers[0] = "Lowell Boundary";
  groupLayers[1] = "Parcels";
  groupLayers[2] = "Street Names";
  groupLayers[3] = "Addresses";
  groupLayers[4] = "Buildings";
  groupLayers[5] = "Trash Day Schedule";  
  groupLayers[6] = "Roadways";
  groupLayers[7] = "Water Bodies";
  groupLayers[8] = "Street Centerline";
  groupLayers[9] = "Vegetation";
  groupLayers[10] = "City Display";
  groupLayers[11] = "Selected Parcel";
  groupLayerInfo[0] = groupVisible;
  groupLayerInfo[1] = groupName;
  groupLayerInfo[2] = groupLayers;
  groupManager[6] = groupLayerInfo;
  
      //   Group 5 - DPW Mapping - All street information
  // nb - after the first group, do not use VAR to declare variables, they are only declared once.
  groupVisible = true;
  groupName = "Flood Plain Map";
  groupLayers = new Array();
  groupLayerInfo = new Array();
  groupLayers[0] = "Lowell Boundary";
  groupLayers[1] = "Parcels";
  groupLayers[2] = "Street Names";
  groupLayers[3] = "Flood Evacuation Routes";
  groupLayers[4] = "Buildings";
  groupLayers[5] = "Roadways";
  groupLayers[6] = "Lowell Wetlands";
  groupLayers[7] = "Water Bodies";
  groupLayers[8] = "FEMA Flood 100 Year";
  groupLayers[9] = "Street Centerline";
  groupLayers[10] = "Vegetation";
  groupLayers[11] = "City Display";
  groupLayers[12] = "Selected Parcel";
  groupLayerInfo[0] = groupVisible;
  groupLayerInfo[1] = groupName;
  groupLayerInfo[2] = groupLayers;
  groupManager[7] = groupLayerInfo;
  
  //   Group 3 - Street Map - All street information
  // nb - after the first group, do not use VAR to declare variables, they are only declared once.
  groupVisible = true;
  groupName = "Lowell Digital Terrain Model";
  groupLayers = new Array();
  groupLayerInfo = new Array();
  groupLayers[0] = "Lowell Boundary";
  groupLayers[1] = "Parcels";
  groupLayers[2] = "Addresses";
  groupLayers[3] = "Buildings";
  groupLayers[4] = "Street Names";  
  groupLayers[5] = "Roadways";
  groupLayers[6] = "Street Centerline";
  groupLayers[7] = "Border Towns";
  groupLayers[8] = "Lowell Tin Model";
  groupLayers[9] = "Selected Parcel";
  groupLayerInfo[0] = groupVisible;
  groupLayerInfo[1] = groupName;
  groupLayerInfo[2] = groupLayers;
  groupManager[8] = groupLayerInfo;


  //Group 4 - Digital Orthophotos - All street information
  // nb - after the first group, do not use VAR to declare variables, they are only declared once.
  groupVisible = true;
  groupName = "Parcels with Aerial Photos";
  groupLayers = new Array();
  groupLayerInfo = new Array();
  groupLayers[0] = "Aerial Photo 2004";
  groupLayers[1] = "Parcels";
  groupLayers[2] = "Street Names";
  groupLayers[3] = "Parcels for Aerial Photo";
  groupLayers[4] = "Lowell Boundary";
  groupLayers[5] = "Street Centerline";
  groupLayers[6] = "Selected Parcel";
  groupLayerInfo[0] = groupVisible;
  groupLayerInfo[1] = groupName;
  groupLayerInfo[2] = groupLayers;
  groupManager[9] = groupLayerInfo;
  
/*
  Function uses the services of the getGroup to determine if the
  named layer belongs to a group.
  Modified from original sample version to support multiple layers.
  
  @param glayerName The name of a layer in the layer list.
  @return True if the layer in question is a group layer.
  @see #getGroup
  @author Cord Thomas
  */
function checkLayer(glayerName) {
   var gName=getGroup(glayerName);
   if (gName.length==0)
      return false;
   else
      return true;
}

/*
  Function returns the group a specified layer belongs to.
  
  @param glayerName The name of a layer in question.
  @return The name of the group the layer belongs to.
  @author Cord Thomas
  */
function getGroup(glayerName) {
  var glInfo=null;
  var gLayers=null;
  var gName="";
  for (var gm=0;gm<groupManager.length;gm++) {
     glInfo = groupManager[gm];
     gLayers = glInfo[2];
     gName = glInfo[1];
     for (var gl=0;gl<gLayers.length;gl++) {
        if (glayerName == gLayers[gl]) {
           return gName;
           break
	}
     }
   }
   return "";   // This should be blank.
}

/* Returns whether a named layer is in a group. 
   @param layerName The name of hte layer in question.
   @param groupName The name of the group to search in.
*/ 
function layerInGroup(layerName, groupName) {
   var glInfo=null;
   var gLayers=null;
   var gName="";
   for (var gm=0;gm<groupManager.length;gm++) {
      glInfo = groupManager[gm];
      gName = glInfo[1];
      // If the group name is the same as the groupName then we are in the right group.
      if (gName == groupName) {
         gLayers = glInfo[2];
         for (var gl=0;gl<gLayers.length;gl++) {
            if (layerName == gLayers[gl]) {
               return true;
               break
            }
         }
      }
   }
  return false;   // This should be blank.
}

function setGroup(theVal) {
 var theName;
  if (theVal == 1) { 
    groupVisible = true
  } else { 
    groupVisible = false 
  }
  for(var ii=0;ii<layerCount;ii++) {
    theName = LayerName[ii];
    if (checkLayer(theName) == true) {
      LayerVisible[ii] = theVal;     
    }
  }
}

/* Loop through all the layers in the map, if the layer is in the
   currently active group, then it is set to visible, otherwise it is not. */
function updateGroupLayers() {
  var j = 0;
  var groupFromLayer="";
  
  for (var i=0;i<t.layerCount;i++) {
    if (layerInGroup(t.LayerName[i], activeGroup)) {
      // alert (t.LayerName[i] + '  is visible');
      t.LayerVisible[i] = 1;
    } else {
      t.LayerVisible[i] = 0;
    }
  }
  t.sendMapXML();
}

 function setLayers (formListName) {
     activeGroup = eval("document." + formListName + ".value");
     if (activeGroup!="Default") {
        updateGroupLayers();
     }
 }

