// FloatMaker is Copyright 2006, P. Lutus
// Portions Copyright 2006, Oliver Bryant and Matthew Tagg
// http://boxover.swazz.org */

if (typeof document.attachEvent!='undefined') {
   window.attachEvent('onload',setup);
   document.attachEvent('onmousemove',mouseMove);
}
else {
   window.addEventListener('load',setup,false);
   document.addEventListener('mousemove',mouseMove,false);
}

var the_box;

function setup() {
   the_box=document.createElement("div");
   the_box.style.width='12em';
   the_box.style.padding='4px';
   the_box.style.paddingLeft='8px';
   the_box.style.paddingRight='8px';
   the_box.style.fontSize='1.0em';
   the_box.style.border='1px solid #000000';
   the_box.style.background='#ffffc0';
   the_box.style.position="absolute";
   the_box.style.visibility='hidden';
   the_box.style.zIndex='100';
   document.body.appendChild(the_box);
}

// set this "true" to perform fade
var perform_fade = true;

// offset of box from mouse cursor, should not be zero
var offset = 10;
// velocity of fade
var fade_rate = 8;
// delay before action
var time_delay = 250;

var on_target = false;
var fadeTimer=null;

// entities need to specify class="floatmaker"
// and have the desired text in their ID field
// the ID field text can have HTML tags as: &lt;tag&gt;
// and they will be rendered correctly

function mouseMove(e)
{
   evt=e?e:event;
   if (evt) {
      target=evt.target?evt.target:evt.srcElement;
      bst=document.documentElement&&document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop;
      bsl=document.documentElement&&document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft;
      mouseX=evt.pageX?evt.pageX-bsl:evt.clientX-document.body.clientLeft;
      mouseY=evt.pageY?evt.pageY-bst:evt.clientY-document.body.clientTop;
      the_box.style.left=bsl+mouseX+offset+"px";
      the_box.style.top=bst+mouseY+offset+"px";
      on_target = (target.className == "floatmaker" && target.id.length > 0);
      if (on_target) {
         the_box.innerHTML = target.id;
         if(opacity < 50) {
            // don't fade in all over again if already visible
            clearTimeout(fadeTimer);
            if (perform_fade) {
               fadeTimer=setTimeout("setup_fade("+fade_rate+")",time_delay);
            }
            else {
               opacity = 100;
               fadeTimer=setTimeout("the_box.style.visibility='visible';fadeTimer=null;",time_delay);
            }
         }
      }
      else {
         if(opacity >= 50) {
            clearTimeout(fadeTimer);
            if (perform_fade) {
               fadeTimer=setTimeout("setup_fade(-"+fade_rate+")",time_delay);
            }
            else {
               opacity = 0;
               fadeTimer=setTimeout("the_box.style.visibility='hidden';fadeTimer=null;",time_delay);
            }
         }
      }
   }
}

var opacity = 0;
var max = 100;

function setup_fade(delta) {
   opacity=(delta < 0)?max:0;
   the_box.style.visibility='visible';
   do_fade(delta);
}

function do_fade(delta) {
   opacity=opacity+delta;
   opacity=(opacity>max)?max:opacity;
   opacity=(opacity<0)?0:opacity;
   if (delta < 0) {
      if(on_target) { // change fade direction
         do_fade(-delta);
         return;
      }
      if(opacity == 0) {
         the_box.style.visibility='hidden';
         return;
      }
   }
   else { // delta > 0
      if(!on_target) { // change fade direction
         do_fade(-delta);
         return;
      }
      if(opacity == max) {
         return;
      }
   }
   // if we fall throught to here, then carry on fading
   // this line is for MSIE
   the_box.style.filter='alpha(opacity='+opacity+')';
   // this line is for the others
   the_box.style.opacity=opacity / 90.0;
   clearTimeout(fadeTimer);
   fadeTimer = setTimeout("do_fade("+delta+")",20);
}

