var chapterLinkImagePath = 'images/common/mldown.gif';
var chapterLinkImageHeight = 16;
var chapterLinkImageWidth = 23;

var chapterLinkTableWidth = "100%";
var chapterLinkTableBorder = 0;
var chapterLinkTableCellSpacing = 0;
var chapterLinkTableCellPadding = 4;
var chapterLinkTableChapterRowColor = '#ffffff';
var chapterLinkTableHeaderRowColor = '#ffffff';

function jumpToChapter(theId) {
	var addIt = true;
	for (var i=0; i < openChapters.length; i++) {
		if (openChapters[i] == theId) {
			addIt = false;
			break;
		}
	}
	if (addIt) {
		arrayPush(openChapters, theId);
	}
	window.location.replace(window.location.pathname + '?openChapters=' + escape(openChapters.toString()) + '#' + theId);
}

function generateChapterLinkTable (headerRowHeight, chapterRowHeight, numColumns, chapterLinkTableHeaderText) {
	var theChapter;
	var theChapKey;

	var visChapKeys = new Array();

	for (theChapKey in allChapters) {
		if (allChapters[theChapKey]["show"]) {
			arrayPush(visChapKeys, theChapKey);
		}
	}

	var numRows = Math.ceil(visChapKeys.length / numColumns);

	var theOut = '<table width="' + chapterLinkTableWidth + '" border="' + chapterLinkTableBorder +
		'" cellspacing="' + chapterLinkTableCellSpacing + '" cellpadding="' + chapterLinkTableCellPadding + '" >\r\n';
	theOut += '<tr bgcolor="' + chapterLinkTableHeaderRowColor + '" height=' + headerRowHeight + '>';
	theOut += '<td  colspan="' + numColumns + '" class="c" >';
	theOut += '<h2 class="c">' + chapterLinkTableHeaderText + '</h2></td></tr>\r\n';

	var theIndex = 0;
	for (var row = 0; row < numRows; row ++) {
		for (var col = 0 ; col < numColumns ; col++) {
			if (col == 0) {
					theOut += '<tr bgcolor="' + chapterLinkTableChapterRowColor + '" height="' + chapterRowHeight + '">\r\n';
			}
			theIndex = (row + (col * numRows));
			theOut += '<td valign="top">';
			if (theIndex < visChapKeys.length) {
				theChapKey = visChapKeys[theIndex];
				theChapter = allChapters[theChapKey];
				theOut += '<a href="javascript:jumpToChapter(\'' + theChapter.idString + '\')">';
				theOut += '<img src="' + chapterLinkImagePath + '" height=' + chapterLinkImageHeight + 
					' width=' + chapterLinkImageWidth + ' border=0 alt="Jump To Chapter Listing" border="0" />';
				theOut += theChapter.shortName + '</a>';
			} else {
				theOut += '&nbsp;';
			} 
			theOut += '</td>\r\n';
			if (col == (numColumns - 1)) {
					theOut += '</tr>\r\n';
			}
		}
	}

	theOut += '</table>\r\n';
	return theOut;
}
