$(function()
{
    var hideDelay = 500;
    var currentID;
    var hideTimer = null;
    var ajax = null;
    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="infoPopupContainer">'
        + '<table width="" border="0" cellspacing="0" cellpadding="0" align="center" class="infoPopupPopup">'
        + '<tr>'
        + '   <td class="topLeft">&nbsp;</td>'
        + '   <td class="top"><div id="infoPopupContent"></div></td>'
        + '   <td class="topRight">&nbsp;</td>'
        + '</tr>'
        + '<tr>'
        + '   <td class="bottomLeft">&nbsp;</td>'
        + '   <td class="bottom">&nbsp;</td>'
        + '   <td class="bottomRight"></td>'
        + '</tr>'
        + '</table>'
        + '</div>');

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

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

                    var aHref = $(this).attr('href');
                    if (aHref == '' || aHref == '#')
                        return;

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

                    if (currentPosition.left == reposition.left &&
                        currentPosition.top == reposition.top)
                        return;

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

                    currentPosition = reposition;

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

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

                    ajax = $.ajax({
                        type: 'GET',
                        url: '/infopopup.php',
                        data: 'href=' + aHref,
                        success: function(data)
                        {
                            if (data.indexOf('infoPopupResult') < 0)
                            {
                                $('#infoPopupContent').html('<span style="color:red" class="smallText">Cannot load info popup for "' + aHref + '". Please contact us if error persists.</span>');
                            }

                            if (data.indexOf(aHref) > 0)
                            {
				var text = $(data).find(".infoPopupResult").html();
				if (text != "")
				{
	                                $('#infoPopupContent').html(text);
					container.css('display', 'block');
				}
				else
				{
					container.css('display', 'none');
				}
                            }
                        }
                    });

                    //container.css('display', 'block');
                },
                hideFunction
            );
            $(this).trigger('mouseover');
        }
    });

    $('#infoPopupContainer').mouseover(function()
    {
        if (hideTimer)
            clearTimeout(hideTimer);
    });

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


