jQuery.fn.extend({
	popTip: function( options ) {
		return this.each(function() {
			new jQuery.popTip( this, options );
		});
	}
});
jQuery.popTip = function( el, options )
{
	//	Define defaults.
	var timer;
	var defTitle	= $(el).attr('title');
	var url			= $(el).attr('href');
	defaults = {
		xAdjust		: 10,
		yAdjust		: 10,
		opacity		: 0.85,
		inDelay		: 0,
		outDelay	: 0,
		inSpeed		: 'fast',
		outSpeed	: 'fast',
		showURL		: true,
		className	: 'tooltip'
	};
	
	options = $.extend(defaults, options);

	//	Create our box.
	var box = document.createElement('div');
	
	$(el).bind('mouseover', function( event ) {
		
		timer = clearTimeout(timer);
		//	Empty title tag to stop browser tips.
		$(this).attr('title','');
		
		//	Append box and box contents.
		$('body').append(box);
		$(box).html('<p>'+ defTitle +'</p>');
		if( options.showURL == true )
			$(box).append('<em>'+ url +'</em>');
		
		//	Add box class.
		$(box).addClass( options.className );
		
		//	Position and display box.
		timer = setTimeout( function(){
			var x = event.pageX, y = event.pageY;
			x = ( options.x == 'center' ) ? (x - ($(box).width() / 2)) : (x + options.x);
			y = ( options.y == 'above' ) ? (y - $(box).height() - $(el).height() - 15) : (y + options.y);
			var outeredge = x + $(box).width(), bodywidth = $('body').width();
			x = ( x < 0 ) ? 0 : ( ( outeredge > bodywidth ) ? x - (outeredge - bodywidth) : x );
			y = ( y<  0 ) ? 0 : y;
			
			$(box).css({'position':'absolute', 'opacity':options.opacity, 'left':x, 'top':y});

			$(box).stop().fadeIn( options.inSpeed );
		}
		, options.inDelay );
		
	}).bind('mouseout', function(e) {
		//	Put title back.
		$(this).attr('title', defTitle );
		
		//	Hide box.
		timer = clearTimeout(timer);
		timer = setTimeout( function() {
			$(box).stop().fadeOut( options.outSpeed, function(){ 
				$(this).remove();
			});
			timer = clearTimeout(timer);
		}, options.outDelay );
	});
}