﻿jQuery.bubble = function(object) {
	// Create a link to self
	var me = this;
	
	var $object = $(object);
	var timeout = null;
	
	//create the container for the bubble
	var bubblecontainer = document.createElement("div");
	var $bubblecontainer = $(bubblecontainer);
	
	$bubblecontainer.html("hide");
	$bubblecontainer.hide().addClass("BubbleContainer").css("position", "absolute");
	
	$("body").prepend(bubblecontainer);
	
	$object
	.mousemove(function (e) {
    	//only move the bubble if it is visible
        movebubble(e.pageX, e.pageY);
	})
	.mouseout(function (e) {
	    clearTimeout(timeout);
	    hidebubble();
	})
	.mouseover(function (e) {
	    //display the bubble after x millisec
	    if (timeout) clearTimeout(timeout);
	    timeout = setTimeout(function(){displaybubble();}, 0);
	})
	
	function displaybubble()
	{
	    var ajaxurl = $object.attr("ajaxurl");
	    
	    //first show the bubble
	    $bubblecontainer.show(100);
	    
	    //display the loading data
	    $bubblecontainer.html('<img src="/images/icons/ajax-loader.gif" class="Loader"/>');
	    
	    if (ajaxurl != null)
	    {
            $.get(ajaxurl, function(data){
                setTimeout(function(){$bubblecontainer.html(data);}, 500);
            });
	    }

	}
	function hidebubble()
	{
	    $bubblecontainer.hide(100);
	}

    function movebubble(x,y)
    {	    
        var mouseX = x;
        var mouseY = y;   

        $bubblecontainer.css('top',(y+20)+'px');
        $bubblecontainer.css('left',(x+5)+'px');
    }
}

jQuery.fn.bubble = function() {
	this.each(function() {
		var object = this;
		new jQuery.bubble(object);
	});
	return this;
}
