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 metadataStub = './metadata/';
var metadataExtension = '.faq.html';

var tableWidth = "100%";
var tableBorder = 0;
var tableCellSpacing = 2;
var tableCellPadding = 6;
var tableHeaderRowClass = 'gray_lite';
var tableChapterRowClass = 'generic_chapter_med';
var tableChapterTextColor = '#333333';
var tableLayerRowClass = 'generic_chapter_lite';
var tableHeaderMapLayer = 'Map Layer';
var tableHeaderDataDate = 'Current Raw Data Release';
var tableHeaderMetadataDate = 'Current Raw Data Documentation Release';
var tableHeaderFile = 'Format and Raw Data Name';
var tableHeaderSize = 'File Size in MB: Compressed / Decompressed';

var dataDateNotAvailable = "NA";
var metadataDateNotAvailable = "NA";
var sizeNotAvailable = "NA";
var realTimeText = "Updated Daily";

var openChapters = new Array();
var paramstr = document.location.search;
var inHash = document.location.hash;
var onsiteTarget = '_self';
var offsiteTarget = '_blank';

function prepVariables () {
	if (self != window.top) {
		onsiteTarget = '_top';
	}

	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 generateHeaderRow(rowHeight, nameWidth, dateWidth, fileWidth) {
	var theOut ='<tr height=' + rowHeight + '>\r\n' +
		'<td width=' + nameWidth + ' class="' + tableHeaderRowClass + '">' + tableHeaderMapLayer + '</td>\r\n' +
		'<td width=' + dateWidth + ' class="' + tableHeaderRowClass + '">' + tableHeaderDataDate + '</td>\r\n' +
		'<td width=' + dateWidth + ' class="' + tableHeaderRowClass + '">' + tableHeaderMetadataDate + '</td>\r\n' +
    '<td width=' + fileWidth + ' class="' + tableHeaderRowClass + '">' + tableHeaderFile + '</td>\r\n' +
		'<td class="' + tableHeaderRowClass + '">' + tableHeaderSize + '</td></tr>\r\n';
	return theOut;
}

function generateTableHtml(rowHeight, nameWidth, dateWidth, fileWidth, includeHeaderRowAtTop, includeHeaderRowWithChapter, useRainbow, useExample) {
	var theOut = '<table width="' + tableWidth + '" border="' + tableBorder +
		'" cellspacing="' + tableCellSpacing + '" cellpadding="' + tableCellPadding + '" >\r\n';

	if (includeHeaderRowAtTop) {
		theOut += generateHeaderRow(rowHeight, nameWidth, dateWidth, fileWidth);
	}

	var rowSpan = 1;
	var first = true;
	var theChapter;
	var theLayerDesc;
	var theDataItem;
	var theDataKey;
	var theLayerKey;
	var theChapKey;
	var chapterOpen;
	var layerCountInChapter = 0;

	for (theChapKey in allChapters) {
		theChapter = allChapters[theChapKey];
		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=5>';
		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;
			if (theChapter.layers.length > 0 && includeHeaderRowWithChapter) {
				theOut += generateHeaderRow(rowHeight, nameWidth, dateWidth, fileWidth);
			}
			for(var i = 0; i < theChapter.layers.length; i++) {
				theLayerKey = theChapter.layers[i];
				theLayerDesc = allLayerDescs[theLayerKey];
				if (theLayerDesc.show)  {
					rowSpan = theLayerDesc.dataItems.length;
					first = true;
					for (var j = 0; j < theLayerDesc.dataItems.length; j++) {
						theDataKey = theLayerDesc.dataItems[j];
						theDataItem = allDataItems[theDataKey];
						if (first) {
							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 + ' rowspan="' + rowSpan + '"><font size="1"> ';
							theOut += '<a name="' + theLayerDesc.idString.substr(0,7) + '"/>';
							theOut += '<a name="' + theLayerDesc.idString + '"/>';
							theOut += '<a target="' + onsiteTarget + '" href="' + mldStub;
							if (theLayerDesc.altMldId == null) {
								theOut += theLayerDesc.idString.substr(0,7);
							} else {
								theOut += theLayerDesc.altMldId.substr(0,7);
							}
							theOut += '.html">';
							theOut += theLayerDesc.title + '</a></td>\r\n';
							theOut += '<td width=' + dateWidth + ' align="center" rowspan="' + rowSpan + '"><font size="1">';
							if (theDataItem.creationDate == null) {
							  theOut += dataDateNotAvailable;
							} else if (theDataItem.creationDate == 'REAL_TIME') {
							  theOut += realTimeText;
							} else {
							  theOut += formatDate(theDataItem.creationDate, 'MONTH', ' ',  'YYYY');
							}
							theOut += '</font></td>\r\n';
							theOut += '<td width=' + dateWidth + ' align="center" rowspan="' + rowSpan + '"><font size="1">';
							if (theLayerDesc.metaDataDate == null) {
							  theOut += metadataDateNotAvailable;
							} else {
							  if (theLayerDesc.metaDataOffsiteLink != null) {
							    theOut += '<a target="' + offsiteTarget + '" href="' +
							     theLayerDesc.metaDataOffsiteLink + '">';
							  } else {
							    theOut += '<a target="' + onsiteTarget + '" href="' + metadataStub +
								  theLayerDesc.idString + metadataExtension + '">';
							  }
							  theOut += formatDate(theLayerDesc.metaDataDate, 'MONTH', ' ', 'YYYY') + '</a>';
							}
							theOut += '</font></td>\r\n';
							first = false;
						} else {
							theOut += '<tr height="' + rowHeight + '" ';
							if (useRainbow) {
								theOut += 'class="' + theChapter.idString.substring(3) + '_lite">\r\n';
							}	else {
								theOut += 'class="' + tableLayerRowClass + '">\r\n';
							}
						}
						theOut += '<td width=' + fileWidth + ' align="center"><font size="1">';
						if (theDataItem.offsiteText != null) {
						  theOut += '<a href="' + theDataItem.downloadUrl + '">' + theDataItem.offsiteText + '</a>';
						} else {
						  theOut += theDataItem.type + ' :  <a href="' + theDataItem.downloadUrl + '">' + theDataItem.fileName + '</a>';
						}
						theOut += '</font></td>\r\n';
						theOut += '<td align="center"><font size="1">';
						if (theDataItem.compressedBytes == null) {
						  theOut += sizeNotAvailable;
						} else {
						  theOut += formatBytes(theDataItem.compressedBytes);
						}
						theOut += ' / ';
						if (theDataItem.rawBytes == null) {
						  theOut += sizeNotAvailable;
						} else {
						  theOut += formatBytes(theDataItem.rawBytes);
						}
						theOut += '</font></td>\r\n</tr>\r\n';
					}
					layerCountInChapter++;
				}
			}
			if (layerCountInChapter == 0) {
				theOut += '<tr height=' + rowHeight + ' ><td colspan=5>No layer available in this section.</td></tr>\r\n';
			}
			
		}
	}
	theOut += '</table>\r\n';
	return theOut;
}
