// JavaScript Document
// copyright (c) 2007 Gary Phillips II
var searchPosition = 0;
var searchWithin = "";
var wordCount = 0;
var previousWord = "";

var dictArray = new Array(); var dictNum = 0;
var dictArrayL = new Array();
aW ("Dan Brown",			"Brown, Dan");
//aW ("Howard Carter",		"Carter, Howard");
//aW ("The Code Room",		"Code Room");
//aW ("Central Intelligence Agency","CIA");
//aW ("The CIA",				"CIA");
//aW ("copper scroll");
//aW ("Covert Obsolescence");
aW ("Elonka Dunin",			"Dunin, Elonka");
aW ("Elonka",				"Dunin, Elonka");
aW ("Dunin",				"Dunin, Elonka");
//aW ("Jim Gillogly",			"Gillogly, Jim");
//aW ("Gillogly",				"Gillogly, Jim");
//aW ("The KGB",				"KGB");
//aW ("Kryptos");
//aW ("The Listening Post",	"Listening Post");
aW ("Phillips, Gary II",	"Phillips, Gary II");
aW ("Gary Phillips II",		"Phillips, Gary II");
aW ("Gary Phillips",		"Phillips, Gary II");
aW ("Mr. Phillips",			"Phillips, Gary II");
aW ("Phillips",				"Phillips, Gary II");
aW ("Ben Seaver",			"Seaver, Ben");
//aW ("James Sanborn",		"Sanborn, James");
//aW ("Jim Sanborn",			"Sanborn, James");
//aW ("Sanborn",				"Sanborn, James");
//aW ("Ed Scheidt",			"Scheidt, Ed");
//aW ("Scheidt",				"Scheidt, Ed");
aW ("Gary Warzin",			"Warzin, Gary");
aW ("Warzin",				"Warzin, Gary");

function implementDictionary() {
	prepareHTML();
	for (var dictIter = 0; dictIter<dictArray.length; dictIter++) {
		if (dictArrayL[dictIter] != previousWord) {
			previousWord = dictArrayL[dictIter];
			wordCount = 0;
		}
		implementWordLink (dictIter);
		if (wordCount>0 && dictIter<dictArray.length-1 && dictArrayL[dictIter] != dictArrayL[dictIter+1]) addWordLink(dictIter);
		if (wordCount>0 && dictIter == dictArray.length-1) addWordLink(dictIter);
	}
}

function prepareHTML() { //preformat
	document.getElementById("idDictionary").innerHTML = document.getElementById("idDictionary").innerHTML;
	searchWithin = document.getElementById("idDictionary").innerHTML;
}

function implementWordLink (inWord) {
	var searchFound = 0;
	searchPosition = 0;
	
	while (searchFound!=-1) {
	  searchFound = searchWithin.toUpperCase().indexOf(dictArray[inWord].toUpperCase(),searchPosition);
	  if (searchFound!=-1) createWordLink(inWord,searchFound);
	}
}

function createWordLink(inWord, inFound) {
	var canCreate = true;
	var openTag = searchWithin.toUpperCase().indexOf("<A ", inFound);
	var closeTag = searchWithin.toUpperCase().indexOf("</A>", inFound);
	if (closeTag<openTag) canCreate = false;
	if ((openTag==-1) && (closeTag>-1)) canCreate = false;
	var openTag = searchWithin.toUpperCase().indexOf("<!--C*D*-->", inFound);
	var closeTag = searchWithin.toUpperCase().indexOf("<!--O*D*-->", inFound);
	if (closeTag<openTag) canCreate = false;
	if ((openTag==-1) && (closeTag>-1)) canCreate = false;
	var openTag = searchWithin.toUpperCase().indexOf("<", inFound);
	var closeTag = searchWithin.toUpperCase().indexOf(">", inFound);
	if (closeTag<openTag) canCreate = false;
	if ((openTag==-1) && (closeTag>-1)) canCreate = false;
	
	if (canCreate) {
		wordCount++;
		var newHTML = searchWithin.substring(0,inFound); var beforeLength = searchWithin.length;
		newHTML += "<a href=\"http://www.kryptos.info/dictionary/lookup.asp?word="+escape(dictArrayL[inWord])+"\" title=\"more information about "+dictArrayL[inWord]+"\">";
		newHTML += searchWithin.substring(inFound,inFound+dictArray[inWord].length);
		newHTML += "</a>";
		newHTML += searchWithin.substring(inFound+dictArray[inWord].length,searchWithin.length);

		document.getElementById("idDictionary").innerHTML = newHTML;
        searchWithin = document.getElementById("idDictionary").innerHTML;
		searchPosition = Number(inFound + (searchWithin.length-beforeLength) + 1);

	} else {
		searchPosition = inFound + 1;
	}
}

function aW (inWord, inWordList) {
	if (inWordList==null) inWordList = inWord;
	dictArray[dictNum]=inWord;
	dictArrayL[dictNum++]=inWordList;
}

function addWordLink (inWord) {
	document.getElementById("idTerminology").style.display="block";
	document.getElementById("idTerminologyLinks").innerHTML += "<div class=\"panelLink\"><img src=\"http://www.kryptos.info/images/page/panel-link.gif\" width=\"9\" height=\"9\" border=\"0\" />&nbsp;<a href=\"http://www.kryptos.info/dictionary/lookup.asp?word="+escape(dictArrayL[inWord])+"\" title=\"more information about "+dictArrayL[inWord]+"\">"+dictArrayL[inWord]+"</a> ("+wordCount+")</div>";
}