// JavaScript object to draw outlines that indicate the functions of an interface
// The contents of this file are (c) 2002 by Sprague Technologies, Inc.
// All rights reserved.  Unauthorized users will be prosecuted.
//

function UIHoverBox(name)
{
  this.m_name = new String(name);
  this.m_boxes = new Array;
  this.m_box=null;
  this.m_pointer=null;
  this.m_desc=null;
  this.m_left = -1;
  this.m_top = -1;
  this.m_boxColor = "#ffff00";
  this.m_boxVisible = false;
  this.m_boxBlink = false;

  this.m_boxPos = [ 0,0,1,1,""];
  this.m_boxPosDesired = null;
  this.m_currentPageNum = 0;
  this.m_currentPage = 1;
  this.m_descriptionWidth = 250;
  this.m_descriptionOffset = 80;
  this.m_imageUrls = null;
}
UIHoverBox.prototype.constructor = UIHoverBox;

function findAbsOffsetLeft( obj )
{
  if ( null == obj || "" == obj )
  {
    //alert( "findAbsOffsetLeft: leaving upon encountering "+obj );
    return 0;
  }
  
  //alert( "findAbsOffsetLeft: recursing for "+obj.offsetparent );
  var par = obj.offsetParent;
  var l = obj.offsetLeft;
  if ( par != null )  
    l += findAbsOffsetLeft( obj.offsetParent );
  return l;
}

function findAbsOffsetTop( obj )
{
  if ( null == obj || "" == obj )
    return 0;
  return findAbsOffsetTop( obj.offsetParent ) + obj.offsetTop;
}

function findObject(o)
{
  var obj = document.all[o];
  if ( null == obj || "undefined" == String(obj) )
    alert( o + " not found!" );

  return obj;
}

function UIHoverBox_Init()
{
  //alert( style );
  //alert( "abs pos:"+m_left+","+m_top );

  this.m_box = findObject(this.m_name+"divBox");
  this.m_pointer = findObject(this.m_name+"divPointer");
  this.m_desc = findObject(this.m_name+"divDescription");

  if ( null != this.m_box && "" != this.m_box )
  {
    setInterval( this.m_name+".blinkBox();", 500 );
    setInterval( this.m_name+".updateBoxPosition();", 50 );
  }
}
UIHoverBox.prototype.Init = UIHoverBox_Init;

function UIHoverBox_imgLoad( i )
{
  this.m_img = i;
  this.m_left = findAbsOffsetLeft( i );
  this.m_top = findAbsOffsetTop( i );
}
UIHoverBox.prototype.imgLoad = UIHoverBox_imgLoad;

function UIHoverBox_ChangePage(i)
{
  if ( this.m_imageUrls != null && i >= 0 && i < this.m_imageUrls.length )
  {
    this.m_currentPageNum = i;
    this.m_currentPage = 1<<i;
    this.m_img.src = this.m_imageUrls[i];
  }

}
UIHoverBox.prototype.ChangePage = UIHoverBox_ChangePage;

function UIHoverBox_EmitHtml(img)
{
  this.m_imageUrls = arguments;
  var strHtml = '<IMG ID="'+this.m_name+'Image" SRC="'+img+'" onMouseMove="'+this.m_name+'.findBox();" onMouseOut="'+this.m_name+'.hideBox();" onLoad="'+this.m_name+'.imgLoad(this);" STYLE="z-Index:50;"><DIV '+
            'ID="'+this.m_name+'divBox" STYLE="font-size:5; left: 0px, top:0px; height:0px; width: 0px; border: 2px solid; color: '+this.m_boxColor+'; z-Index:100; position:absolute; visibility:hidden;"> </DIV><DIV '+
            'ID="'+this.m_name+'divPointer" STYLE="font-size:1; left: 0px, top:0px; height:0px; width: 2px; border: 2px solid; background-color: '+this.m_boxColor+'; color: '+this.m_boxColor+'; z-Index:100; position:absolute; visibility:hidden;"> </DIV><DIV '+
            'ID="'+this.m_name+'divDescription" CLASS="description"> </DIV>';

  //alert( strHtml );

  document.write(strHtml);
}

UIHoverBox.prototype.EmitHtml = UIHoverBox_EmitHtml;

function UIHoverBox_showBox( i )
{
  if ( null == this.m_box ||
       null == this.m_pointer || 
       null == this.m_desc )
  {
    //alert( "ShowBox: not enough info: "+this.m_box+","+this.m_pointer +","+this.m_desc  );
    return;
  }

  var box = this.m_boxes[i];
  if ( this.m_boxVisible && null != this.m_boxPosDesired &&
       box[0] == this.m_boxPosDesired[0] && box[1] == this.m_boxPosDesired[1] &&
       box[2] == this.m_boxPosDesired[2] && box[3] == this.m_boxPosDesired[3] )
  {
    //alert( "ShowBox: already moving there" );
    return; // already moving there
  }

  if ( this.m_boxVisible && null != this.m_boxPos &&
       box[0] == this.m_boxPos[0] && box[1] == this.m_boxPos[1] &&
       box[2] == this.m_boxPos[2] && box[3] == this.m_boxPos[3] )
  {
    //alert( "ShowBox: already there" );
    return; // already there
  }


  this.m_boxPosDesired = box;
  if ( null == this.m_boxPosDesired || "" == this.m_boxPosDesired )
  {
    //alert( "ShowBox: no desired box" );
    return;
  }
  this.m_boxVisible = true;
  this.m_box.style.visibility = 'visible';
  this.m_pointer.style.visibility = 'visible';
  this.m_desc.innerText = this.m_boxPosDesired[4];
  this.m_desc.style.visibility = 'visible';
}
UIHoverBox.prototype.showBox = UIHoverBox_showBox;

function UIHoverBox_copyArray( a )
{
  var b = new Array( a.length );
  var i;
  for ( i=0; i<a.length; i++ )
  {
    b[i] = a[i];
  }
  return b;
}
UIHoverBox.prototype.copyArray = UIHoverBox_copyArray;

function UIHoverBox_updateBoxPosition()
{
  if ( null == this.m_boxPosDesired )
    return;
  if ( null == this.m_boxPos )
  {
    this.m_boxPos = copyArray( this.m_boxPosDesired );
  } else
  {
    var i;
    for ( i=0; i<4; i++ )
    {
      var extra = i<2 ? 1 : 0;
      this.m_boxPos[i] = (extra+this.m_boxPos[i]+this.m_boxPosDesired[i])/2;
      if ( Math.abs(this.m_boxPos[i] - this.m_boxPosDesired[i]) <= 3-extra )
        this.m_boxPos[i] = this.m_boxPosDesired[i];
    }
  }

  this.m_box.style.pixelLeft = this.m_left+this.m_boxPos[0]-2;
  this.m_box.style.pixelTop = this.m_top+this.m_boxPos[1]-2;
  this.m_box.style.pixelWidth=this.m_boxPos[2]+4;
  this.m_box.style.pixelHeight=this.m_boxPos[3]+4;

// box is above everything
//  this.m_pointer.style.pixelLeft = this.m_box.style.pixelLeft;
//  this.m_pointer.style.pixelTop = this.m_top-this.m_descriptionOffset;
//  this.m_pointer.style.pixelWidth=2;
//  this.m_pointer.style.pixelHeight=this.m_box.style.pixelTop-this.m_pointer.style.pixelTop;
//  this.m_desc.style.pixelTop = this.m_pointer.style.pixelTop-20;
//  this.m_desc.style.pixelLeft = this.m_left-5;
//  this.m_desc.style.pixelHeight = 20;
//  this.m_desc.style.pixelWidth = this.m_descriptionWidth;

// box is right of everything
  this.m_pointer.style.pixelLeft = this.m_box.style.pixelLeft+this.m_box.style.pixelWidth;
  this.m_pointer.style.pixelTop = this.m_box.style.pixelTop;
  this.m_pointer.style.pixelWidth=40;
  this.m_pointer.style.pixelHeight=0;

  this.m_desc.style.pixelTop = this.m_pointer.style.pixelTop;
  this.m_desc.style.pixelLeft = this.m_pointer.style.pixelLeft+this.m_pointer.style.pixelWidth;
  this.m_desc.style.pixelHeight = 20;
  this.m_desc.style.pixelWidth = this.m_descriptionWidth;

  if ( this.m_boxPos[0] == this.m_boxPosDesired[0] &&
       this.m_boxPos[1] == this.m_boxPosDesired[1] &&
       this.m_boxPos[2] == this.m_boxPosDesired[2] &&
       this.m_boxPos[3] == this.m_boxPosDesired[3] )
  {
    // we're there, so change things so stuff will stop animating.
    this.m_boxPos = this.copyArray( this.m_boxPosDesired );
    this.m_boxPosDesired = null;
  }
  
}
UIHoverBox.prototype.updateBoxPosition = UIHoverBox_updateBoxPosition;

function UIHoverBox_timerHideStuff()
{
  if ( !this.m_boxVisible )
  {
    if ( null != this.m_box )
      this.m_box.style.visibility='hidden';
    if ( null != this.m_pointer )
      this.m_pointer.style.visibility='hidden';
    if ( null != this.m_desc )
      this.m_desc.style.visibility='hidden';
  }
}
UIHoverBox.prototype.timerHideStuff = UIHoverBox_timerHideStuff;

function UIHoverBox_hideBox()
{
  if ( this.m_boxVisible )
  {
    this.m_boxVisible = false;
    setTimeout( this.m_name+".timerHideStuff();", 500 );
  }
}
UIHoverBox.prototype.hideBox = UIHoverBox_hideBox;

function UIHoverBox_findBox()
{
  var x = window.event.offsetX;
  var y = window.event.offsetY;

  var i;
  for ( i=0; i<this.m_boxes.length; i++ )
  {
    var box = this.m_boxes[i];
    if ( x >= box[0] && x < box[0]+box[2] &&
         y >= box[1] && y < box[1]+box[3]  &&
         ( (box.length < 6) || (0 != (this.m_currentPage & box[5])) ) 
       )
    {
      this.showBox(i);
      return;
    }
  }
  this.hideBox();
  //alert( "Box not found: ("+x+","+y+") in list of boxes "+this.m_boxes.length );
}
UIHoverBox.prototype.findBox = UIHoverBox_findBox;

function UIHoverBox_blinkBox()
{
  if ( !this.m_boxVisible )
    return;

  this.m_box.style.color = this.m_boxColor;
  this.m_pointer.style.color = this.m_boxColor;
  if ( this.m_boxPosDesired != null ) // we're moving the box, so keep it visible until it's at the destination
  {
    return;
  }
  this.m_boxBlink = !this.m_boxBlink;

  this.m_box.style.visibility = this.m_boxBlink ? 'visible' : 'hidden';
  this.m_pointer.style.visibility = this.m_boxBlink ? 'visible' : 'hidden';
}

UIHoverBox.prototype.blinkBox = UIHoverBox_blinkBox;
