// CivXplorer Advanced Layer Grouping
// Suzanne Fliege
// Red Horse Geographics, LLC
// contains functions to supported grouped layers

//******************************************************************************
// master variables - don't change here
groupedlayersAvailable = true;
var groupedName = new Array();  // alias to display in layer menu
var groupedID = new Array();  // expects format to be "grouped-xxxxxxxx"
var groupedType = new Array();  // use "overlay" "orthoimg" "optional" (if using UserMgmt module, can also use "overlay-restricted", "orthoimg-restricted", "optional-restricted" and "subgroup-restricted")
var groupedIDs = new Array();  // list of layers by ID in AXL, or subgroup name(s)
var groupedVisible = new Array(); // whether group starts as visible (optional only!), if not specified, then false by default
var groupedExpand = new Array(); // whether group starts expanded in menu (optional only!), if not specified then false by default
//******************************************************************************
// SUBGROUPS are only allowed for OPTIONAL layers!!!  Not overlay or orthoimg!!!
//******************************************************************************


//******************************************************************************
// overlays and imagery
//******************************************************************************
groupIndexCount = 0;
// Customizable variables containing groups related info
groupedName[groupIndexCount] = "2005 Aerial Photos";  // alias to display in layer menu
groupedID[groupIndexCount] = "grouped-2005aerial";  // expects format to be "grouped-xxxxxxxx"
groupedType[groupIndexCount] = "orthoimg";  // use "overlay" "orthoimg" or "optional"
if (siteName == "REDWINGRESTRICTED") {
	groupedIDs[groupIndexCount] = new Array("grouped-orthoimg-2005ortho");
} else {
	groupedIDs[groupIndexCount] = new Array("grouped-orthoimg-2005ortho","grouped-orthoimg-nuccover");
}

if ((siteName != "GOODHUECOUNTYPUBLIC") && (siteName != "")) {
	groupIndexCount += 1;
	groupedName[groupIndexCount] = "2007 City Aerial Photos";  // alias to display in layer menu
	groupedID[groupIndexCount] = "grouped-2007aerial";  // expects format to be "grouped-xxxxxxxx"
	groupedType[groupIndexCount] = "orthoimg";  // use "overlay" "orthoimg" or "optional"
	if (siteName == "REDWINGRESTRICTED") {
		groupedIDs[groupIndexCount] = new Array("restricted-orthoimg-2007ortho","restricted-orthoimg-2007ortho-goodhue","restricted-orthoimg-2007ortho-kenyon","restricted-orthoimg-2007ortho-cannonfalls","restricted-orthoimg-2007ortho-redwing");
	} else {
		groupedIDs[groupIndexCount] = new Array("grouped-orthoimg-nuccover","restricted-orthoimg-2007ortho","restricted-orthoimg-2007ortho-goodhue","restricted-orthoimg-2007ortho-kenyon","restricted-orthoimg-2007ortho-cannonfalls","restricted-orthoimg-2007ortho-redwing");
	}
}

groupIndexCount += 1;
groupedName[groupIndexCount] = "Zoning - County";  // alias to display in layer menu
groupedID[groupIndexCount] = "grouped-zoning";  // expects format to be "grouped-xxxxxxxx"
groupedType[groupIndexCount] = "overlay";  // use "overlay" "orthoimg" or "optional"
groupedIDs[groupIndexCount] = new Array("grouped-zoning-main","grouped-zoning-add");

//******************************************************************************
// optionals
//******************************************************************************
groupIndexCount += 1;
groupedName[groupIndexCount] = "Parcel Information";
groupedID[groupIndexCount] = "grouped-parcel";
groupedType[groupIndexCount] = "optional";
groupedIDs[groupIndexCount] = new Array("grouped-parcels","grouped-parcelhousenumtxt","grouped-parcellocidtxt","grouped-parcelowners","grouped-plats");
groupedVisible[groupIndexCount] = true;
groupedExpand[groupIndexCount] = true;

groupIndexCount += 1;
groupedName[groupIndexCount] = "Water Features";
groupedID[groupIndexCount] = "grouped-water";
groupedType[groupIndexCount] = "optional";
groupedIDs[groupIndexCount] = new Array("grouped-lakes","grouped-streams","grouped-laketxt","grouped-boatlandings","grouped-watershed","grouped-gwslpy","grouped-shoreland","restricted-shpt","restricted-sppt");
groupedVisible[groupIndexCount] = true;
groupedExpand[groupIndexCount] = false;

groupIndexCount += 1;
groupedName[groupIndexCount] = "Transportation";
groupedID[groupIndexCount] = "grouped-transport";
groupedType[groupIndexCount] = "optional";
if (siteName.substring(0,5) == "GCCEA") {
	groupedIDs[groupIndexCount] = new Array("grouped-roads","grouped-roadtxt","grouped-railroad","grouped-airport","grouped-hwytypes","grouped-gccea-roads","grouped-gccea-hwytxt","grouped-gccea-roadtxt");
} else {
	groupedIDs[groupIndexCount] = new Array("grouped-roads","grouped-roadtxt","grouped-railroad","grouped-airport","grouped-hwytypes");
}
groupedVisible[groupIndexCount] = true;
groupedExpand[groupIndexCount] = false;

groupIndexCount += 1;
groupedName[groupIndexCount] = "Municipal";
groupedID[groupIndexCount] = "grouped-municipal";
groupedType[groupIndexCount] = "optional";
groupedIDs[groupIndexCount] = new Array("grouped-historiccommunity","grouped-citytwphalls","grouped-unincorp","grouped-municipalbounds");
groupedVisible[groupIndexCount] = true;
groupedExpand[groupIndexCount] = false;

groupIndexCount += 1;
groupedName[groupIndexCount] = "FEMA";
groupedID[groupIndexCount] = "grouped-fema";
groupedType[groupIndexCount] = "optional";
groupedIDs[groupIndexCount] = new Array("grouped-fema-floodzones","grouped-fema-floodways","restricted-fema-xs","restricted-fema-bfe");
groupedVisible[groupIndexCount] = false;
groupedExpand[groupIndexCount] = false;

if (siteName.indexOf("PUBLIC") == -1) {
	groupIndexCount += 1;
	groupedName[groupIndexCount] = "2ft Elevation Contours";
	groupedID[groupIndexCount] = "grouped-contours";
	groupedType[groupIndexCount] = "optional";
	groupedIDs[groupIndexCount] = new Array("restricted-cf-contours","restricted-gdh-contours","restricted-kn-contours","restricted-lc-contours","restricted-wan-contours");
	groupedVisible[groupIndexCount] = false;
	groupedExpand[groupIndexCount] = false;
}

if ((siteName.substring(0,7) == "REDWING") || (siteName == "ADMINISTRATOR")) {
	groupIndexCount += 1;
	groupedName[groupIndexCount] = "Red Wing Map";
	groupedID[groupIndexCount] = "grouped-redwing";
	groupedType[groupIndexCount] = "optional";
	groupedIDs[groupIndexCount] = new Array("restricted-rwpub-structures","restricted-rwpub-parks","restricted-rwpub-bikepath","restricted-rwpub-pollingplace","restricted-rwpub-facilities","restricted-rwpub-historicdistrict","restricted-rw-sidewalks","restricted-rw-wireless");
	groupedVisible[groupIndexCount] = true;
	groupedExpand[groupIndexCount] = false;
}

if ((siteName.substring(0,11) == "CANNONFALLS") || (siteName == "ADMINISTRATOR")) {
	groupIndexCount += 1;
	groupedName[groupIndexCount] = "Cannon Falls Map";
	groupedID[groupIndexCount] = "grouped-cannonfalls";
	groupedType[groupIndexCount] = "optional";
	groupedIDs[groupIndexCount] = new Array("restricted-cfpub-trails","restricted-cf-bluffs","restricted-gcug-parks","restricted-cf-sidewalks","restricted-gcug-trails");
	groupedVisible[groupIndexCount] = true;
	groupedExpand[groupIndexCount] = false;
	
	groupIndexCount += 1;
	groupedName[groupIndexCount] = "Cannon Falls Utilities";
	groupedID[groupIndexCount] = "grouped-cannonfalls-utility";
	groupedType[groupIndexCount] = "optional";
	groupedIDs[groupIndexCount] = new Array("restricted-cfpub-waterlines","restricted-cfpub-waterpts","restricted-cfpub-sanitarylines","restricted-cfpub-sanitarypts","restricted-cfpub-stormlines","restricted-cfpub-stormpts");
	groupedVisible[groupIndexCount] = false;
	groupedExpand[groupIndexCount] = false;
}

if ((siteName.substring(0,11) == "GOODHUECITY") || (siteName == "ADMINISTRATOR")) {
	groupIndexCount += 1;
	groupedName[groupIndexCount] = "City of Goodhue Map";
	groupedID[groupIndexCount] = "grouped-goodhuecity";
	groupedType[groupIndexCount] = "optional";
	groupedIDs[groupIndexCount] = new Array("restricted-gcug-parks","restricted-gdh-sidewalks","restricted-gcug-trails");
	groupedVisible[groupIndexCount] = true;
	groupedExpand[groupIndexCount] = false;

	groupIndexCount += 1;
	groupedName[groupIndexCount] = "City of Goodhue Utilities";
	groupedID[groupIndexCount] = "grouped-goodhuecity-utility";
	groupedType[groupIndexCount] = "optional";
	groupedIDs[groupIndexCount] = new Array("restricted-gdh-pipelines","restricted-gdh-pipefacilities");
	groupedVisible[groupIndexCount] = false;
	groupedExpand[groupIndexCount] = false;
}

if ((siteName.substring(0,6) == "KENYON") || (siteName == "ADMINISTRATOR")) {
	groupIndexCount += 1;
	groupedName[groupIndexCount] = "City of Kenyon Map";
	groupedID[groupIndexCount] = "grouped-kenyon";
	groupedType[groupIndexCount] = "optional";
	groupedIDs[groupIndexCount] = new Array("restricted-gcug-parks","restricted-kn-sidewalks","restricted-gcug-trails");
	groupedVisible[groupIndexCount] = true;
	groupedExpand[groupIndexCount] = false;

	groupIndexCount += 1;
	groupedName[groupIndexCount] = "City of Kenyon Utilities";
	groupedID[groupIndexCount] = "grouped-kenyon-utility";
	groupedType[groupIndexCount] = "optional";
	groupedIDs[groupIndexCount] = new Array("restricted-kn-stormpts","restricted-kn-stormlines","restricted-kn-sanitarypts","restricted-kn-sanitarylines","restricted-kn-waterpts","restricted-kn-waterlines","restricted-kn-strmpnd");
	groupedVisible[groupIndexCount] = false;
	groupedExpand[groupIndexCount] = false;
}

if ((siteName.substring(0,8) == "LAKECITY") || (siteName == "ADMINISTRATOR")) {
	groupIndexCount += 1;
	groupedName[groupIndexCount] = "Lake City Utilities";
	groupedID[groupIndexCount] = "grouped-lakecity-utility";
	groupedType[groupIndexCount] = "optional";
	groupedIDs[groupIndexCount] = new Array("restricted-lc-waterpts","restricted-lc-waterlines","restricted-lc-stormpts","restricted-lc-stormlines","restricted-lc-sanitarypts","restricted-lc-sanitarylines","restricted-lc-sanitaryplant");
	groupedVisible[groupIndexCount] = false;
	groupedExpand[groupIndexCount] = false;
}

if ((siteName.substring(0,9) == "WANAMINGO") || (siteName == "ADMINISTRATOR")) {
	groupIndexCount += 1;
	groupedName[groupIndexCount] = "City of Wanamingo Map";
	groupedID[groupIndexCount] = "grouped-wanamingo";
	groupedType[groupIndexCount] = "optional";
	groupedIDs[groupIndexCount] = new Array("restricted-gcug-parks","restricted-wan-sidewalks","restricted-gcug-trails","restricted-wan-hydrants");
	groupedVisible[groupIndexCount] = true;
	groupedExpand[groupIndexCount] = false;
}

if (siteName.substring(0,5) == "GCCEA") {
	groupIndexCount += 1;
	groupedName[groupIndexCount] = "GCCEA Base Layers";
	groupedID[groupIndexCount] = "grouped-gccea-base";
	groupedType[groupIndexCount] = "optional";
	groupedIDs[groupIndexCount] = new Array("optional-gccea-servicebound","grouped-indianreservation","grouped-forestboundary","grouped-parks","grouped-gccea-twpbound","grouped-gccea-sectionbound");//,"grouped-gccea-places"
	groupedVisible[groupIndexCount] = true;
	groupedExpand[groupIndexCount] = false;
	
	groupIndexCount += 1;
	groupedName[groupIndexCount] = "GCCEA Grid Layers";
	groupedID[groupIndexCount] = "grouped-gccea-grid";
	groupedType[groupIndexCount] = "optional";
	groupedIDs[groupIndexCount] = new Array("grouped-gccea-twpgrid","grouped-gccea-sectiongrid","grouped-gccea-qtrgrid","grouped-gccea-mapgrid");
	groupedVisible[groupIndexCount] = true;
	groupedExpand[groupIndexCount] = false;
	
	groupIndexCount += 1;
	groupedName[groupIndexCount] = "GCCEA Electric";
	groupedID[groupIndexCount] = "grouped-gccea-electric";
	groupedType[groupIndexCount] = "optional";
	groupedIDs[groupIndexCount] = new Array("grouped-gccea-servicebound","grouped-gccea-subboundary","grouped-gccea-cabinet","grouped-gccea-capacitor","grouped-gccea-circuitnumber","grouped-gccea-consumer","grouped-gccea-fuse","grouped-gccea-minmax","grouped-gccea-mm","grouped-gccea-node","grouped-gccea-ocr","grouped-gccea-openpoints","grouped-gccea-phaselabel","grouped-gccea-regulator","grouped-gccea-substation","grouped-gccea-switch","grouped-gccea-transformer","grouped-gccea-conductorohprimary","grouped-gccea-conductorugprimary","grouped-gccea-conductorsecondary","grouped-gccea-foreigntrans","grouped-gccea-gretrans");
	groupedVisible[groupIndexCount] = true;
	groupedExpand[groupIndexCount] = false;
	
	/*
	groupIndexCount += 1;
	groupedName[5] = "GCCEA Water";
	groupedID[5] = "grouped-gccea-water";
	groupedType[5] = "optional";
	groupedIDs[5] = new Array("grouped-gccea-lakes","grouped-gccea-rivers","grouped-gccea-laketxt");
	groupedVisible[5] = true;
	groupedExpand[5] = false;
	*/
}
//******************************************************************************
// do not edit below here
//******************************************************************************
// get indexes for group layers, set visibility and expansion if not already
var groupedIndexes = new Array();
var groupedLayerVisible = new Array();
var groupedHasAvailableLayers = new Array();
var groupedVisibleAtStart = new Array();
function getGroupedIndexes() {
	for (var j=0;j<groupedIDs.length;j++) {
		var tempIdxAry = new Array();
		var tempVisAry = new Array();
		for (var k=0;k<groupedIDs[j].length;k++) {
			for (var i=0;i<LayerID.length;i++) {
				if (groupedIDs[j][k] == LayerID[i]) {
					tempIdxAry[k] = i;
					tempVisAry[k] = LayerVisibleAtStart[i];
				}
			}
		}
		groupedIndexes[j] = tempIdxAry;
		groupedLayerVisible[j] = tempVisAry;
		if (groupedVisible[j] == null) groupedVisible[j] = false;
		if (groupedExpand[j] == null) groupedExpand[j] = false;
		groupedVisibleAtStart[j] = groupedVisible[j];
	}
	updateGroupedLayerVisibility();
	return false;
}

// reset group visibility from "home" button
function resetGroupedOptionalVisibility() {
	for (var j=0;j<groupedIDs.length;j++) {
		groupedVisible[j] = groupedVisibleAtStart[j];
		groupedExpand[j] = false;
		var tempVisAry = new Array();
		for (var k=0;k<groupedIDs[j].length;k++) {
			tempVisAry[k] = LayerVisibleAtStart[groupedIndexes[j][k]];
		}
		groupedLayerVisible[j] = tempVisAry;
	}
	updateGroupedLayerVisibility();
	return false;
}

// add grouped layers to TOC lists
function AddGroupedLayers() {
	for (i=0;i<groupedID.length;i++) {
		if ((groupedType[i] == "overlay") || (groupedType[i] == "orthoimg")) {
			tocOverlayList[tocOverlayList.length] = new Array(groupedName[i],groupedID[i],groupedType[i],groupedID[i]);
			groupedVisible[i] = false;
		} else { // optional
			if (groupedType[i] == "optional") tocOptionalList[tocOptionalList.length] = new Array(groupedName[i],groupedID[i],groupedID[i],"true");
		} // don't load restricted groups
	}
	return false;
}

// turn off all grouped overlay layers
function TurnGroupOverlaysOff() {
	for (var j=0;j<groupedID.length;j++) {
		if ((groupedType[j] == "overlay") || (groupedType[j] == "orthoimg")) {
			for (var k=0;k<groupedIndexes[j].length;k++) {
				LayerVisible[groupedIndexes[j][k]] = "false";
			}
		}
	}
	return false;
}

// turn on specified group overlay layer
function TurnGroupOverlayOn(theGroup) {
	for (var i=0;i<groupedID.length;i++) {
		if (groupedID[i] == theGroup) {
			tocOverlayCurrent = theGroup;
			tocOverlayCurrentType = groupedType[i];
			for (var k=0;k<groupedIndexes[i].length;k++) {
				LayerVisible[groupedIndexes[i][k]] = "true";
			}
		}
	}
	return false;
}

// turn on specified group optional layer
function TurnGroupOptionalOn(theGroup) {
	for (var i=0;i<groupedID.length;i++) {
		if (groupedID[i] == theGroup) {
			groupedVisible[i] = true;
			updateGroupedLayerVisibility();
		}
	}
	return false;
}

// add layers to optional list
function getOptionalGroupList(theOptionalIndex) {
	var tempString = ""
	for (var j=0;j<groupedID.length;j++) {
		if (groupedID[j] == tocOptionalList[theOptionalIndex][1]) { var tempGroupIdx = j; }
	}
	var tempGroupIndexes = groupedIndexes[tempGroupIdx];
	if (groupedHasAvailableLayers[tempGroupIdx]) {
		// main layer heading
		tempString += '<tr><td class="tocLayer" align="center" width="8"><input type="checkbox" onclick="setGroupVisibility('+tempGroupIdx+',this.checked);" class="tocInput" name="OptionalLayerList" value="group:' + tempGroupIdx + '" ';
		if (groupedVisible[tempGroupIdx] == true) tempString += 'checked';
		tempString += '>';
		// expanded group
		if (groupedExpand[tempGroupIdx]) {
			tempString += '<td width="24"><img src="images/folderopened.jpg" align="top" onClick="groupedExpand[\'' + tempGroupIdx + '\']=false;createTOC();"></td>';
			tempString += '<td class="tocLayer" align="left" colspan="2">' + groupedName[tempGroupIdx] + '</td></tr>';
			// group layers
			for (var l=0;l<tempGroupIndexes.length;l++) {
				if (LayerAvailable[tempGroupIndexes[l]] == "true") {
					tempString += '<tr><td></td><td width="24" align="right"><img src="images/foldercontent.gif" align="middle"></td>';
					tempString += '<td class="tocLayer" width="8"><input type="checkbox" onclick="setGroupLayerVisibility('+tempGroupIdx+','+l+',this.checked);" class="tocInput" name="OptionalLayerList" value="grouplayer:' + tempGroupIndexes[l] + '" ';
					if (groupedLayerVisible[tempGroupIdx][l] == "true") tempString += 'checked';
					tempString += '></td>';
					tempString += '<td class="tocLayer" align="left" width="' + (sidebarWidth-8-24-8-20) + '">' + LayerName[tempGroupIndexes[l]] + '</td>';	
					tempString += '</tr>';
				}
			}
		} else {// not expanded group
			tempString += '<td width="24"><img src="images/folderclosed.jpg" align="top" onClick="groupedExpand[\'' + tempGroupIdx + '\']=true;createTOC();"></td>';
			tempString += '<td class="tocLayer" align="left" colspan="2" width="' + (sidebarWidth-8-24-20) + '">' + groupedName[tempGroupIdx] + '</td></tr>';
		}
	}
	return tempString;
}

// set visibility variables for group
function setGroupVisibility(idxGroup,isOn) {
	groupedVisible[idxGroup] = isOn;
	updateGroupedLayerVisibility();
	if (mapAutoUpdate) {
		UpdateLayers();
	} else {
		return false;
	}
}

// set visibility variables for group layer
function setGroupLayerVisibility(idxGroup,idxGroupLayer,isOn) {
	if (isOn) {
		groupedLayerVisible[idxGroup][idxGroupLayer] = "true";
	} else {
		groupedLayerVisible[idxGroup][idxGroupLayer] = "false";
	}
	updateGroupedLayerVisibility();
	if (mapAutoUpdate) {
		UpdateLayers();
	} else {
		return false;
	}
}

// update all (sub)group layer visibilities after (sub)group visibility change
function updateGroupedLayerVisibility() {
	for (var i=0;i<groupedID.length;i++) {
		if (groupedType[i] == "optional") {
			var tempGroupHasData = false;
			var tempGroupIndexes = groupedIndexes[i];
			var tempGroupIDs = groupedIDs[i];
			for (var j=0;j<tempGroupIndexes.length;j++) {
				if (LayerAvailable[tempGroupIndexes[j]] == "true") tempGroupHasData = true;
				if ((groupedVisible[i]) && (groupedLayerVisible[i][j]) == "true") { // group and layer are on, turn on layer on map
					LayerVisible[tempGroupIndexes[j]] = "true";
				} else {
					LayerVisible[tempGroupIndexes[j]] = "false";
				}
			}
			groupedHasAvailableLayers[i] = tempGroupHasData;
		}
	}
	return false;
}