(function($)
{
 	$.fn.tooltip = function(options) 
 	{

		//Set the default values, use comma to separate the settings, example:
		var defaults = {
			hideDelay: 1000,
			elements : '.tooltip',
			ajax_type : 'GET',
			ajax_url : null,
			loading_image: '/images/ajax-loader.gif'
		}

		var options =  $.extend(defaults, options);
		
	    var hideDelay = options.hideDelay;
	    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 ajaxCall = function(ajax_url)
	    {
	    	
            if (ajax)
            {
                ajax.abort();
                ajax = null;
            }

            ajax = $.ajax({
                type: options.ajax_type,
                url: ajax_url,
                data: 'page=',
                success: function(data)
                {
                	var text = ajax_url;
                	$('#tooltipPopupContent').html(data);
                }
            });	    	
	    };

	    $(options.elements).click(function(event) {
	    	event.preventDefault();
	    });
	    
	    var currentPosition = { left: '0px', top: '0px' };

	    // One instance that's reused to show info for the current person
	    var container = $('<div id="tooltipPopupContainer">'
	        + '<table width="" border="0" cellspacing="0" cellpadding="0" align="center" class="tooltipPopupPopup">'
	        + '<tr>'
	        + '   <td class="top"></td>'
	        + '</tr>'
	        + '<tr>'
	        + '   <td class="middle"><div id="tooltipPopupContent"></div></td>'
	        + '</tr>'
	        + '<tr>'
	        + '   <td class="bottom">&nbsp;</td>'
	        + '</tr>'
	        + '</table>'
	        + '</div>');

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

		return this.each(function() {
			var o = options;

		    $(options.elements).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
		                    var ajax_url = $(this).attr('href');
		                    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;

		                    $('#tooltipPopupContent').html("<img id='tooltipLoadingImage' src='"+options.loading_image+"'/>");

		                    ajaxCall(ajax_url);

		                    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
		    $('#tooltipPopupContainer').mouseover(function()
		    {
		        if (hideTimer)
		            clearTimeout(hideTimer);
		    });

		    // Hide after mouseout
		    $('#tooltipPopupContainer').mouseout(hideFunction);				
			
			//code to be inserted here
			//you can access the value like this
		
		});
	}
	
})(jQuery);


