//*** Javascript for automatic glossary module ***//
// Adapted from :
/*-------------------------------------------
	Glossary-JS version 3.2 
	Michigan State University
	Virtual University Design and Technology
	Creator:  Nathan Lounds
	Project Page: http://code.google.com/p/glossary-js/
	Dependencies:  jquery.js (http://jquery.com)
		auto-highlighting requires jquery.highlight.js (http://bartaz.github.com/sandbox.js/jquery.highlight.html)
		glossary.css
	Copyright (c) 2011 Michigan State University Board of Trustees
	License: http://www.opensource.org/licenses/mit-license.php
--------------------------------------------*/
GlossaryJS_autohighlight = window.GlossaryJS_autohighlight || true;
//GlossaryJS_selector =window.GlossaryJS_selector || ".wrap-richtext-theme4";
GlossaryJS_selector =window.GlossaryJS_selector || ".content-article";

var GlossaryJS = {
	glossary: new Array(),
	initialize : function(glossary_arr, highlight_arr) {
		
		GlossaryJS.glossary = glossary_arr;
		
		$(".highlightSpan")
			.live("mouseover", function() {
				GlossaryJS.word = this;
				clearTimeout(GlossaryJS.timer);
				GlossaryJS.getDefinition();
			})
			.live("mouseout", function() {
				var the_div = document.getElementById("glossaryTooltip");
				if(the_div) {
					GlossaryJS.timer = setTimeout("$('#glossaryTooltip').remove()",1000);
				}
			})
			.live("click", function() {
				var me = this;
				me.blur();
				$("#glossaryTooltip").attr("title","click to close");
				GlossaryJS.word = me;
				GlossaryJS.getDefinition();
			});

		if(GlossaryJS_autohighlight===true && highlight_arr.length>0) {
			$(GlossaryJS_selector).highlight(highlight_arr, { wordsOnly: true, className: 'highlightSpan', firstOccurenceOnly: true, ignoreNodes: new Array('a','label','input','h1','h2','h3')});
		}
	},
	getDefinition : function() {
		for (i = 0; i < GlossaryJS.glossary.length; i++) {
			var the_term = GlossaryJS.word.childNodes[0].nodeValue;
			if(jQuery.trim(GlossaryJS.glossary[i].word.toUpperCase())==jQuery.trim(the_term.toUpperCase())) {
				GlossaryJS.createGlossaryTooltip(GlossaryJS.word,GlossaryJS.glossary[i]);
				return true;
			}
		}
		var tmp_obj = {};
		tmp_obj.def = "<span style='color:red;font-weight:bold'>Error:</span> not in glossary";
		tmp_obj.word = GlossaryJS.word;
		GlossaryJS.createGlossaryTooltip(GlossaryJS.word,tmp_obj);
	},
	createGlossaryTooltip : function(word_div,glossary_element) {
		$("#glossaryTooltip").remove();
		var position = $(word_div).offset();
		var the_left;
		$(document.body).append('<div id="glossaryTooltip"><div class="g_shadow"><div class="g_content"><div class="screen-reader">Definition for '+$(word_div).text()+'</div>'+glossary_element.def.substr(0,1000)+'<div class="screen-reader">End Definition</div></div></div></div>');
			
		//alert($(document).width()*0.5+' :: '+position.left+' :: '+$("#glossaryTooltip").innerWidth());
		if(position.left > ($(document).width()*0.5)) {  // put tooltip to left of word, starting from right of the word
			the_left = (position.left+$(word_div).width()-$("#glossaryTooltip").innerWidth()) + "px";
		} else {  // put tooltip to right of word, starting from left of the word
			the_left = position.left + "px";
		}
		//alert(the_left);
		$("#glossaryTooltip")
			.css("left",the_left)
			.css("top",(position.top+10+$(word_div).height())+"px")
			.click(function() {
				$(this).remove();
			})
			.mouseover(function() {
				clearTimeout(GlossaryJS.timer);
			})
			.mouseout(function() {
				GlossaryJS.timer = setTimeout("$('#glossaryTooltip').remove()",1000);
			}).focus();
	}
}

$(document).ready(function() {
	if(termsTableJs.length>0 && termsToHighlight.length>0){
		GlossaryJS.initialize(termsTableJs, termsToHighlight) ;
	}
});


