$(function()
{
    var hideDelay = 50;
    var currentID;
    var hideTimer = true;
    var ajax = null;
    var method;
    var hideFunction = function()
    {
    	
        if (hideTimer)
            clearTimeout(hideTimer);
        hideTimer = setTimeout(function()
        {
            currentPosition = { left: '0px', top: '0px' };
            container.css('display', 'none');
        }, hideDelay);
    };

    var currentPosition = { left: '0px', top: '0px' };

    // One instance that's reused to show info for the current person
    var container = $('<div id="popupContainer">'
        + '<table width="" border="0" cellspacing="0" cellpadding="0" align="center" class="personPopupPopup">'
        + '<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="popupContent"></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>');

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

    $('.popupTrigger').live('mouseover', function()
    {
    	
        if (!$(this).data('hoverIntentAttached'))
        {
            $(this).data('hoverIntentAttached', true);
            $(this).hoverIntent
            (
            		
                // hoverIntent mouseOver
                function()
                {
                
                    if (hideTimer)
                        clearTimeout(hideTimer);

                    // format of 'rel' tag: pageid,personguid
                    currentID = $(this).attr('rel');
                    method= $(this).attr('method');
                    // If no guid in url rel tag, don't popup blank
                    if (currentID == '')
                        return;

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

                    // If the same popup is already shown, then don't requery
                    if (currentPosition.left == reposition.left &&
                        currentPosition.top == reposition.top)
                        return;

                    container.css({
                        left: reposition.left,
                        top: reposition.top
                    });

                    currentPosition = reposition;

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

                    if (ajax)
                    {
                        ajax.abort();
                        ajax = null;
                    }

                    $.ajax({
                        type: 'GET',
                        url: 'ajax.php?action=ajax_actions/tree_functions',
         
                        data: '&method=get_tree_hover&&user=' + currentID,
                        beforeSend:function(){
                        $('#popupContent').html(loadingStr);
                    },

                        success: function(data)
                        {

                    	
                  	  $('#popupContent').html(data);

                        }
                    });
                    container.css('display', 'block');
                },
                // hoverIntent mouseOut
                hideFunction
            );
            // Fire mouseover so hoverIntent can start doing its magic
            $(this).trigger('mouseover');
        }
    });

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

    // Hide after mouseout
    $('#popupContainer').mouseout(hideFunction);
});
