// Pop-up text boxes for mouse-over help etc.
// Uses jax.js
// Pick an element that you want the "tie" the text to.  Code events on the tag:
//  <span onmouseout="plink_off()" onmouseover="plink_on(this,'Whatever text or HTML you want')" class="plink">Blah</span>
//  <span onmouseout="plink_off()" onmouseover="plink_dynamic(this,url)" class="plink">Blah</span>
//  <span onmouseout="plink_off()" onmouseover="plink_dynamic(this,url,true)" class="plink">Blah</span>

// If you use plink_dynamic(this,url), supply the url of a script that will return the desired
// text or HTML.  Your script will be passed a parameter plinkID.  Pass that ID back, separated 
// from the text by 4 equal signs.  If your script is passed plinkID=3, then pass back your
// text as:
//  3====text
// The plinkID guards against putting the text be the wrong element if the user has 
// moused quickly form one element to another.

// The result of the call to the URL will be cached UNLESS you supply the third argument,
// noCache as TRUE.  If you use onmouseover="plink_dynamic(this,'my_text.php',true)",
// my_text.php will be c alled (with a new plinkID) each time the user mouses over the
// element.
IncludeOnce("/scripts/jax.js");

var plink_pane = null;
var plinkID;
var plink_cache;
var plink_IsOn;
var plink_dynamic_who;
var plink_dynamic_url;

function plink_on( who, which )
{
  if( plink_pane == null )
    plink_init();
    
  plink_pane.innerHTML = which;
  plink_pane.style.left = 1;
  plink_pane.style.top = 1;
  plink_pane.style.display='block';

  var plink_browser_width;
  var plink_browser_height;
  var who_left = plink_objOffsetLeft( who );
  var who_top = plink_objOffsetTop( who );
  
  if( window.innerWidth ) {
    plink_browser_width = window.innerWidth;
    plink_browser_height = window.innerHeight;
  } else if( document.body && document.body.offsetWidth ) {
    plink_browser_width = document.body.offsetWidth;
    plink_browser_height = document.body.offsetHeight;
  } else if( document.documentElement && document.documentElement.offsetWidth ) {
    plink_browser_width = document.body.offsetWidth;
    plink_browser_height = document.body.offsetHeight;
  } else {
    plink_browser_width = 1600;
    plink_browser_height = 1280;
  }

  if( who_left > (plink_browser_width - who_left - who.offsetWidth) ) {
    if( who_top > (plink_browser_height - who_top - who.offsetWidth ) ) {
      plink_pane.style.left= Math.max(who_left - plink_pane.clientWidth - 3, 0) + 'px';
      plink_pane.style.top= Math.max(who_top - plink_pane.clientHeight, 0) + 'px';
    } else {
      plink_pane.style.left= Math.max(who_left - plink_pane.clientWidth - 3, 0) + 'px';
      plink_pane.style.top= Math.max(who_top + who.offsetHeight + 3, 0) + 'px';
    }
  } else {
    if( who_top > (plink_browser_height - who_top - who.offsetWidth ) ) {
      plink_pane.style.left=(Math.maxwho_left + who.offsetWidth + 3, 0) + 'px';
      plink_pane.style.top= Math.max(who_top - plink_pane.clientHeight, 0) + 'px';
    } else {
      plink_pane.style.left=Math.max(who_left + who.offsetWidth + 3, 0) + 'px';
      plink_pane.style.top= Math.max(who_top + who.offsetHeight + 3, 0) + 'px';
    }
  }
  
  plink_IsOn = true;
}

function plink_dynamic( who, url, noCache ) {
  if( plink_pane == null )
    plink_init();
    
  if( typeof noCache == 'undefined' )
    noCache = false;
    
  // First thing, do we have cached data?  If so, use it.
  if( ! noCache && plink_cache[url] ) 
    return plink_on( who, plink_cache[url] );
  
  plink_dynamic_url = url;
  
  // Set IsOn even though we don't the the pane open yet.  If it is false by then time 
  // we get the content back, we will not try to show it!
  plink_IsOn = true;
  
  // Call the URL to get the content.
  plink_dynamic_who = who;
  
  plinkID += 1;
  if( url.indexOf( '?' ) == -1 )
    url += '?';
  else
    url += '&';

  url += 'plinkID=' + plinkID;
  jax( url, plink_responder );
}


function plink_off()
{
  plink_IsOn = false;
  plink_pane.style.display="none";
}

function plink_init() {
  plink_pane = document.createElement("div"); 
  plink_pane.id = 'plink_pane';
  plink_pane.className = 'plink_pane';
  plink_pane.onclick = plink_off;
  document.body.appendChild(plink_pane);
  
  plinkID = 0;
  plink_cache = new Array();
  plink_IsOn = true;
}

function plink_responder( result, status ) {
  if( status == 200 ) {
    resultArray = result.split( '====' );
    
    if( resultArray[0] == plinkID ) {
      plink_cache[plink_dynamic_url] = resultArray[1];
      if( plink_IsOn ) {
        plink_on( plink_dynamic_who, resultArray[1] );
      }
    } 
  }
}

function plink_objOffsetTop( who )
{
  var edge = who.offsetTop;
  obj = who;
  
  while( obj.offsetParent )
  {
    obj = obj.offsetParent;
    edge += obj.offsetTop; 
  }
  
  return edge;
}
function plink_objOffsetLeft( who )
{
  var edge = who.offsetLeft;
  obj = who;
  
  while( obj.offsetParent )
  {
    obj = obj.offsetParent;
    edge += obj.offsetLeft; 
  }
  
  return edge;
}

