(function($){

	$.fn.tooltip = function(options){

		var settings = $.extend({}, $.fn.tooltip.defaultSettings, options);

		return this.each(function(){

			var $this = $(this);

			//-------------------------------------------------------
			// create HTML element that will contain tooltip text
			//-------------------------------------------------------
			var $ttContainer = $("<div>").addClass("jqTooltip");

			//-------------------------------------------------------
			// 1. Make container absolutely positioned and hidden
			// 2. Append to BODY element
			// 3. Add optional class to container
			// 4. Add tooltip text to container
			//-------------------------------------------------------
			$ttContainer.css({
				position: 'absolute',
				display:'none'
			}).appendTo($this.parent()).addClass(settings.cssClass).text(settings.caption);

			//-------------------------------------------------------
			// get height, width, x & y coordinates of $this
			//-------------------------------------------------------
			var x,y;
			var pos = $this.position();
			var xOriginal = x = pos.left;
			var yOriginal = y = pos.top;
			var w = $this.width();
			var h = $this.height();

			//-------------------------------------------------------
			// detect where tooltip should appear relative to $this
			//-------------------------------------------------------
			var c = settings.cardinal.toLowerCase();

			if(c=="n"){
				y = yOriginal - h;
			} else if(c=="e"){
				x = xOriginal + w;
			} else if(c=="s"){
				y = yOriginal + h;
			} else if(c=="w"){
				x = xOriginal - $ttContainer.width();
			} else if(c=="se"){
				y = yOriginal + h;
				x = xOriginal + w;
			} else if(c=="ne"){
				y = yOriginal - h;
				x = xOriginal + w;
			} else if(c=="nw"){
				y = yOriginal - h;
				x = xOriginal - $ttContainer.width();
			} else if(c=="sw"){
				y = yOriginal + h;
				x = xOriginal - $ttContainer.width();
			}

			//-------------------------------------------------------
			// add distance between tooltip and element, or don't
			// depending on fit option value
			//-------------------------------------------------------
			if(settings.fit=="loose"){

				if(c=='n' || c=='ne' || c=='nw'){
					y-=10;
				} else if(c=='s' || c=='se' || c=='sw'){
					y+=10;
				}

				if(c=='e' || c=='ne' || c=='se'){
					x+=10;
				} else if(c=='w' || c=='sw' || c=='nw'){
					x-=10;
				}
			}

			//-------------------------------------------------------
			// add class to container that represents cardinality
			//-------------------------------------------------------
			$ttContainer.addClass("jqToolTip-"+c);

			//-------------------------------------------------------
			// set tooltip settings and append to DOM body
			// the tooltip will fadeOut within 5 seconds
			//-------------------------------------------------------
			$ttContainer.css({top: y, left: x}).fadeIn();

			window.setTimeout("$('.jqTooltip').fadeOut('fast')", settings.duration);
		});

	}


	$.fn.tooltip.defaultSettings = {
		"cssClass" : "tooltip",

		"caption"  : "Tooltip Text",

		"cardinal" : "N",

		"duration" : "5000",

		"fit"	: "snug"
	}
})(jQuery);
