(function($){
	$.fn.tooltip = function( options ) {
		var settings = {
			    contentSelector: ''
		    };
		
		if ( options ) $.extend( settings, options );
		
		return this.each( function(){
			var $this = $(this);

			$this['tiptext'] = '' !== settings.contentSelector ? 
			          $this.find( settings.contentSelector ).html() : 
				      $this.attr( 'title' )
			// remove so we don't have double tips
			$this.removeAttr( 'title' );
			$this.find( '[title]' ).removeAttr( 'title' );
			$this.find( '[alt]' ).removeAttr( 'alt' );
			          
			$this.mouseover(function(e){
				var x = e.pageX + 10,
				    y = e.pageY + 10;
				
				if ( x + 400 > $(document).width() ){
					x -= 300;
				}
				
				$('body').append( ['<div class="tooltip-box" style="position:absolute;left:',
				                 x,
				                 'px;top:',
				                 y,
				                 'px;">', $this.tiptext, '</div>'].join('') );
			});
			
			$this.mousemove(function(e){
			    var x = e.pageX + 10,
			        y = e.pageY + 10;
			
				if ( x + 400 > $(document).width() ){
					x -= 300;
				}
				
				if ( y + 400 > $(document).width() ){
					y -= 300;
				}
				
				$('.tooltip-box').css( 'left', x + 'px' );
				$('.tooltip-box').css( 'top', y + 'px' );
			});
			
			$this.mouseout(function(e){
				$('.tooltip-box').remove();
			});
		});
	}
})(jQuery);

