// 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...";
  var groupLayers = new Array();
  var groupLayerInfo = new Array();
  groupLayers[0] = "Roads";
  groupLayers[1] = "Townline";
  groupLayers[2] = "Water Bodies";
  groupLayers[3] = "Road Centerline";
  groupLayers[4] = "Trails";
  groupLayers[5] = "Rivers and Streams";    
  groupLayers[6] = "Selected 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] = "Neighboring Towns";
  groupLayers[1] = "Kittery Water Bodies";
  groupLayers[2] = "Road Centerlines";
  groupLayers[3] = "Parcels - Clear";
  groupLayers[4] = "Parcel IDs";
  groupLayers[5] = "Selected Parcel";
  groupLayerInfo[0] = groupVisible;
  groupLayerInfo[1] = groupName;
  groupLayerInfo[2] = groupLayers;
  groupManager[1] = groupLayerInfo;
  

  groupVisible = false;
  groupName = "Base Mapping";
  groupLayers = new Array();
  groupLayerInfo = new Array();
  groupLayers[0] = "Parcels - Clear";               
  groupLayers[1] = "Road Centerlines";              
  groupLayers[2] = "Railroads";                 
  groupLayers[3] = "Parcels - Clear";               
  groupLayers[4] = "Kittery Water Bodies";          
  groupLayers[5] = "Streams";                 
  groupLayers[6] = "Selected Parcel";               
  groupLayers[7] = "Neighboring Towns";             
  groupLayerInfo[0] = groupVisible;                 
  groupLayerInfo[1] = groupName;                 
  groupLayerInfo[2] = groupLayers;                 
  groupManager[2] = 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 = "Zoning Mapping";
  groupLayers = new Array();
  groupLayerInfo = new Array();
  groupLayers[0] = "Parcels - Clear";
  groupLayers[1] = "Zoning";
  groupLayers[2] = "Roads";
  groupLayers[3] = "Townline";
  groupLayers[4] = "Road Centerlines";
  groupLayers[5] = "Kittery Water Bodies";
  groupLayers[6] = "Selected Parcel";
  groupLayers[7] = "Neighboring Towns";
  groupLayerInfo[0] = groupVisible;
  groupLayerInfo[1] = groupName;
  groupLayerInfo[2] = groupLayers;
  groupManager[3] = groupLayerInfo; 

  //   Group 10 - 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 = "Flood Plain Mapping";
  groupLayers = new Array();
  groupLayerInfo = new Array();
  groupLayers[0] = "Neighboring Towns";
  groupLayers[1] = "Parcels - Clear";
  groupLayers[2] = "Parcel IDs";
  groupLayers[3] = "Flood Zones";
  groupLayers[4] = "Road Centerlines";
  groupLayers[5] = "Kittery Water Bodies";
  groupLayers[6] = "Streams";
  groupLayers[7] = "Selected Parcel";
  groupLayerInfo[0] = groupVisible;
  groupLayerInfo[1] = groupName;
  groupLayerInfo[2] = groupLayers;
  groupManager[4] = groupLayerInfo;
  
    //   Group 11 - 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 = "Aerial Photography";
  groupLayers = new Array();
  groupLayerInfo = new Array();
  groupLayers[0] = "Parcels - Ortho";
  groupLayers[1] = "Road Centerlines";
  groupLayers[2] = "Neighboring Towns";
  groupLayers[3] = "Air Photo";
  groupLayers[4] = "Isle of Shoals Air Photo";
  groupLayers[5] = "Selected Parcel";
  groupLayerInfo[0] = groupVisible;
  groupLayerInfo[1] = groupName;
  groupLayerInfo[2] = groupLayers;
  groupManager[5] = 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 = false;
  //groupName = "*Quick Map Test*";
  //groupLayers = new Array();
 //groupLayerInfo = new Array();
  //groupLayers[0] = "1";
  //groupLayers[1] = "2";
  //groupLayers[2] = "3";
  //groupLayers[3] = "4";
  //groupLayers[4] = "Structure";
 // groupLayers[5] = "Parcels - Ortho";
  //groupLayers[6] = "Aerial Photography - 2002";
  //groupLayers[7] = "Selected Parcel";
  //groupLayerInfo[0] = groupVisible;
  //groupLayerInfo[1] = groupName;
  //groupLayerInfo[2] = groupLayers;
  //groupManager[10] = 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();
     }
 }
