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

  // One instance that's reused to show info for the current person
  var container = $('<div id="personPopupContainer"><div id="personPopupContent"></div>');

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

  $('.trigger').live('mouseover', function()
  {
      // format of 'rel' tag: pageid,personguid
      var id = $(this).attr('memberid');

      // If no guid in url rel tag, don't popup blank

      if (hideTimer)
          clearTimeout(hideTimer);

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

      $('#personPopupContent').html('&nbsp;');

      $.ajax({
          type: 'GET',
          url: 'get.php',
          data: 'id=' + id,
          success: function(data)
          {
                  var text = data;
                  $('#personPopupContent').html(text);
          }
      });

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

  $('.trigger').live('mouseout', function()
  {
      if (hideTimer)
          clearTimeout(hideTimer);
      hideTimer = setTimeout(function()
      {
          container.fadeOut(0);
      }, hideDelay);
  });

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

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