// USGS_NATMAP_class.js

nmClassPresent = true;

/**
 * function and variable definitions for the classes used in the table of contents.
 * class is the middle tier of the display hierarchy of the table of contents
 * theme contains classes
 * class contains wmsLayers
 *
 * Based in part on aimsLayer.js by ESRI
 */

var classCount = 0;

// get list of layers, id/shape fields, scalefactors, etc.
function getThemesAndClasses(theReply) {

	var theReplyUC = theReply.toUpperCase();

	var startpos = 0;
	var endpos = 0;

	var pos   = -1;
	var lpos  = 1;
	var epos  = 1;
	var zpos  = 1;
	var zpos2 = 1;

	var tempString="";
	var visString = "";

	if (ClassID.length == 0) {

		classCount          = 0;
		ClassName.length    = 1;
		ClassVisible.length = 1;
		ClassID.length      = 1;
		ClassThemeName.length = 1;

		// Added to support dynamic loading of TOC layerlist
		var themeIdxArray = new Array();  // associative array to hold key-value pair
		                                  //  key: {themeName} 
																			//          => value: {theme index in ThemeName}
																			//  Note: same theme index is used for the
																			//  ThemeClassMap array.
		var themeCount = 0;   // used for index when adding to themesArray
		var themesList = "";	// pipe-separated list to store themeNames.
		                      // used for quick lookup to see if a theme has already
													// been added to the ThemeName list
                          
    var onClassNames = null;
    if (defaultOnClasses != undefined && defaultOnClasses != null && defaultOnClasses != "") {
      onClassNames = defaultOnClasses.split("|");
    }

		lpos = theReplyUC.indexOf("<LAYERINFO",zpos);
	
		while (lpos > -1) {

			// example LAYERINFO element:
			// <LAYERINFO id="11" maxscale="5.0" visible="true" type="TRANSPORTATION"
			//    name="CLASS 2 ROADS">

			// For Phase D viewer,
			//  LAYERINFO 'type' corresponds to 'theme'
			//  LAYERINFO 'name' corresponds to 'class'
			
			if (lpos != -1) {
				
				zpos = theReplyUC.indexOf("</LAYERINFO",lpos);

				if (zpos!=-1) {
					pos = theReplyUC.indexOf("NAME=",lpos);
					if (pos != -1) {

						startpos = pos + 6;
						endpos = theReply.indexOf(dQuote, startpos);

						tempString = theReply.substring(startpos,endpos);
						tempString = tempString.replace(/&apos;/g, "'");
						ClassName[classCount] = tempString;
						var className = tempString;
						var themeName = "";
						var classID   = "";

						// extract contents of TYPE attribute - gives us the themes
						startpos = theReplyUC.indexOf("TYPE=",lpos);
						if (startpos != -1) {
							startpos = startpos + 6;
							endpos = theReply.indexOf(dQuote, startpos);
							tempString = theReply.substring(startpos,endpos);
							tempString = tempString.replace(/&apos;/g, "'");
							themeName  = tempString;
						}

						// determine visibility
						// startpos = theReplyUC.indexOf("VISIBLE=",endpos);
						startpos = theReplyUC.indexOf("VISIBLE=",lpos);
						if (startpos != -1) {
							startpos = startpos + 9;
							endpos = startpos + 4;
							visString = theReply.substring(startpos,endpos);
						}

						startpos = theReplyUC.indexOf("ID=",lpos);
						if ((startpos != -1) && (startpos<zpos)) {
							startpos = startpos + 4;
							endpos = theReply.indexOf(dQuote, startpos);
							tempString = theReply.substring(startpos,endpos);
							tempString = tempString.replace(/&apos;/g, "'");
							ClassID[classCount] = tempString;
							classID = tempString;
						} 
						else {
							ClassID[classCount] = ClassName[classCount];
						}

						// Check theme list - have we already handled a class for
						// this theme? 
						if (themesList.indexOf(themeName) == -1) {

							// not found - add current theme to theme list
							themesList = themesList + themeName + "|";
							themeIdxArray[themeName] = themeCount.toString();
							ThemeName[themeCount]       = themeName;
							ThemeClassMap[themeCount] = classCount;
							themeCount++;
						}
						else { // theme exists, append classID to ThemeClassMap

							// lookup theme index for this theme
							themeIdx = themeIdxArray[themeName];
							ThemeClassMap[themeIdx] += "|" + classCount;
						}
						
						visString = "false";
						
            if (onClassNames != null) {
						  for (var k = 0; k < onClassNames.length; k++) {
							 if (className == onClassNames[k]) {
								  visString="true";
								  break;
							 }
						  }
            } else {
              for (var k = 0; k < defaultOnClassIds.length; k++) {
                if (classID == defaultOnClassIds[k]) {
                  visString="true";
                  break;
                }
              }
            }
						ClassVisible[classCount] = (visString == "true");
						ClassThemeName[classCount] = themeName;
						
						classCount += 1;
						endpos = zpos;
					}
					lpos = theReplyUC.indexOf("<LAYERINFO",zpos);
				} 
				else {
					lpos = -1;
				}
			}					
		}
	}

	// Collapse all categories
	for (var k = 0; k < ThemeName.length; k++) {
		ThemeExpanded[k] = false;
	}
}

function setDefaultLayers() {
	var theLayers = null;
	if (defaultOnLayers.length > 0) {
		theLayers = defaultOnLayers.split("|");
		for (var i = 0; i < theLayers.length; i++) {
			lp_setLayerPref(theLayers[i], true);
		}
	}
	if (defaultOffLayers.length > 0) {
		theLayers = defaultOffLayers.split("|");
		for (var i = 0; i < theLayers.length; i++) {
			lp_setLayerPref(theLayers[i], false);
		}
	}
	
}
