/**
 * @author mthornton
 */
(function($){
	$.fn.tooltip = function(options){
		//sets up 2 variables. settings becomes the merged results of defaults and parameters passed in.
		var 
			defaults = {
				background : '#791104',
				color: '#002140',
				rounded: false,
				topOffset : 10,
				leftOffset : 20
			},
			settings = $.extend({}, defaults, options);
			//sets up tooltip function for each item that matches the selector specification on outside function call
			this.each(function(){
				var $this = $(this);
				var unSlicedTitle = this.title;
				var partlySliced = unSlicedTitle.slice(26);
				var title = partlySliced.slice(0,-4);
				if ($this.is('a')  && $this.attr('title') != '' || $this.is('div') && $this.attr('title') != ''){
					this.title = '';
					$this.hover(function(e){
						//mouse over
						//var title = $this.attr('title');
						//appends a div to the body with the text of the tooltip, offsets it based on mouse, and uses settings from above
						$('<div id="tooltip"><span>'+title+'</span></div>')
							.appendTo('body')
							.hide()
							.css({
								backgroundColor: settings.background,
								color: settings.color,
								top: e.pageY + settings.topOffset,
								left: e.pageX + settings.leftOffset
							})
							.fadeIn(350);
							
							if (settings.rounded) {
								$('#tooltip').addClass('rounded');
							}
					},
					function(){
						//mouse out
						$('#tooltip').remove();	
					});
				}
	
				$this.mousemove( function(e){
					$('#tooltip').css({
						top: e.pageY + settings.topOffset,
						left: e.pageX + settings.leftOffset
					})
				});
			});
			//returns the jQuery object, enabling chaining if required
			return this;
	}
})(jQuery);
