/****************************************************************
 * Balloons in JavaScript. 
 *   
 * Balloon000515
 * by Christiaan Hofman, May 2000
 *   
 * This code is based upon the code by Michelle Wyner, provided by 
 * by Netscape Communications Corporation, copyright 1998. 
 *   
 * You may use or modify this code provided that this copyright notice
 * appears on all copies.
 *   
 *  First load this file using a SCRIPT tag. 
 *  Then create balloons using (either in head section or at the end):
 *   
 *    makeBalloon( id, width, text );
 *   
 *  Link to a balloon in the text using (within SCRIPT tags): 
 *   
 *    linkBalloon( id, text [, status, href] );
 *      
 *  The 'status' is the text in the statusline (default:  balloonStatus = "Tip"). 
 *  Special values: "id", for id, and "href" for the link URL. 
 *  Setting the 'href' makes it a clickable link. 
 *   
 *  You can change the styles of the balloons (DIV.balloon) and the 
 *  links to the balloons (A.tip and A.linktip) using the STYLE tag. 
 *  This must be done after loading the 'balloon.js' file. 
 *   
 *===============================================================
 *  Example: 
 *---------------------------------------------------------------
 *   
 *  <HTML>
 *  <HEAD>
 *
 *  <SCRIPT SRC="http://www.rci.rutgers.edu/~hofman/js/balloon.js"></SCRIPT>
 *  <STYLE><!--
 *  DIV.balloon { background-color:gray; layer-background-color:black; }
 *  --></STYLE>
 *  <SCRIPT> 
 *    function setUpTips() { 
 *        makeBalloon('ex1',100,'This is the first Balloon');
 *        makeBalloon('ex2',100,'This is the second Balloon');
 *        ...
 *    }   
 *  </SCRIPT> 
 *
 *  </HEAD> 
 *  <BODY> 
 *
 *  Some HTML and text, ...  
 *   
 *  ... when passing the <SCRIPT>linkBalloon('ex1','first balloon')</SCRIPT>, ... 
 *   
 *  ... when clicking the <SCRIPT>linkBalloon('ex2','second balloon','href','http://www.rci.rutgers.edu/~hofman')</SCRIPT>, ... 
 *   
 *  ... and some more HTML.  
 *   
 *  <SCRIPT> 
 *    setUpTips()
 *  </SCRIPT> 
 *   
 *  </BODY>
 *  </HTML>
 *   
 ****************************************************************
 */

//  Sets the styles for the Balloons and links to the balloons. Always needed. 

//  Creates a new Balloon.
function makeBalloon( id, width, message ) {
    var balloon = '<STYLE TYPE="text/css"><!-- #'+id+' {width:'+width+';height:auto;} --></STYLE>';
    balloon += '<DIV CLASS="balloon" ID="'+id+'">'+message+'</DIV>';
    document.writeln(balloon);
}

//  Creates a link to a Balloon.
function linkBalloon( id, text, status, href ) {
    status = status || balloonStatus;
    if (href) {
        var cls = 'linktip';
        if (status == 'href')  status = null;
    } else {
        var cls = 'tip';
        href = 'javascript:void(toggleBalloon(\''+id+'\'))';        
        if (status == 'href')  status = '';
    }
    if (status == 'id')  status = id;
    status = (typeof(status) == 'string')? 'status=\''+status+'\'' : '';
    document.write('<A HREF="'+href+'" CLASS="'+cls+'"'+
       ' onMouseOver="showBalloon(\''+id+'\', event);'+status+'"'+
       ' onMouseOut="hideBalloon(\''+id+'\',event)">'+
       text +'</A>');
}

//  Standard call for a Balloon, called by the link at mouseover. 
function showBalloon( id, event ) {
    if (document.all) {
        var x = document.body.scrollLeft + window.event.clientX + 10;
        var y = document.body.scrollTop + window.event.clientY + 10;
    } else if (document.layers || document.getElementById) {
        var x = event.pageX + 10;
        var y = event.pageY + 10;
    } 
    putBalloon(id, x, y);
    if (window.onBalloonCall)  window.onBalloonCall(id, event);             
}

//  Standard removal of a Balloon, called by the link at mouseout. 
function hideBalloon( id, event ) {
    removeBalloon(id);
    if (window.onBalloonHide)  window.onBalloonHide(id, event);
}

//  The event handelers onBalloonCall and onBalloonHide can be set to perform appropriate actions. 
//  When another Balloon is called or removed, use the following two functions (to avoid a loop). 
function putBalloon( id, x, y ) {
      var l;
      with (Math) {
      if (document.layers) {
          l = document.layers[id];
          l.left = min(max(x, window.pageXOffset), window.pageXOffset + window.innerWidth - l.clip.width);
          l.top = min(max(y, window.pageYOffset), window.pageYOffset + window.innerHeight - l.clip.height);
      } else if (document.all) {
          l = document.all[id].style;
          l.pixelLeft = min(max(x, document.body.scrollLeft), document.body.scrollLeft + document.body.clientWidth - l.pixelWidth);
          l.pixelTop = min(max(y, document.body.scrollTop), document.body.scrollTop + document.body.clientHeight -l.pixelHeight);
      } else if (document.getElementById) {
          l = document.getElementById(id).style;
          l.left = min(max(x, window.pageXOffset), window.pageXOffset + window.innerWidth - parseInt(l.width)) +"px";
          l.top = min(max(y, window.pageYOffset), window.pageYOffset + window.innerHeight - parseInt(l.height)) +"px";
      } 
    }
    if (!l) return;
    l.visibility = "visible";
    l.zIndex = 10;
}

//  Removal of a Balloon. 
function removeBalloon( id ) {
    if (document.layers) {
        var l = document.layers[id];
    } else if (document.all) {
        var l = document.all[id].style; 
    } else if (document.getElementById) {
        var l = document.getElementById(id).style;
    }
    if (!l) return;
    if (!l.fixed)   l.visibility = "hidden";
}

//  Toggles the fixation of the Balloon.
function toggleBalloon( id ) {
    if (document.layers) {
        var l = document.layers[id];
    } else if (document.all) {
        var l = document.all[id].style; 
    } else if (document.getElementById) {
        var l = document.getElementById(id).style;
    }
    if (!l) return;
    l.fixed = !l.fixed;
    if (l.fixed)  l.zIndex = 1;
}

//  The default balloon status text. 
var balloonStatus = "Tip";

//  The default balloon and link styles. 
document.writeln( '<STYLE TYPE="text/css"><!--',
    'DIV.balloon {',
    ' position:absolute; visibility:hidden; display:block; ',
    ' background-color:lightgrey; layer-background-color:white; color:black;',
    ' font-family:helvetica; border-color:darkgrey;',
    ' border-width:2; border-style:inset; padding:4; } ',
    'A.tip:link { font-size: 10pt; color:darkblue; cursor:default; } ',
    'A.tip:visited { text-decoration:none; color:green; cursor:default; } ',
    'A.tip:active { text-decoration:none; color:green; cursor:default; } ',
    'A.tip:hover { text-decoration:none; color:red; cursor:default; } ',
    'A.linktip:link { color:green; } ',
    'A.linktip:visited { color:darkgreen; } ',
    'A.linktip:active { color:green; } ',
    'A.linktip:hover { color:red }',
    '--></STYLE>' );
