/**
 * @author MarquesNew7
 */
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text =arr;
	}
	return dumped_text;
}



$(function()
{
  var hideDelay = 500;  
  var eqID;
  var hideTimer = null;

  // One instance that's reused to show info for the current person
  var container = $('<div id="personPopupContainer" >'
      + '<table width="" border="0" cellspacing="0" cellpadding="0" align="center" class="personPopupPopup">'
	  + '<tr>'
    
      + '   <td class="top"></td>'
     
      + '</tr>'
      + '<tr>'
    
      + '   <td id="personPopupContent"></td>'
      
      + '</tr>'
      + '<tr>'
     
      + '   <td class="bottom">&nbsp;</td>'
    
      + '</tr>'
      + '</table>'
      + '</div>');

  $('body').append(container);

  $('.personPopupTrigger').live('mouseover', function()
  {
      // format of 'rel' tag: pageid,personguid
      var settings = $(this).attr('rel').split(',');
      var eqID = settings[0];
      

      // If no guid in url rel tag, don't popup blank
      if (eqID == '')
          return;

      if (hideTimer)
          clearTimeout(hideTimer);
		
	
		
      var pos = $(this).offset();
	      var width = $(this).width();
		  var w = $(this).width();
			//alert (w);
			var leftoff = 0;
			switch(w) {
				case 632:
					leftoff = -340;
					break;
				//chrome one
				case 635:
					leftoff = -292;
					break;
				case 636:
					leftoff = -292;
					break;
				case 637: 
					leftoff = -292;
					break;
				case 638: 
					leftoff = -292;
					break;
				case 639:
	    			leftoff = -292;
					break;
				case 640:
					leftoff = -292;
					break;
				case 644: 
				case 645: 
				case 647:
	    			leftoff = -291;
					break;
				case 648:
					leftoff = -291;
					break;
				case 649:
					leftoff = -291;
					break;
				case 650:
					leftoff = -294;
					break;
				case 651:
					leftoff = -294;
					break;
				case 652:
					leftoff = -294;
					break;
				case 653: 
					leftoff = -294;
					break;
				case 654:
					leftoff = -294;
					break;
				case 655:
					leftoff = -292;
					break;
				case 656:
					leftoff = -292;
					break;
				case 657:
					leftoff = -292;
					break;
				case 658:
					leftoff = -292;
					break;
			}
	      container.css({
          left: (pos.left + leftoff) + 'px',
          top: pos.top - 5 + 'px'
      });

      $('#personPopupContent').html('&nbsp;');
	$.ajaxSetup({
		cache:false
	});
	 $('#personPopupContent').html('<p align="center"><img src="/includes/images/ajax-loader.gif"  /></p>');

      $.ajax({
          type: 'GET',
          url: '/includes/thumbnails.php',
          data: 'eq_ID=' + eqID + '&sid=' + Math.random(),
          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 ' + eqID + ' did not return a valid result for person ' + eqID + '.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(eqID) > 0)
              {                  
                  var text = $(data).find('.personPopupResult').html();
				 
				var g_text = dump(data);
                  $('#personPopupContent').html(g_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);
  });
});
