// ****************************************************************************
// This Javascript package creates a floatable iFrame "popup" that can be
// displayed next to a target in response to a mouseover event. The Create() 
// function is typically called on Page Load to create the iFrame, and then
// Show() and Hide() are called to move it into and out of view.
// ****************************************************************************
// Original code came from Jason. I modified it to better position the popup.
// We use this for the ratingsPop, sealPop, and a few random float popups.
// ****************************************************************************

// This is the constructor.
function Popover(id, src, iWidth, iHeight)
{
   this.id = id;
   this.src = src;
   this.width = iWidth;
   this.height = iHeight;
   this.popElem = null;
   this.visible = false;
   this.offScreen = '-5000px'; // arbitrary
}

// This is called OnLoad to create the IFrame.
Popover.prototype.Create = function ()
{
   if (!document.createElement) {return true};
   {
      var tempIFrame=document.createElement('iframe');
      tempIFrame.setAttribute('id',this.id);
      tempIFrame.setAttribute('src',this.src);
      tempIFrame.setAttribute('scrolling','no');
      tempIFrame.setAttribute('marginwidth','0');
      tempIFrame.setAttribute('frameborder','0');
      tempIFrame.style.border='none';
      tempIFrame.style.position='absolute';
      tempIFrame.style.width= this.width + 'px';
      tempIFrame.style.height= this.height + 'px';
      tempIFrame.style.top= this.offScreen;
      tempIFrame.style.left= this.offScreen;
      this.popElem = document.body.appendChild(tempIFrame);
   }
}

// This is called to display the popup.
Popover.prototype.Show = function(event,parms)
{
   var fullpath = this.src + parms;
   if (this.popElem.getAttribute('src')!=fullpath)
   {
      // We need to change out the window.
      this.visible=false
      this.popElem.setAttribute('src',fullpath);
   }
   if (! this.visible)
   {
      // Two different ways to get at the IFRAME document, based on the NS6 and IE DOM's.
      var frameDocument = null;
      if (document.frames)  // IE
         frameDocument = document.frames(this.id).document;
      else  // NS6
         frameDocument = this.popElem.contentDocument;
         
      var me = this; this.popElem.onmouseout = function () { me.Hide(); };

      var left = 0; 
      var top  = 0; 

      // (NOTE: event.target is NS6 and event.scrElement is IE.)
      if ( (event.target) || (event.srcElement) )
      {
         var target = event.target ? event.target : event.srcElement;
         // Walk the document tree to figure out where the click landed.
         do 
         {
            left = left + target.offsetLeft;
            top  = top + target.offsetTop;
            target = target.offsetParent;
         }
         while (target.offsetParent);

         if (left < target.clientWidth/2) // Pop Right
            left = left; // + (event.target ? event.target : event.srcElement).width;
         else // Pop Left
            left = left - this.popElem.offsetWidth;

//       if (top < target.clientHeight/2) // Pop Down
//         top = top + (event.target ? event.target : event.srcElement).height;
//       else // Pop Up
         top  = top  - this.popElem.offsetHeight;
           
//       left = target.offsetLeft + target.offsetParent.offsetLeft + (target.offsetWidth / 2) - (this.popElem.offsetWidth / 2);
//       top = target.offsetTop + target.offsetParent.offsetTop + (target.offsetHeight / 2) - (this.popElem.offsetHeight / 2);
      }
      this.visible = true;
      this.popElem.style.left = left + 'px';
      this.popElem.style.top = top + 'px';
   }
}

// This is called to hide the popup.
Popover.prototype.Hide = function()
{
   if (this.visible)
   {
      this.visible = false
      this.popElem.style.top = this.offScreen;
      this.popElem.style.left = this.offScreen;
   }
}
