	var xmlDoc, $CallOutContainer, $CloseContainer, finished = false, usedTerms = new Array();
	var glossaryURL = 'http://www.publicintegrity.org/investigations/global_climate_change_lobby/Glossary/';
	
	window.onload = function() { CallOutInit($('.glossary-script-content'), $("#cpi_content")); };
	
	function CallOutInit($node, $close) {
		$CallOutContainer = $node;
		$CloseContainer = $close
		callOutNode = document.createElement("div");
		callOutNode.setAttribute("id", "callOutNode");
		$("body").prepend(callOutNode);
		$(callOutNode).hide();
		loadTerms();
	}
	
	// Load terms for inserting span tags
	function loadTerms() {
	  $.ajax({
		type: "GET",
		url: 'http://www.publicintegrity.org/investigations/global_climate_change_lobby/data/terms/',
		//url: 'Data/terms.xml',

		dataType: "xml",
		//error: function(XMLHttpRequest, textStatus, errorThrown) { alert(errorThrown); },
		success: function(xml, text) { 
		
			$(xml).find("term").each(function() {
				insertTag($(this).text(), $(this).attr("id"));
			});
		}
	  });
	  
	}
	
	// Load individual node
	function loadNode(id) {
	  $.ajax({
		type: "GET",
		beforeSend: function() { document.getElementById("callOutNode").innerHTML = 'Loading term...'; },
		url: glossaryURL + 'Scripts/create.php',
		dataType: "xml",
		data: 'id=' + id + '&type=id',
		success: function(xml, text) { 
			xmlDoc = xml;
			getByID(xmlDoc);
		}
	  });
	}
	
	// Glossary object
	function Glossary(xml) {
		var glossaryTerm, glossaryID, glossaryDef, glossaryAcronym;
		
		$(xml).find("entry").each(function() {
			glossaryTerm = $(this).find("term").text();
			glossaryID = $(this).attr("id");
			glossaryDef = $(this).find("definition").text();
			glossaryAcronym = $(this).find("acronym").text();
		});
		
		this.term = glossaryTerm;
		this.id = glossaryID;
		this.definition = glossaryDef;
		this.acronym = glossaryAcronym;
	}
	
	//Get by ID
	function getByID(xml) {
		
		mGlossary = new Glossary(xml);

		$entryNode = $("<div />");
		$entryNode.attr("id","glossaryEntry");
		
		$tempNode = $("<div />");
		$tempNode.attr("id", "closeCallOut");
		$tempNode.attr("class", "callOutLink");
		$tempNode.html("Close");
		$entryNode.append($tempNode);
		
		
		$tempNode = $("<div />");
		$tempNode.attr("id", "name");
		$tempNode.attr("class", "callOutBrand");
		$tempNode.html("Climate Lingo");
		$entryNode.append($tempNode);
		
		$tempNode = $("<div />");
		$tempNode.attr("id", "term");
		string = "<b>" + mGlossary.term;
		if(mGlossary.acronym != '') string += " (" + mGlossary.acronym +")</b>";
		else string += "</b>";
		$tempNode.html(string);
		$entryNode.append($tempNode);
		
		$tempNode = $("<div />");
		$tempNode.attr("id", "definition");
		$tempNode.html(mGlossary.definition);
		$entryNode.append($tempNode);
		
		$("#callOutNode").html($entryNode);
		
		$("#closeCallOut").live('click', function() { 
      		$(callOutNode).hide('fast');
   		});
		
	}
	
	// Format currency
	function formatCurrency(number) { 
		var temp = new String, count = 0, formated = '$';
		
		for(var i=number.length-1; i>-1; i--) {
			if(count == 3) {
				temp += ',';
				temp +=  number.charAt(i);
				count = 0;
				count++;
			}
			else {
				temp += number.charAt(i);
				count++;
			}
			
		}
		
		for(var i=temp.length; i>-1; i--) {
			formated += temp.charAt(i);	
		}
		return formated;
	}
	
	//Insert tag
	function insertTag(searchstring, id) {
		searchExp = new RegExp(searchstring, 'i');
		var usedIndex = new Number();
		
		for(i=0; i<searchstring.length; i++) {
			usedIndex += searchstring.charCodeAt(i);
		}
		
		
		//alert(usedIndex);
		

			$($CallOutContainer).find("p").each(function(index, domelm) {
				temp = $(this).html();
				var count = 0;
				if(temp.search(searchExp) != -1) {
					term = temp.slice(temp.search(searchExp), temp.search(searchExp)+searchstring.length)
					nodeID = Math.floor(Math.random()*1000);
					
					replaceTag = "<span id=\"callOutTag_" + nodeID + "\" class=\"callOutTag\" style=\"white-space:nowrap\"><img src=\"" + glossaryURL + "Images/document.png\" id=\"calloutImg\"/><span class=\"callOutLink\" onmouseover=\"drawIt(" + nodeID + ", " + id + ")\">" + term + "</span></span>";
					
					$(this).html(temp.replace(searchExp, replaceTag));
					
					tagNode = document.getElementById('callOutTag_' + nodeID);
					if (navigator.userAgent.search(new RegExp('msie 7', 'i')) == -1) {
						clickNode = document.createElement("div");
						clickNode.setAttribute("id", "clickNode_" + nodeID);
						clickNode.setAttribute("class", "clickNode");
						clickNode.setAttribute("onmouseover", "drawIt(" + nodeID + ", " + id + ")");
						$("body").prepend(clickNode);
						$("#clickNode_"+nodeID).css({"width": $(tagNode).width(), "height":$(tagNode).height(), "position":"absolute", "left": $(tagNode).offset().left, "top": $(tagNode).offset().top, "z-index": "50"});
					}	

					return false;
				}
			});
			
			for(var i in usedTerms) {
				$("#e").append('key is: ' + i + ', value is: ' + usedTerms[i] + '<br />');
			}
			
			////// MAKE THIS WORK AGAIN! 
			
	}
	
	// draw callouts, loadxml
	function drawIt(nodeID, id) {
		$(callOutNode).hide();
		drawCallOut(nodeID);
		loadNode(id);        
	
	}
		
	//Draw callout
	function drawCallOut(id) {
		function findPos(obj) {
			var curleft = curtop = 0;
			if (obj.offsetParent) {
				do {
					curleft += obj.offsetLeft;
					curtop += obj.offsetTop;
				} while (obj = obj.offsetParent);
				return [curleft,curtop];
			}
		}
		
		calloutid = document.getElementById("callOutTag_" + id);
		middle = parseInt(findPos(calloutid)[0]) + parseInt($(calloutid).width()/2);
		
		var left = middle - parseInt($(callOutNode).width()/2);
		var top = parseInt(findPos(calloutid)[1]) - parseInt($(callOutNode).height()+22);
		if( parseInt(($(callOutNode).width())+left) > (parseInt($($CallOutContainer).innerWidth()) + $CallOutContainer.offset().left) ) {
			left = ($($CallOutContainer).width() + $CallOutContainer.offset().left) - ($('#callOutNode').innerWidth());
		}
		if(left < $CallOutContainer.offset().left) {
			left = $CallOutContainer.offset().left - ($($CallOutContainer).width() - $($CallOutContainer).innerWidth());
		}
		
		$(callOutNode).show('fast');
		$(callOutNode).css({'top' : top, 'left' : left});
		$(calloutid).live('mousedown', function() { 
      		$(callOutNode).show('fast');
   		});

		//$CloseContainer.attr('mousedown', "function() {$(callOutNode).hide('fast');}");

		$($CloseContainer).live('mousedown', function() { 
      		$(callOutNode).hide('fast');
   		});		
	}