var expandedImagePath = 'images/common/triangle_expanded.gif';
var expandedImageHeight = 14;
var expandedImageWidth = 14;

var collapsedImagePath = 'images/common/triangle_collapsed.gif';
var collapsedImageHeight = 14;
var collapsedImageWidth = 14;

var mldStub = './mld/';

var tableWidth = "100%";
var tableBorder = 0;
var tableCellSpacing = 2;
var tableCellPadding = 6;
var tableChapterRowClass = 'generic_chapter_med'
var tableChapterTextColor = '#444444';
var tableLayerRowClass = 'generic_chapter_lite';

var mldLinkText = 'View map layer description';
var mapMakerLinkText = 'View sample layer in Map Maker';
var noMapMakerLinkText = 'No sample layer in Map Maker Available';
var noLayerAvailableText = 'No layer available in this section.';

var openChapters = new Array();
var paramstr = document.location.search;
var inHash = document.location.hash;
var mapMakerTarget = '';

function prepVariables () {

	if (paramstr != null && paramstr != "") {
		openChapters = (unescape(getInString(paramstr,0,'openChapters=','&'))).split(',');
		if (getInString(paramstr,0,'showAllChapters=','&') != null) {
			// clear any previous values and push in all values
			openChapters.length = 0;
			for (chapId in allChapters) {
				arrayPush(openChapters, chapId);
			}
		}
	}

	if (openChapters.length != allChapters.length && inHash != null && inHash != "") {
		if (inHash.charAt(0) == '#') {
			inHash = inHash.substring(1);
		}
		if (inHash.substr(0,3) != 'chp') {
			var layersInChapter = null;
			var layerId = null;
			var chapIdContainingHash = null;
			for (chapId in allChapters) {
				 layersInChapter = allChapters[chapId]['layers'];
				 for (layerId in layersInChapter) {
					 if (layersInChapter[layerId].substr(0,7) == inHash) {
						 chapIdContainingHash = chapId;
						 break;
					 }
				 }
			}
			if (chapIdContainingHash != null) {
				var needToAddChapter = true;
				for (chapId in openChapters) {
					if (chapId == chapIdContainingHash) {
						needToAddChapter = false;
						break;
					}
				}
				if (needToAddChapter) {
					arrayPush(openChapters, chapIdContainingHash);
				}
			}
		}
	}
}

function toggleChapter(theId) {
	var addIt = true;
	var localOpenChapters = new Array();
	for (var i=0; i < openChapters.length; i++) {
		if (openChapters[i] == theId) {
			addIt = false;
		} else {
			arrayPush(localOpenChapters, openChapters[i]);
		}
	}
	if (addIt) {
		arrayPush(localOpenChapters, theId);
	}
	openChapters = localOpenChapters;
	window.location.replace(window.location.pathname + '?openChapters=' + escape(openChapters.toString()) + '#' + theId);
}

function generateTableHtml(rowHeight, nameWidth, mldLinkWidth, mapMakerWidth, useRainbow, useExample) {
	var theOut = '<table width="' + tableWidth + '" border="' + tableBorder +
		'" cellspacing="' + tableCellSpacing + '" cellpadding="' + tableCellPadding + '" >\r\n';

	var theChapter;
	var theLayerDesc;
	var theLayerKey;
	var theChapKey;
	var chapterOpen;
	var layerCountInChapter = 0;

	for (theChapKey in allChapters) {
		theChapter = allChapters[theChapKey];
		if (theChapter.show) {
		chapterOpen = (arraySearch(openChapters, theChapter.idString) >= 0);
		
		theOut += '<tr height="' + rowHeight + '" '; 
		if (useRainbow) {
			theOut += ' class="' + theChapter.idString.substring(3) + '_med">'; 
		}	else {
			theOut += ' class="' + tableChapterRowClass + '">';
		}
		theOut += '<td id="' + theChapter.idString + '" colspan=3 >';
		theOut += '<a href="javascript:toggleChapter(\'' + theChapter.idString + '\')">';
		if (chapterOpen) {
			theOut += '<img src="' + expandedImagePath + '" height=' + expandedImageHeight + 
				' width=' + expandedImageWidth + ' border=0 alt="Hide all map layers in this category." />';
		} else {
			theOut += '<img src="' + collapsedImagePath + '" height=' + collapsedImageHeight + 
				' width=' + collapsedImageWidth + ' border=0 alt="View all map layers in this category." />';
		}
		theOut += '<font color="' + tableChapterTextColor + '">';
		if (useExample) {
			theOut += '<b>' + theChapter.shortName + ':</b>&nbsp;';
			if (theChapter.example != null) {
				theOut += theChapter.example;
			}
		} else {
			theOut += '<b>' + theChapter.longName + '</b>';
		}
		theOut += '</font></a></td></tr>\r\n';
		if (chapterOpen) {
			layerCountInChapter = 0;
			for(var i = 0; i < theChapter.layers.length; i++) {
				theLayerKey = theChapter.layers[i];
				theLayerDesc = allLayerDescs[theLayerKey];
				if (theLayerDesc.show)  {
							theOut += '<tr height="' + rowHeight + '" ';
							if (useRainbow) {
								theOut += 'class="' + theChapter.idString.substring(3) + '_lite">\r\n';
							}	else {
								theOut += 'class="' + tableLayerRowClass + '">\r\n';
							}
							theOut += '<td width=' + nameWidth + ' "><font size="1"> ';
							theOut += '<a name="' + theLayerDesc.idString.substr(0,7) + '"/>';
							theOut += '<a name="' + theLayerDesc.idString + '"/>';
							theOut += theLayerDesc.title + '</td>\r\n';
							theOut += '<td width=' + mldLinkWidth + ' align="left" ><font size="1">';
							theOut += '<a href="' + mldStub;
							if (theLayerDesc.altMldId == null) {
								theOut += theLayerDesc.idString.substr(0,7);
							} else {
								theOut += theLayerDesc.altMldId.substr(0,7);
							}
							theOut += '.html">' + mldLinkText + '</a></font></td>\r\n';
							theOut += '<td width=' + mapMakerWidth + ' align="left" ><font size="1">';
							if (theLayerDesc.mapLink == null) {
							  theOut += noMapMakerLinkText;
							} else {
							  theOut += '<a target="' + mapMakerTarget + '" href="' + theLayerDesc.mapLink + '">';
							  theOut += mapMakerLinkText + '</a>';
							}
							theOut += '</font></td>\r\n</tr>\r\n';
					layerCountInChapter++;
				}
			}
			if (layerCountInChapter == 0) {
				theOut += '<tr height=' + rowHeight + ' ><td colspan=3>' + noLayerAvailableText +'</td></tr>\r\n';
			}
			
		}
		}
	}
	theOut += '</table>\r\n';
	return theOut;
}
