var lastword;
var jargonTimer;

function jargon(word) {
	clearTimeout(jargonTimer);
	jargonTimer=setTimeout("jargonLookup(\""+word+"\")",200);
}

function jargonLookup(word) {

	// check if the jargonwindow already exists and word is the same
	if(document.getElementById("jargonwindow") && word==lastword) return;

	// if not, we need to do a lookup to get the html for new word

	// set last word for future reference
	lastword=word;

	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.onreadystatechange = function () {
		if(req.readyState == 4) {
			jargonWindow(req.responseText);
		}
	};
	req.open("GET", "/scripts/jargon.php?word="+word+"&random="+Math.random(), true);
	req.send(null);


}

function jargonWindow(html) {
	
	// if jargonwindow already exists we just need to update with the new innerHTML
	// if jargonwindow does not exist we need to create it before we populate the innerHTML
	// duplicate word requests have been checked before the async request went out

	if(!document.getElementById("jargonwindow")) {
		// create the element
		jw=document.createElement("div");
		// give it the right id
		jw.id="jargonwindow";
		jw.innerHTML="";
		
		iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body
		jwtop=document.all? iebody.scrollTop : pageYOffset
	
		if (window.innerWidth) { //if browser supports window.innerWidth
			jwwidth=window.innerWidth;
			jwheight=window.innerHeight;
		}
		else if (document.all) { //else if browser supports document.all (IE 4+)
			jwwidth=document.body.clientWidth;
			jwheight=document.body.clientHeight;
		}
	
		jwleft=((jwwidth-280)/2);
		jwtop=(jwtop<115)? 115 : jwtop;
		jw.style.top=10+jwtop+"px";
		jw.style.left=jwleft+"px";
		
		// finally append it - does IE5 bork?
		document.getElementById("main").appendChild(jw);
	}

	jw.innerHTML=html;

}

function destroyJargonWindow() {
	if(document.getElementById("jargonwindow")) {
		document.getElementById("main").removeChild(document.getElementById("jargonwindow"));
	}
}

