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

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

 

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

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

      if (hideTimer)
          clearTimeout(hideTimer);

      

      $('#personPopupContent').html('&nbsp;<img src="foot/loader.gif" alt="chargement" />');

	  if($(this).hasClass('aideContext'))
	  {
		   $('body').append(container2);
		  
		  var ref = $(this).attr('href');
		  console.log(ref);
		  
		  var pos = $(this).offset();
		  var width = $(this).width();
		  container2.css({
			  left: (pos.left + width) + 'px',
			  top: pos.top + 20 + 'px'
		  });
	  
		  $.ajax({
			  type: 'POST',
				url : '/aideContent.php',
				dataType : 'html',
				data: 'id_aide=' + ref,
			  success: function(data)
			  {
				  // Verify that we're pointed to a page that returned the expected results.
				
					  $('#personPopupContainer2 #personPopupContent').html(data);
				  
			  }
		  });
		  
		  container2.css('display', 'block');
	  }
	  else
	  {
		   $('body').append(container);
		   
		  var pos = $(this).offset();
		  var width = $(this).width();
		  container.css({
			  left: (pos.left + width) + 'px',
			  top: pos.top + 7 + 'px'
		  });
	  
		  $.ajax({
			  type: 'POST',
				url : '/profilContent.php',
				dataType : 'html',
				data: 'joueur=' + pageID,
			  success: function(data)
			  {
				  // Verify that we're pointed to a page that returned the expected results.
				
					  $('#personPopupContainer #personPopupContent').html(data);
				  
			  }
		  });
		  
		  container.css('display', 'block');
	  }

      
  });

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

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

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

  
  
  
});
