//see hidepopup.js - closes glossary popup.

// current position of mouse on the PAGE
var pageX = -1;
var pageY = -1;

// current postion of mouse on the SCREEN
var screenX = -1;
var screenY = -1;

// once the glossary is selected the origonal click position is saved.
// current position of mouse on the PAGE
var clickedPageX = -1;
var clickedPageY = -1;

// current postion of mouse on the SCREEN
var clickedScreenX = -1;
var clickedScreenY = -1;

// offset from click point of the glossary div
var offsetX = 20;
var offsetY = 20;

// browser check
var ie = (document.all) ? 1:0;
var ns6 = (document.getElementById && !document.all) ? 1:0;

// register onMouseMove event handler
document.onmousemove = update;// update(event) implied on NS, update(null) implied on IE
function update(e)
{
  if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)

  if (e)
  { 
      screenX = e.clientX;
      screenY = e.clientY;
      
      pageX = getBody().scrollLeft + screenX;
      pageY = getBody().scrollTop + screenY;
  }
}

// ensures that we get the correct document object to work with
function getBody(){
    
    if(document.compatMode && document.compatMode!="BackCompat")
        return document.documentElement;
    else
        return document.body;
}

// AJAX - data get
function ajaxGetPopup(strURL) {
    var req;
    
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    if(!req){
        alert("Could not obtain xmlRequest object");
    }
    
    req.open('GET', strURL, true);
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            populatePopup(req.responseText);
        }
    }
    req.send(null);
}

// Called from ajax once data is loaded
function populatePopup(content){
    var glossary = document.getElementById("glossaryHTML");
    var div = document.getElementById("glossaryDiv");
    glossary.innerHTML = content;
    //alert(content);

    // ensure that glossary will be visible now that content is visible
    var windowWidth = (ie ? getBody().clientWidth : window.innerWidth - 20);
    var windowHeight = (ie ? getBody().clientHeight : window.innerHeight - 20);
    
    var glossaryWidth = glossary.clientWidth;
    var glossaryHeight = glossary.clientHeight;
    
    
    if(clickedScreenX + offsetX + glossaryWidth > windowWidth){
        
    }
    
    
    if(clickedScreenY + offsetY + glossaryHeight > windowHeight){
        // Y overflow - reposition
        div.style.top = clickedPageY - glossaryHeight - (offsetY*2) + "px";
    }
}

function glossary(url){

    var glossary = document.getElementById("glossaryDiv");
    if(!glossary)
    {
        return false;
    }
    glossClicked = 1;
    
    // set glossary to say 'loading' while waiting for ajax
    var glossText = document.getElementById("glossaryHTML");
    glossText.innerHTML = "loading...";
    
    // save the mouse positions for later
    clickedPageX = pageX;
    clickedPageY = pageY;
    clickedScreenX = screenX;
    clickedScreenY = screenY;
    
    // position the glossary
    glossary.style.left = clickedPageX + offsetX + 'px';
    glossary.style.top  = clickedPageY + offsetY + 'px';
    
    glossary.style.zIndex = 10; // move it to the top
    glossary.style.visibility = "visible";
    
    // get content
    ajaxGetPopup(url);
}

function hide(){
    document.getElementById("glossaryDiv").style.visibility = "hidden";
    glossClicked = 0;
}
