(function($){
	$.fn.GlobalToolTip = function ( tooltip , callback , area )
	{
		//hide the tooltip:
		$(tooltip).hide();
		
		//Next, create the global parameters:
		var global_tool_tip = new GlobalToolTip( tooltip , callback , area );
		
		//Finally, bind onMouseEnterToolTip:
		return this.each( function()
		{
			this.params = global_tool_tip;
			$(this).bind( 'mouseenter' , {params : global_tool_tip , object : this } , onMouseEnterTarget );
		});
	};
})(jQuery);

function GlobalToolTip( tooltip , callback , area )
{
	//properties:
	this.tooltip = tooltip; //Holds the DOM element of the actual tooltip.
	this.callback = callback; //Holds the function that will activate when a tooltip is to be shown.
	this.is_active; //Holds whether or not the tooltip is currently being shown.
	this.timer = new Timer();	//Timer for events.
	this.area = area;			//holds the DOM element of the actual area.
	
};

function onMouseEnterTarget( event )
{
	//This function shall be called by a binded eventhandler - 
	//it will have the following information:
	//event.data.params -> this is a GlobalToolTip Object.
	//event.data.object -> this is a reference to the actual object that was clicked.
	
	//No matter what, change tool tip:
	changeToolTip( event.data.object , event.data.params.tooltip , event.data.params.callback );
	//$(event.data.params.tooltip).css( { left : event.pageX + 'px' , top : event.pageY + 'px' } );
	
	//Check to see if the tooltip is active:
	if( !event.data.params.is_active )
	{
		//if not already active, we must start the timer:
		event.data.params.timer.setTimeout( "showToolTip( '" + event.data.params.tooltip.id + "' , " + event.pageX + " , " + event.pageY + " )" , 200 );	
		//event.data.params.timer.setTimeout( "checkForMouseHover( " + event.data.object.id + " , " + event.data.params.tooltip.id + " )" , 300 );
		
		$(event.data.object).bind( 'mouseleave' , { object: event.data.object , params: event.data.params } , stopTimeout );
		event.data.params.is_active = true;
		$(event.data.object).bind( 'mousemove' , { tooltip : event.data.params.tooltip } , moveToolTipHandler );

		//Finally, set an event listener for when the mouse moves out of the parent:
		$(event.data.object.parentNode).bind( 'mouseleave' , { object: event.data.object , params : event.data.params } , hideToolTip );
	};
};

function moveToolTipHandler( event )
{
	moveToolTip( event.data.tooltip , event.pageX , event.pageY );
	return;
};

function moveToolTip( tooltip , x , y )
{
	if( typeof tooltip == 'string' )
	{
		tooltip = $('#' + tooltip)[0];
	};

	if( tooltip.offsetWidth + x + 50 < screen.availWidth )
	{
		$(tooltip).css( { left : ( x - 200 ) + 'px' } );
	}
	else
	{
		//$(tooltip).css( { left : ( screen.availWidth - tooltip.offsetWidth - 25 ) + 'px' } );
		$(tooltip).css( { left : ( x - 200 ) + 'px' } );
	};
	/*
	if( tooltip.offsetHeight + y + 50 < screen.availHeight )
	{
		//alert( (tooltip.offsetHeight + y + 50) + " < " + screen.availHeight );
		$(tooltip).css( { top : (y + 25) + 'px' } );
	}
	else
	{
		//alert( (tooltip.offsetHeight + y + 50) + " > " + screen.availHeight );
		$(tooltip).css( { top : ( y - tooltip.offsetHeight - 25 ) + 'px' } );
	};
	*/
	
	$(tooltip).css( { top : (y + 25) + 'px' } );
	
	return;
};

function stopTimeout( event )
{
	event.data.params.timer.stop();
	event.data.params.is_active = false;
	$(event.data.object).unbind( 'mousemove' ,  moveToolTipHandler );
	$(event.data.object).unbind( 'mouseleave' , stopTimeout );
	return;
};

function changeToolTip( object , tooltip , callback )
{
	//This function shall be called by a binded eventhandler - 
	//it will have the following information:
	//event.data.params -> this is a GlobalToolTip Object.
	//event.data.object -> this is a reference to the actual object that was clicked.
		
	callback( object , tooltip );
	
	return;
};

function showToolTip( tooltip , x , y )
{
	if( typeof tooltip == 'string' )
	{
		$('#' + tooltip).show();
	}
	else
	{
		$(tooltip).show();
	}
	
	moveToolTip( tooltip , x , y );
	
	return;
};

function hideToolTip( event )
{
	$(event.data.params.tooltip).hide();
	event.data.params.is_active = false;
	$(event.data.object).unbind( 'mousemove' ,  moveToolTipHandler );
	return;
};

//Callback function:
/*
function callback( object , tooltip )
{
	//object refers to the DOM element that was registered for the action.
	//tooltip holds a reference to the tooltip DOM element.
	
	return true; //Means to show the tooltip;
	return false; //Means DONT show the tooltip;
};
*/