/*******************************
	DICTIONARY STUFF
*******************************/

//initialize dictionary
if(!loggedin) {
	addEvent(window,"load",dictionaryInit);
	var glossaryXML;
	var termsList = new Array();
}

function dictionaryInit() {
	//alert("Initializing Inline Dicionary System.");
	loadDictionaryData();
	
	var definitionBox = document.createElement("div");
	var subBox = document.createElement("div");
		subBox.id = "defcontent";
		subBox.className = "defcontent";
		subBox.innerHTML = "Blah blah blah, yadda yadda yadda, and so on and so forth. Blah blah blah, yadda yadda yadda, and so on and so forth.";
		definitionBox.appendChild(subBox);
		subBox = document.createElement("div");
		subBox.className = "defshadright";
		definitionBox.appendChild(subBox);
		subBox = document.createElement("div");
		subBox.className = "defshadbottom";
		definitionBox.appendChild(subBox);
		subBox = document.createElement("div");
		subBox.className = "defshadcorner";
		definitionBox.appendChild(subBox);
	definitionBox.id = "defbox";
	document.body.appendChild(definitionBox);
	definitionBox.style.visibility = "hidden";
}

function loadDictionaryData() {
	glossaryXML = getHTTPObject();
	glossaryXML.open("GET","xml/glossaryData.aspx",true);
	glossaryXML.onreadystatechange = function() {
		if(glossaryXML.readyState == 4) {
			dictionaryToArray();
		}
		}
	glossaryXML.send(null);
}

function getHTTPObject() {
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp = false;
			}
		}
	@else
	xmlhttp = false;
	@end @*/
	
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

function dictionaryToArray() {
	var rootElem = glossaryXML.responseXML.getElementsByTagName("glossary");
	if(rootElem[0]) {
		var theTerms = rootElem[0].getElementsByTagName("term");
		for(var i = 0;i < theTerms.length;i++) {
			termName = theTerms[i].getAttribute("name");
			termDef = theTerms[i].firstChild.nodeValue;
			termsList[termName.toLowerCase()] = termDef;
			placeDefinition2(termName,termDef);
		}
	}
}

function placeDefinition2(term,def) {
	searchIn = document.getElementById("mainbody_right_content");
	paragraphs = searchIn.getElementsByTagName("p");
	
	for(var i = 0;i < paragraphs.length;i++) {
		var paraKids = paragraphs[i].childNodes;
		for(var j = 0;j < paraKids.length;j++) {
			var thisText = paraKids.item(j);
			while(thisText.nodeType != 3) {
				//do recursive
				if(thisText.nextSibling == null) {
					break;
				}
				thisText = thisText.nextSibling;
			}
			
			var origContent = thisText.nodeValue;
			var contentLower = origContent;
			var termLower = term;
			contentLower = contentLower.toLowerCase();
			var termIndex = contentLower.toUpperCase().indexOf(termLower.toUpperCase());
			if(termIndex >= 0) {
				//Get the content
				preText = origContent.substring(0,termIndex);
				termAsIs = origContent.substring(termIndex,termIndex + term.length);
				postText = origContent.substring(termIndex + term.length);
				
				//Make new nodes
				termNode = document.createElement("a");
				termNode.setAttribute("href","glossary.aspx#" + term);
				//termNode.setAttribute("title","Definition of " + term);
				termNode.className = "medterm";
				termNode.appendChild(document.createTextNode(termAsIs));
				addEvent(termNode,"mouseover",showDef);
				addEvent(termNode,"mouseout",showDef);
				
				//put in new nodes
				thisText.parentNode.insertBefore(document.createTextNode(preText),thisText);
				thisText.parentNode.insertBefore(termNode,thisText);
				thisText.parentNode.insertBefore(document.createTextNode(postText),thisText);
				thisText.parentNode.removeChild(thisText);
			}
		}
	}
}


function showDef(e) {
	if(!e) var e = window.event;
	var targ;
	if(e.target) { targ = e.target; } else if(e.srcElement) { targ = e.srcElement; }

	while(targ.nodeType != 1 && targ.className != "medterm") {
		if(targ.parentNode) {
			targ = targ.parentNode;
		} else {
			alert("could not find term");
			return false;
		}
	}
	
	theTerm = targ.firstChild.nodeValue;
	
	defBox = document.getElementById("defbox");
	//alert(defBox.style.visibility);
	
	
	if(defBox.style.visibility == "hidden") {
		//alert("building new box");
		defCont = document.getElementById("defcontent");
		//defCont = document.createElement("div");
		//defCont.className = "defcontent";
		//defCont.id = "defcontent";

		var definitionText = "<strong>" + theTerm + ": </strong>" + termsList[theTerm.toLowerCase()];
		definitionText += "<br/><em>(Click term to go to glossary.)</em>";

		var definitionBox = document.createElement("div");
		var subBox = document.createElement("div");
			subBox.id = "defcontent";
			subBox.className = "defcontent";
			subBox.innerHTML = definitionText;
			definitionBox.appendChild(subBox);
			subBox = document.createElement("div");
			subBox.className = "defshadright";
			definitionBox.appendChild(subBox);
			subBox = document.createElement("div");
			subBox.className = "defshadcorner";
			definitionBox.appendChild(subBox);
			subBox = document.createElement("div");
			subBox.className = "defshadbottom";
			definitionBox.appendChild(subBox);
		definitionBox.id = "defbox";

		defBox.parentNode.replaceChild(definitionBox,defBox);
		defBox = definitionBox;

		var posLeft = targ.offsetLeft + 20;
		var posTop = targ.offsetTop + 20;
		offsetter = targ;
		while(offsetter.offsetParent) {
			posLeft += offsetter.offsetParent.offsetLeft;
			posTop += offsetter.offsetParent.offsetTop;
			offsetter = offsetter.offsetParent;
		}

		defBox.style.left = posLeft + "px";
		defBox.style.top = posTop + "px";

		//defBox.replaceChild(defCont,oldDefCont);
		//var newBottom = document.createElement('div');
		//newBottom.className = "defshadbottom";
		//defBox.replaceChild(newBottom,defBox.lastChild);

		//defBox.style.visibility = (defBox.style.visibility == "visible") ? "hidden" : "visible";
		defBox.style.visibility = "visible";
	} else {
		defBox.style.visibility = "hidden";
	}
}