// 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(8);    // let's give the system a clue and dimension an array with an exact number of layers.
  var activeGroup = "";

  // The default group 
  var groupVisible = true;
  var groupName = "Default Map";
  var groupLayers = new Array();
  var groupLayerInfo = new Array();
  groupLayers[0] = "Road Centerlines";
  groupLayers[1] = "Streams and Brooks";
  groupLayers[2] = "Bridges";
  groupLayers[3] = "Buildings and Structures";    
  groupLayers[4] = "Pools";  
  groupLayers[5] = "Driveways";
  groupLayers[6] = "Roads";
  groupLayers[7] = "Parcels Clear";
  groupLayers[8] = "Parcels";
  groupLayers[9] = "Town Boundary";
  groupLayers[10] = "Boundaries";
  groupLayers[11] = "Towns";
  groupLayers[12] = "Major Roads";
  groupLayers[13] = "Parcels Buffer";
  groupLayers[14] = "";
  groupLayerInfo[0] = groupVisible;
  groupLayerInfo[1] = groupName;
  groupLayerInfo[2] = groupLayers;
  groupManager[0] = groupLayerInfo;


  // Now set up each of the group arrays
   
  groupVisible = false;
  groupName = "Parcel Map";
  groupLayers = new Array();
  groupLayerInfo = new Array();
  groupLayers[0] = "Road Centerlines";
  groupLayers[1] = "Parcels";
  groupLayers[2] = "Parcels Clear";
  groupLayers[3] = "Buildings and Structures";    
  groupLayers[4] = "Town Boundary";  
  groupLayers[5] = "Hydrography"; 
  groupLayers[6] = "Streams and Brooks";
  groupLayers[7] = "Pools";
  groupLayers[8] = "Boundaries";
  groupLayers[9] = "Towns";
  groupLayers[10] = "Major Roads"; 
  groupLayers[11] = "Parcels Buffer";
  groupLayerInfo[0] = groupVisible;
  groupLayerInfo[1] = groupName;
  groupLayerInfo[2] = groupLayers;
  groupManager[1] = groupLayerInfo;
  
  
    groupVisible = false;
  groupName = "Aerial Photo Map";
  groupLayers = new Array();
  groupLayerInfo = new Array();
  groupLayers[0] = "Road Centerlines";
  groupLayers[1] = "Parcels Ortho";
  groupLayers[2] = "Parcels Buffer";
  groupLayers[3] = "Parcels Clear";
  groupLayers[4] = "Town Boundary";  
  groupLayers[5] = "Orthos 2000";						
  groupLayerInfo[0] = groupVisible;
  groupLayerInfo[1] = groupName;
  groupLayerInfo[2] = groupLayers;
  groupManager[2] = groupLayerInfo; 
  
  

      groupVisible = false;
  groupName = "Aerial Photo (w/Contours)";
  groupLayers = new Array();
  groupLayerInfo = new Array();
  groupLayers[0] = "Road Centerlines";
  groupLayers[1] = "Parcels Ortho";
  groupLayers[2] = "";
  groupLayers[3] = "Parcels Clear";
  groupLayers[4] = "Town Boundary";  
  groupLayers[5] = "Orthos 2000";
  groupLayers[6] = "Contours - 2 foot Ortho";
  groupLayers[7] = "Parcels Buffer";						
  groupLayerInfo[0] = groupVisible;
  groupLayerInfo[1] = groupName;
  groupLayerInfo[2] = groupLayers;
  groupManager[3] = groupLayerInfo; 
 
  
  
    groupVisible = false;
  groupName = "Base Map";
  groupLayers = new Array();
  groupLayerInfo = new Array();
  groupLayers[0] = "Road Centerlines";
  groupLayers[1] = "Pools";
  groupLayers[2] = "Parcels";
  groupLayers[3] = "Parcels Clear";
  groupLayers[4] = "Town Boundary";  
  groupLayers[5] = "Roads";
  groupLayers[6] = "Bridges";
  groupLayers[7] = "Hydrography";
  groupLayers[8] = "Streams and Brooks";
  groupLayers[9] = "Buildings and Structures";
  groupLayers[10] = "Driveways";
  groupLayers[11] = "Parking Areas";
  groupLayers[12] = "Sidewalks & Walkways";
  groupLayers[13] = "Vegetated Areas";
  groupLayers[14] = "Fence";
  groupLayers[15] = "Street Signs & Traffic Lights";
  groupLayers[16] = "Utility and Light Poles";
  groupLayers[17] = "Manholes and Catch Basins";
  groupLayers[18] = "Hydrants";
  groupLayers[19] = "Water Gates";
  groupLayers[20] = "Boundaries";
  groupLayers[21] = "Towns";
  groupLayers[22] = "Major Roads";
  groupLayers[23] = "Parcels Buffer";  						
  groupLayerInfo[0] = groupVisible;
  groupLayerInfo[1] = groupName;
  groupLayerInfo[2] = groupLayers;
  groupManager[4] = groupLayerInfo;    
  
      groupVisible = false;
  groupName = "Zoning Map";
  groupLayers = new Array();
  groupLayerInfo = new Array();
  groupLayers[0] = "Road Centerlines";
  groupLayers[1] = "Hydrography";
  groupLayers[2] = "Parcels";
  groupLayers[3] = "Parcels Clear";
  groupLayers[4] = "Town Boundary";  
  groupLayers[5] = "Buildings and Structures";
  groupLayers[6] = "Streams and Brooks";
  groupLayers[7] = "Pools";
  groupLayers[8] = "Zoning Districts";
  groupLayers[9] = "Boundaries";
  groupLayers[10] = "Towns";
  groupLayers[11] = "Major Roads";
  groupLayers[12] = "Parcels Buffer";
  groupLayerInfo[0] = groupVisible;
  groupLayerInfo[1] = groupName;
  groupLayerInfo[2] = groupLayers;
  groupManager[5] = groupLayerInfo;
  
   groupVisible = false;
  groupName = "Voter Precinct Map";
  groupLayers = new Array();
  groupLayerInfo = new Array();
  groupLayers[0] = "Road Centerlines";
  groupLayers[1] = "Parcels";
  groupLayers[2] = "Parcels Clear";
  groupLayers[3] = "Roads";    
  groupLayers[4] = "Town Boundary";  
  groupLayers[5] = "Hydrography"; 
  groupLayers[6] = "Buildings and Structures";
  groupLayers[7] = "Pools";
  groupLayers[8] = "Driveways";
  groupLayers[9] = "Parking Areas";
  groupLayers[10] = "Sidewalks & Walkways";
  groupLayers[11] = "Voter Precincts";
  groupLayers[12] = "Boundaries";
  groupLayers[13] = "Towns";
  groupLayers[14] = "Major Roads";
  groupLayers[15] = "Parcels Buffer";
  groupLayerInfo[0] = groupVisible;
  groupLayerInfo[1] = groupName;
  groupLayerInfo[2] = groupLayers;
  groupManager[6] = groupLayerInfo;
  

  
       groupVisible = false;
  groupName = "Contour Map";
  groupLayers = new Array();
  groupLayerInfo = new Array();
  groupLayers[0] = "Road Centerlines";
  groupLayers[1] = "Parcels";
  groupLayers[2] = "Parcels Clear";
  groupLayers[3] = "";    
  groupLayers[4] = "Town Boundary";
  groupLayers[5] = "Contours - 2 foot"; 
  groupLayers[6] = "Roads";
  groupLayers[7] = "Bridges";
  groupLayers[8] = "Hydrography";
  groupLayers[9] = "Streams and Brooks";
  groupLayers[10] = "Buildings and Structures";
  groupLayers[11] = "Pools";
  groupLayers[12] = "Driveways";
  groupLayers[13] = "Parking Areas";
  groupLayers[14] = "Sidewalks & Walkways";
  groupLayers[15] = "Parcels Buffer";
  groupLayers[16] = "";
  groupLayerInfo[0] = groupVisible;
  groupLayerInfo[1] = groupName;
  groupLayerInfo[2] = groupLayers;
  groupManager[7] = 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();
}

//*  MB:  This function is used if the setLayers was called from an autogenerated 
//        combobox
 function setLayers (formListName) {
     activeGroup = eval("document." + formListName + ".value");
	// alert (activeGroup);
     if (activeGroup!="Default") {
        updateGroupLayers();
     }
 }

//*  MB:  This function is used if the setLayers was called from an image link button
// function setLayers (layerName) {
//     
//     // alert (layerName)
//	 activeGroup = (layerName);
//	 
//	 // alert (activeGroup)
//	 	 
//     if (activeGroup!="Default") {
//        updateGroupLayers();
//     }
// }

