// jQuery tooltip plugin
// Author: Stefan Moscarda
// Newmarket Town AB 2009
/*

Usage: $('element').tooltip(options);
  
  For example:
  
 $(document).ready(function() {
	var options = {
		color: '#f00',            								(default:black)
		background: '#fff',		//								(default:transparent)
		x: '10',				// x-offset from mouse pointer	(default: 10)
		y: '10', 				// y-offset		 				(default: 10)
		rounded: 'true'			// Rounded corners 				(default: false)
	}
	
	$('a').tooltip(options);
}
*/
(function($){
		  
		 $.fn.tooltip = function (options){
		 	
			var defaults = {
				x: 10,
				y: 10,
				background: 'transparent',
				color:		'black',
				rounded:	false
			},
			settings = $.extend({}, defaults, options);
			
			// cache $(this)
			this.each (function() {
				var $this = $(this);
				var title = this.title;
				
				
				
				if ($this.is('a') && $this.attr('title') != ''){
					this.title = '';
					
					$this.hover(function(e){
						
						$('<div id="tooltip" />')
						.appendTo('body')
						.hide()
						
						.css({
							backgroundColor: settings.background,
							color: settings.color,
							top: e.pageY + settings.y,
							left: e.pageX + settings.x
						})
						.append('<span>'+title+'</span>')
						.fadeIn(850);
						
						if (settings.rounded) {
							$('#tooltip').addClass('rounded');
						}
		 			}, function() {
						// mouse out
						$('#tooltip').remove();
					});
				}
				
				$this.mousemove(function(e) {
					$('#tooltip').css({
						top: e.pageY + settings.y,
						left: e.pageX + settings.x
					});
				});
			});
			
			return this;
		 }
})(jQuery);