(function($){
	$.fn.tooltip = function(options) {
		
		var
		  defaults = {
			addclass: '',
			title: '',
			showon: 'mouse',
			get_attr: '',
			position: 'bottom',
			posX: 0,
			posY: 0		
		  },
		  settings = $.extend({}, defaults, options);
		  
		  this.each(function() {
		  	var $this = $(this);
			var title = settings.title;
			if( settings.get_attr != '' ){
				title = $this.attr(settings.get_attr);
			}
			
			if(title != '') {
				$this.hover(function(e) {
					// mouse over
					var insert_tooltip = 	'<div id="tooltip">'+
											'<span class="ui-icons"></span>'+
											'<div class="tiptext">'+title+'</div>'+
											'</div>';
					$('body').append(insert_tooltip);
					showOn();
					$('#tooltip').fadeIn(150);
					  
				  if(settings.addclass) {
				  	$('#tooltip').addClass(settings.addclass);
				  }
				}, function() {
					// mouse out
					$('#tooltip').remove();
				});	
			}
			
			// Accion ShowOn
			function showOn (){
				
				if (settings.showon == 'mouse') {
					$this.mousemove(function(e){
						$('#tooltip').css({
							top: e.pageY + settings.posY,
							left: e.pageX + settings.posX
						});
					});
				}
				
				if (settings.showon == 'fixed') {
					var offset = $this.offset(),
						this_w = $this.width (),
						this_h = $this.height(),
						tool_w = $('#tooltip').width (),
						tool_h = $('#tooltip').height(),
						top_pos, left_pos;
						
					if (settings.position == 'bottom'){
						left_pos = offset.left;
						top_pos = offset.top + (this_h+tool_h);
					}
					
					if (settings.position == 'top'){
						left_pos = 0;
						top_pos = offset.top - (this_h+tool_h);
					}
					
					$('#tooltip').css({
						top: top_pos + settings.posY,
						left: left_pos + settings.posX
					});
				}
				
			}
			
			
		  });

		  return this;
	}
})(jQuery);
