// glossary.js
/* the better way to do this would be to send an AJAX request to a service tied to the db etc, but the overall load this causes is small */
/* adds about 90KB to a page with glossary links, but oh well */
var glossary = new Object;
function loadGlossary (key) {
    if (glossary.links) {
    	if (glossary.links[key]) {
    		var newWindow = window.open('','details','width=590,height=300,scrollbars=0,addressbar=0');
    		var newDoc = newWindow.document.open();
    		newDoc.write ("<html><head><link rel='stylesheet' type='text/css' href='/MMNA/css/styles-oop.css'>"
    			+ "<body style='background:#fff'><div style='padding:10px'>"
    			+ "<!--[if IE]><style>img {position:relative;left:-40px}</style><![endif]-->"
    			+ glossary.links[key] + "</div></body></head></html>");
    		newDoc.close();
    		newWindow.focus();
    	} else {
    		/*
    		alert ("key not found");
    		alert (glossary.links.length);
    		*/
    	}
    } else {
    	//alert ("no array");
    }
}

$(document).ready( function () {
	if ($("a.glossaryLink").length) {
		// load glossary text
		$.get("/MMNA/jsp/glossary/index.do", function (data) {			
			glossary.text = data;
			glossary.links = new Array;
			// search the page for glossary links, extract the key, and preserve the data on the object
			$("a.glossaryLink").each ( function () {
				var key = $(this).attr("href").match(/\('[a-zA-Z0-9]+'\)/)[0];
		        if (key) {
		        	key = key.substring(2,key.length-2); // trim the parentheses and quote marks
		        	if (glossary.links[key] == undefined) {
		        		// pull the text from the data grab
		        		var startPoint = glossary.text.indexOf('<dt class="topSolid" name="' + key +'"');
		        		if (startPoint != -1) {
			        		var tempText = glossary.text.slice (startPoint);
			        		var endPoint = tempText.indexOf('</dd>') - 1;
			        		
			        		// set it into the object
			        		glossary.links[key] = glossary.text.slice (startPoint, startPoint + endPoint + 6);
		        		} else {
		        			glossary.links[key] = "Glossary key not found!";
		        		}
		        	}
		        }
		        
			});
		}, "text");		
	}
});


