$(function()
{
  var hideDelay = 500;  
  var currentID;
  var hideTimer = null;

  // One instance that's reused to show info for the current person
  var container = jQuery('<div id="ttContainer">'
      + '<table width="" border="0" cellspacing="0" cellpadding="0" align="center" class="ttPopup">'
      + '<tr>'
      + '   <td class="corner topLeft"></td>'
      + '   <td class="top"></td>'
      + '   <td class="corner topRight"></td>'
      + '</tr>'
      + '<tr>'
      + '   <td class="left">&nbsp;</td>'
      + '   <td><div id="ttContent"></div></td>'
      + '   <td class="right">&nbsp;</td>'
      + '</tr>'
      + '<tr>'
      + '   <td class="corner bottomLeft">&nbsp;</td>'
      + '   <td class="bottom">&nbsp;</td>'
      + '   <td class="corner bottomRight"></td>'
      + '</tr>'
      + '</table>'
      + '</div>');

  jQuery('body').append(container);

  jQuery('.ttTrigger').live('mouseover', function()
  {
      // format of 'rel' tag: pageid,personguid
      var settings = jQuery(this).attr('rel').split(',');
      var pageID = settings[0];
      currentID = settings[1];

      // If no guid in url rel tag, don't popup blank
      if (currentID == '')
          return;

      if (hideTimer)
          clearTimeout(hideTimer);

      var pos = jQuery(this).offset();
      var width = jQuery(this).width();
      container.css({
          left: (pos.left + width) + 'px',
          top: pos.top - 5 + 'px'
      });

      jQuery('#ttContent').html('&nbsp;');

      jQuery.ajax({
          type: 'GET',
          url: '/cms/forms/ttip.php',
          data: 'page=' + pageID + '&name=' + currentID,
          success: function(data)
          
          {

              // Verify that we're pointed to a page that returned the expected results.

              // Verify requested person is this person since we could have multiple ajax
              // requests out if the server is taking a while.
                 
                  var text = jQuery(data).find('.ttResult').html();
                  jQuery('#ttContent').html(text);
              
          }
      });

      container.css('display', 'block');
  });

  jQuery('.ttTrigger').live('mouseout', function()
  {
      if (hideTimer)
          clearTimeout(hideTimer);
      hideTimer = setTimeout(function()
      {
          container.css('display', 'none');
      }, hideDelay);
  });

  // Allow mouse over of details without hiding details
  jQuery('#ttContainer').mouseover(function()
  {
      if (hideTimer)
          clearTimeout(hideTimer);
  });

  // Hide after mouseout
  jQuery('#ttContainer').mouseout(function()
  {
      if (hideTimer)
          clearTimeout(hideTimer);
      hideTimer = setTimeout(function()
      {
          container.css('display', 'none');
      }, hideDelay);
  });
});
