/*
vuDAT - Michigan State University
written by:  Nathan Lounds
*/
vuDAT_glossary_css = window.vuDAT_glossary_css || "/msu_jscript/glossary/glossary.css";
vuDAT_glossary = window.vuDAT_glossary || "/msu_jscript/glossary/glossary.txt";
section = window.section || "";  // set section = "" if section hasn't been defined
var timer;
var tmp_word = "";
var ajaxHandlers = {
	start : function(){
		ajaxHandlers.addLoadEvent(function(){
			// code in here gets executed once the page loads // begin section
			
			//<![CDATA[
			// loading the stylesheet into the document header
			if(document.createStyleSheet) { // non-standard IE way of doing it
				document.createStyleSheet(vuDAT_glossary_css);
			} else { // do it the W3C DOM way
				var newCSS=document.createElement('link');
				newCSS.rel='stylesheet';
				newCSS.href=vuDAT_glossary_css;
				newCSS.type='text/css';
				document.getElementsByTagName("head")[0].appendChild(newCSS);
			}
			//]]>
			
			var self = ajaxHandlers;
			self.ajax = new Ajax();
			var the_gloss = document.getElementsBySelector(".glossary");
			if(the_gloss) {
				for (i = 0; i < the_gloss.length; i++) {
					the_gloss[i].className = "highlightSpan";	
				}
			}
			the_words = document.getElementsBySelector(".highlightSpan");
			if(the_words) {
				var cur_word = "";
				for (i = 0; i < the_words.length; i++) {
					cur_word = the_words[i];
					cur_word.onmouseover = function() {
						tmp_word = this;
						stopper();
						self.getDefinition();
					}
					cur_word.onmouseout = function() {
						var the_div = document.getElementById("clickPosText");
						if(the_div) {
							timer = setTimeout("clickPosTextKill()",1200);
							//trace("timeout to kill");
						}
					}
					cur_word.onclick = function() {
						var me = this;
						var the_div = document.getElementById("clickPosText");
						the_div.title = "click to close";
						tmp_word = me;
						self.getDefinition();
					}
				}
			}
			ajaxHandlers.getGlossary();
			// code in here gets executed once the page loads // end section
		});
	},
	getDefinition : function() {
		//trace("getDefinition");
		clickPosText(tmp_word,"...looking for definition...");
		var self = ajaxHandlers;
		self.ajax.doGet(vuDAT_glossary,self.handleDefinition);
		//alert(vuDAT_glossary);
	},
	handleDefinition : function(str) {
		//trace("handleDefinition");
		var the_cont = document.getElementById("clickPosText");
		var the_words = str.split("\n");
		for (i = 0; i < the_words.length; i++) {
			var the_word = the_words[i].split("|");
			var the_term = tmp_word.childNodes[0].nodeValue;
			if(the_word[0].toUpperCase()==the_term.toUpperCase()) {
				if((the_word[1].toUpperCase()==section.toUpperCase())&&(the_word.length==3)) {
					the_cont.innerHTML = the_word[2];
					var my_diff = document.body.clientWidth-document.body.scrollWidth;
					if(my_diff!=0) {
						moveIt(the_cont,my_diff);
					}
					return true;
				} else if (the_word.length==2) {
					the_cont.innerHTML = the_word[1];
					var my_diff = document.body.clientWidth-document.body.scrollWidth;
					if(my_diff!=0) {
						moveIt(the_cont,my_diff);
					}
					return true;
				}
			}
		}
		the_cont.innerHTML = "<span class=\"red\">not in glossary</span>";
		var my_diff = document.body.clientWidth-document.body.scrollWidth;
		if(my_diff!=0) {
			moveIt(the_cont,my_diff);
		}
	},
	getGlossary : function() {
		var the_cont = document.getElementById("glossary");
		if(the_cont) {
			var self = ajaxHandlers;
			self.ajax = new Ajax();
			self.ajax.doGet(vuDAT_glossary,self.handleGlossary);
		}
	},
	handleGlossary : function(str) {
		var the_cont = document.getElementById("glossary");
		var the_words = str.split("\n");
		var ul = document.createElement("ul");
		for (i = 0; i < the_words.length; i++) {
			var the_word_arr = the_words[i].split("|");
			if(the_word_arr) {
				var the_word = the_word_arr[0];
				var the_def = "";
				var the_section = "";
				var li = document.createElement("li");
				if(the_word_arr.length==3) {
					the_section = " (" + the_word_arr[1] + ")";
					the_def = the_word_arr[2];
				} else if (the_word_arr.length==2) {
					the_def = the_word_arr[1];
				}
				li.innerHTML = "<strong>" + the_word + the_section + "</strong>: " + the_def;
				ul.appendChild(li);
			}
		}
		the_cont.appendChild(ul);
	},
	addLoadEvent : function(func){
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				oldonload();
				func();
			}
		}
	}
}
ajaxHandlers.start();

function clickPosText(obj,txt) {
	clickPosTextKill();
	var container = document.createElement('div');
	var pos = findPos(obj);
	container.id = "clickPosText";
	container.style.top = (pos[1]+18) + "px";
	container.style.left = (pos[0]+obj.offsetWidth) + "px";
	container.innerHTML = txt;
	document.body.appendChild(container);
	container.onclick = function() {
		killMe(this);
	}
	container.onmouseover = function() {
		stopper();
	}
	container.onmouseout = function() {
		timer = setTimeout("clickPosTextKill()",1200);
		//trace("timeout to kill (cont)");
	}
}
function clickPosTextKill() {
	//trace("clickPosTextKill()");
	var att = document.getElementById("clickPosText");
	if(att) {
		att.parentNode.removeChild(att);
	}
}
function moveIt(obj,howFar) {
	//trace("moveIt("+obj+","+howFar+")");
	var pos = findPos(obj);
	var the_pos = parseInt(obj.style.left);
	obj.style.left = (the_pos+howFar) + "px";
}
function stopper() {
	//trace("stopper()");
	clearTimeout(timer);
}
function killMe(obj) {
	obj.parentNode.removeChild(obj);
}
/* 
Find Position of object
http://www.quirksmode.org/js/findpos.html
*/
function findPos(obj) {
	//trace("findPos("+obj+")");
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
