

function glossary(id) {

	var start = new Date();

	//console.log('Starting ' + (new Date().getTime() - start.getTime()));
	
	var terms = _glossaryTerms;
	var $elem = $(id);

	var _done = [];

	//console.log('compiling re...' + (new Date().getTime() - start.getTime()));
	
	var re = new RegExp('\\b('+terms+')\\b', 'i'); 
	
	node = parent.firstChild;
	
	
	processNodes($elem[0]);

	function processNodes(parent) {
		for(var i=0, j=parent.childNodes.length; i<j; i++) {
			var node = parent.childNodes[i];
			if(node.nodeType == 3) {
				var t = (node.textContent ? node.textContent : node.data);
				$(node).replaceWith(t.replace(re, function(m, key, value) {
					if($.inArray(key, _done) > -1) return key;
					_done.push(key);
					return '<a class="glossary-term" href="/glossary/list/?t='+escape(key)+'">'+key+'</a>';
				}));
			}
			else if(node.hasChildNodes() && (node.tagName == 'P' || node.tagName == 'LI')) {
				processNodes(node);
			}
		}
	}	
	
	//console.log('replace complete ' + (new Date().getTime() - start.getTime()));


	$gt = $('<div id="GT" style="display:none"><div class="n"></div><div class="bd"><div class="bd-content"><h4>Title</h4><div class="content" /></div></div><div class="s" />');
	$gt.hide(0);
	$("body #container").append($gt);
	

	$gt.find('img')
		//.css('opacity','0.99')
		.click(function() {
			$('#GT').fadeOut(250);
		})
	;
	
	
	var id = 0;
	$('.glossary-term').each(function() {
		this.id = 'g'+(id++);
		$(this)
			.mouseover(function(event) {
				event.preventDefault();
				window.status = event.pageX + "," + event.pageY;
				$gt
					.hide(0)
					.css({left: ($(event.target).position().left + ($(event.target).width() / 2) - 106)+"px", top: ($(event.target).position().top+10)+"px"})
					.find('h4').text(this.innerHTML)
					.end()
					.mouseover(function() {
						window._cancelGtOff = true;
					})
					.mouseout(function() {
						window._cancelGtOff = false;
						window.setTimeout(gtOff, 500);
					})
					.find('.content').load(this.href, function() {
						if($.browser.msie) {;
							$gt.show(0);
						}
						else {
							$gt.fadeIn(250);
						}
					})					
					;
			})
			.mouseout(function(event) {
				window.setTimeout(gtOff, 500);
			});
	});

	function gtOff() {
		if(!window._cancelGtOff) {
			if($.browser.msie && parseInt($.browser.version) == 7) {;
				$('#GT').hide(0);
			}
			else {
				$('#GT').fadeOut(250);
			}
		}
	}


}


$(window).load(function() {
	if(_glossary_enable) {
		glossary('#content .col2');
	}
});