﻿var headers = new Array();

function initIdentifyArray(mapserver){
    // remove all elements first
    headers.splice(0,headers.length);
    if (mapserver.indexOf("landuse") != -1)
    {
//        headers[0]= new Array("Area of each landuse type", "[.]*acres[.]*", new Array());
//        headers[1]= new Array("Percent area of watershed for landuse", "[.]* (% area of watershed){1}$", new Array());
//        headers[2]= new Array("Percent area of peninsula for landuse", "[.]* (% area of peninsula){1}$", new Array());
//        headers[3]= new Array("Percent area of total landuse type for each landuse type", "[.]* (% total land use type){1}$", new Array());
        headers[0] = new Array("Area of each landuse type", "[^%]* (area){1}$", new Array());
        headers[1]= new Array("Percent area of watershed for landuse", "[.]* (% area of watershed){1}$", new Array());
        headers[2]= new Array("Percent area of peninsula for landuse", "[.]* (% area of peninsula){1}$", new Array());
        headers[3]= new Array("Percent area of total landuse type for each landuse type", "[.]* (% total landuse type){1}$", new Array());
    }
    else
    {
        // Habitat - do these first since they are the most restrictive
        headers[0]= new Array("Area of each habitat function type", "[^%]* (habitat area){1}$", new Array());
        headers[1]= new Array("Percent area of watershed for each habitat function type", "[.]* (stream|habitat){1} (% area of watershed){1}$", new Array());
        headers[2]= new Array("Percent area of peninsula for each habitat function type", "[.]* (stream|habitat){1} (% area of peninsula){1}$", new Array());
        headers[3]= new Array("Percent area of total habitat function type for each habitat function type", "[.]* (stream|habitat){1} (% total habitat type){1}$", new Array());
        // Ecosystem
        headers[4]= new Array("Area of each wetland ecosystem", "[^%]* (area){1}$", new Array());
        headers[5]= new Array("Percent area of watershed for ecosystem", "[.]* (% area of watershed){1}$", new Array());
        headers[6]= new Array("Percent area of peninsula for ecosystem", "[.]* (% area of peninsula){1}$", new Array());
        headers[7]= new Array("Percent area of total ecosystem type for each ecosystem type", "[.]* (% total landuse type){1}$", new Array());
    
    
    }

}
function GetIdentifyHTML(attributes)
{
    // Keep track of each attribute, they will be removed as they 
    // are used to avoid any duplicate matching
    var attrNames = [];
    for (var i in attributes) 
    {
        attrNames.push(i);
    }
    
    
    // This loop is where all the action happens for capturing the header groups,
    // we first loop on each header, then loop through the feature's attribute array  
    // and look for regular expression matches.  If a match is found, then that entry 
    // belongs to that header group.  
    
    // TODO: 1. Either all entries must be matchable, or we need to define a global "catch all" group.
    //       2. Once a header is matched, it should be removed from the searchable list of headers
    for (var i=0; i<headers.length; ++i)
    {
      //Create RegExp object for each header
      var regexp = new RegExp(headers[i][1]);
      // Loop on each attribute for this feature
      //for (var j in attributes) 
      for (var j=attrNames.length-1; j>0; --j)
      {
        var name = attrNames[j];
        // If match exists, then add it to the header array of matches
        if ( name.toString().match(regexp) )
        {
          headers[i][2][ headers[i][2].length] = name;
          // Remove the used attribute so it doesn't show up twice if
          // another RE happens to match.
          attrNames.splice(j, 1);
        }
      } 
    }
    
    // This loop gathers up all our results and places them into our XML structure
    // for a feature identify.  The one that I am doing is extremely simple and I would
    // expect there to be changes.  The structure is presented below:
    
    /*
      <Feature Name="Wetlands">
        <Heading Name="Area of each wetland ecosystem">
          <Attribute Name="Depression % area of watershed" Value="0%" />
        </Header>
      </Feature>
    */
    
    
    var xmlDoc = ""; //new ActiveXObject("Microsoft.XMLDOM"); 
    
    if(navigator.appName == "Microsoft Internet Explorer")
    {
        xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    }   
    else
    {
        xmlDoc=document.implementation.createDocument("","",null);
    }
    
    xmlDoc.loadXML("<?xml version='1.0' encoding='utf-8' ?><Feature Name=''></Feature>");
    var root = xmlDoc.documentElement;
    
    // loop on headers
    for (var i=0; i<headers.length; ++i)
    {
      // for each header we need to create a group node 
      var groupNode = xmlDoc.createNode(1, "Header", "");
      groupNode.setAttribute("Name", headers[i][0]);
      
      // loop on the group node's matched attributes and add them into 
      // the group as child nodes.
      for (var j=0; j<headers[i][2].length; ++j)
      {
        var attrNode = xmlDoc.createNode(1, "Attribute", "");
        attrNode.setAttribute("Name", headers[i][2][j]);
        attrNode.setAttribute("Value", attributes[headers[i][2][j]]);
        groupNode.appendChild(attrNode);
      }
      // append each group node to the root feature
      root.appendChild(groupNode);
    }
    
    // Grab the XSL document and load it
    var xslDoc = new ActiveXObject("Microsoft.XMLDOM"); 
    xslDoc.async = false;
    
    xslDoc.load("data/identify.xslt");
    
    // Here is the resulting HTML from the transformation
    // TODO: 1.  Take the above and refactor into a function (or series of functions)
    //       2.  This is only working off of the first result idResults[0], so this 
    //           would need to be updated for multiple results (tabs)
    var output = xmlDoc.transformNode(xslDoc);        
    return output;
}