// Script Source: CodeLifter.com
// Copyright 2003
// Do not remove this header
// some minor changes made

isIE=document.all;
isNN=(!document.all&&document.getElementById);

isHot=false;
useable = false;
ID = "";
var loading = "<div id=\"glossaryBar\"><div onSelectStart=\"return false\"><div onMouseover=\"isHot=true\" onMouseout=\"isHot=false\"><a href=\"#\" onClick=\"hideMe();return false\"><img src=\""+location.href.substr(0,location.href.indexOf("lesson")+6)+"/graphics/gloss_close.gif\" /></a><div id=\"glossaryBarInterior\"></div></div></div></div><div id=\"glossaryContent\"><P class=\"glossary_def\">Loading...<br></P><div id=\"glossaryClose\"><a href=\"#\" onClick=\"hideMe();return false\">CLOSE</a></div></div>";

function init(ID){
  topDog="HTML";
  whichDog=isIE ? document.all[ID+"Block"] : document.getElementById(ID+"Block");  
  return (whichDog != null);
}

function ddInit(e){
  init(ID);
  hotDog=isIE ? event.srcElement : e.target;  
  
  while (hotDog.id!=ID+"Bar"&&hotDog.tagName!=topDog){
	  //alert(hotDog.tagName);
  hotDog=isIE ? hotDog.parentElement : hotDog.parentNode;
  }  
  if (hotDog.id==ID+"Bar"){
    offsetx=isIE ? event.clientX : e.clientX;
    offsety=isIE ? event.clientY : e.clientY;
    nowX=parseInt(whichDog.style.left);
    nowY=parseInt(whichDog.style.top);
    ddEnabled=true;
    document.onmousemove=dd;
  }
}

function dd(e){
  if (!ddEnabled) return;
  whichDog.style.left=(isIE ? nowX+event.clientX-offsetx : nowX+e.clientX-offsetx)+"px"; 
  whichDog.style.top=(isIE ? nowY+event.clientY-offsety : nowY+e.clientY-offsety)+"px";
  return false;  
}

function hideMe(){
  if (isIE||isNN) whichDog.style.visibility="hidden";
  whichDog.innerHTML = loading;
}

function showMe(){
  if (isIE||isNN) whichDog.style.visibility="visible";
}

//added funtions to adapt to our purposes
function create(newid){
  ID = newid;
  init(ID);
  //arbitrary amounts, have to be here for some reason, javascript cant read the values from the css
  whichDog.style.left=200+"px";
  whichDog.style.top=100+"px";
  whichDog.innerHTML = loading;
  useable = true;
}

function update(newid, e, type, html, wide){
  ID = newid;
  init(ID);
  whichDog.innerHTML = loading;
  html = location.href.substr(0,location.href.indexOf("lesson")+6) + "/glossary/glossary_cache/" + html.substr(html.lastIndexOf("/")+1);
  getPage(html, e);
  
  var bar = isIE ? document.all[ID+"Bar"] : document.getElementById(ID+"Bar");
  
  if(wide=="true")
  {
	whichDog.style.width = "390px"; 
	bar.style.width = "384px";
  }
  else
  {
	whichDog.style.width = "247px"; 
	bar.style.width = "241px"
  }
  	if(e!="false")
    {
		var o = eventCursor(e);
  		var y = o.y;
	}
	else y = 100;
  	var h = whichDog.offsetHeight;
  	var scrollTop = document.documentElement.scrollTop;
	if(isIE) y = y + scrollTop;
  	var screenHeight = (isIE)?document.documentElement.offsetHeight:window.innerHeight;
	//var screenHeight = document.body.clientHeight;
  	// Set the initial position.
	//alert(y+"\n" + scrollTop + "\n" + screenHeight + "\n" +h);
  	switch (type) {
   	 case 'wordwatch':
   	   y = y - 80;
		  break;
   	   default:
   	   if (y > (scrollTop + (screenHeight / 2))) {y = y - 15;}
		  	else {y = y + 14;}
		  }
		  // Get the whole panel on the screen, with the top showing a priority.
   	//for older netscapes scrolltop doesnt exist so just place the layer near
	if(document.documentElement.scrollTop!=null){
	    y = Math.min(y + h, scrollTop + screenHeight - 5) - h;
		y = Math.max(y, scrollTop + 5);
	}
	else
	{
		y = o.y - 80;
	}
	//alert(y);
    //whichDog.style.left=posx; 
    whichDog.style.top=y+"px";
    if(useable)showMe();
    return useable;
}

//*** Functions for handling the event object.

// eventCursor - Cross-browser handling of properties related to the cursor.
function eventCursor(e) {
  var o = new Object;
  
  // Get the page X and Y position.
  o.x = (e.pageX) ? e.pageX : e.clientX + document.body.scrollLeft;
  o.y = (e.pageY) ? e.pageY : e.clientY + document.body.scrollTop;

  // That's all folks.
  return o;
}

var xmlhttp

function getPage(url,e)
{
if(e!="false")
{
	var o = eventCursor(e);
  	var y = o.y;
}
else var y = 100;
// code for Mozilla, etc.
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest()
  xmlhttp.onreadystatechange=function() {state_Change(y)};
  xmlhttp.open("GET",url,true)
  xmlhttp.send(null)
  }
// code for IE
else if (window.ActiveXObject)
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
    if (xmlhttp)
    {
	xmlhttp.onreadystatechange=function() {state_Change(y)};
    xmlhttp.open("GET",url,true)
    xmlhttp.send()
    }
  }
}

function state_Change(y)
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
  {
  // if "OK"
  if (true)
  {
	var inner = xmlhttp.responseText;
	inner = inner.replace("/resources/explorations/groundup-cd/lesson",location.href.substr(0,location.href.indexOf("lesson")+6));
	if(inner.indexOf("http://amazing-spacedev.stsci.edu/resources/explorations/groundup-cd/lesson")>=0)inner = inner.replace("http://amazing-spacedev.stsci.edu/resources/explorations/groundup-cd/lesson",location.href.substr(0,location.href.indexOf("lesson")+6));
	whichDog.innerHTML=inner;
  	var h = whichDog.offsetHeight;
	var scrollTop = document.documentElement.scrollTop;
	if(isIE) y = y + scrollTop;
  	var screenHeight = (isIE)?document.documentElement.offsetHeight:window.innerHeight;
	if (y > (scrollTop + (screenHeight / 2))) {y = y - 15;}
	else {y = y + 14;}
	if(document.documentElement.scrollTop!=null){
		y = Math.min(y + h, scrollTop + screenHeight - 5) - h;
		y = Math.max(y, scrollTop + 5);
	}
	else
	{
		y = o.y - 80;
	}	
	whichDog.style.top=y+"px";
  }
  else
  {
  alert("Problem retrieving data:" + xmlhttp.statusText)
  }
  }
}



document.onmousedown=ddInit;
document.onmouseup=Function("ddEnabled=false");


