//var GLOSSARY;
var GLOSSARY={
	'electropherogram': "a 'trace' of an electrophoresis experiment."
};

function loadDictionary() {
	// var serverUrl = window.location.protocol + "//" + window.location.host + '/';
	var dictionaryUrl = 'http://localhost/test/glossary.json'; // PHP faked in cocoon

	var callback = {
		success: function (response) {
			alert('Loaded dictionary.');
			var dictionary = eval( '(' + response.responseText + ')' );
			linkTerms(dictionary);
		},
		failure: function (response) {
			alert('Failed to load dictionary.');
		}
	}
	YAHOO.util.Connect.asyncRequest('GET', dictionaryUrl, callback);
}

function linkTerms(dictionary){
	var TAG = 'TAG_';
	//var target = document.getElementById('content2col');
	for (var term1 in dictionary){
		var re1 = new RegExp ('\\b' + term1 + '\\b','gi');
		for (var term2 in dictionary){
			dictionary[term2] = dictionary[term2].replace(re1, TAG + term1);	//  mark definitions so they do not get linked;
		}
	}
	for (var term in dictionary){
		var def = '  ' + term + ': ' + dictionary[term];
		var re = new RegExp ('\\b' + term + '\\b','gi');
		var url = 'http://en.wikipedia.org/wiki/' + term;
		//var html = target.innerHTML;
		var html = document.body.innerHTML;
		var link = makeLink(term, def, url);
		html = html.replace(re, link);
		//target.innerHTML = html;	
		document.body.innerHTML = html;
	}
	//target.innerHTML = target.innerHTML.replace(new RegExp (TAG,'g'), '');
	document.body.innerHTML = document.body.innerHTML.replace(new RegExp (TAG,'g'), '');
}

function makeLink(linkText, shortDef, url){
	var link =  '<a class=\"dictionaryTerm\" href=\"' + url + '\" title=\"' + shortDef + '\" >' + linkText + '<\/a>';
	return link;
}

function linkEm(){	// onload
	if (false) {
		loadDictionary();
		var ttb = document.getElementById('toolTipBox');
		ttb.innerHTML = '<form><textarea rows="50" cols="120">' + document.body.innerHTML + '<\/textarea><\/form>';
	} else {
		//GLOSSARY = { 'test' : 'a trial', 'link' : 'to form a hyperlink between two web pages.' };
		linkTerms(GLOSSARY);
		// alert('Terms linked:' + GLOSSARY['electropherogram']);
	}
}
