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

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

  $('.personPopupTrigger').live('mouseover', function()
  {
      // format of 'rel' tag: pageid,personguid,locale,country,cpath
      var settings = $(this).attr('rel').split(',');
      var pageID = settings[0];
      currentID = settings[1];
      locale = settings[2];
 	  country= settings[3];
 	  cpath= settings[4];
 	  
      // If no guid in url rel tag, don't popup blank
      if (currentID == '')
          return;

      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: '/includes/buildingPopup.cfm',
          data: 'bid=' + pageID + '&guid=' + currentID +'&localecountry=' + locale + '&cpath=' + cpath + '&country=' + country,
          success: function(data)
          {
     
              // Verify that we're pointed to a page that returned the expected results.
              if (data.indexOf('personPopupResult') < 0)
              { 
                   $('#personPopupContent').html('<span >Page ' + pageID + ' did not return a valid result for person ' + currentID + '. Please have your administrator check the error log.</span>');
              }

              // Verify requested person is this person since we could have multiple ajax
              // requests out if the server is taking a while.
             
              if (data.indexOf(currentID) > 0)
              {
               //We won'd get html from 'data' because it is  variable 
                 var text = $(data).find('.personPopupResult').html();
                  $('#personPopupContent').html(text);
              }
          }
      });

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

  $('.personPopupTrigger').live('mouseout', function()
  {
      if (hideTimer)
          clearTimeout(hideTimer);
      hideTimer = setTimeout(function()
      {
          container.css('display', 'none');
      }, 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);
  });
});
