(function($) {

	$.fn.tooltip = function(options) {

		var browserPage = {

			x : $(document).width(),
			y : $(document).height()

		};

		var settings = {

			maxWidth		: '400px',	// Maximal width of the tooltip window
			offsetPosition	: 10,		// Offset position of the tooltip window in relation to the real position of the cursor
			fadeInTime		: 150,		// Fading IN time of the tooltip window in miliseconds
			fadeOutTime		: 150		// Fading OUT time of the tooltip window in miliseconds

		};

		// Number of the tooltip that is being generated
		var number = 0;

		return this.each(function() {

			// Updating settings parameters
			if(options) {

				$.extend(settings, options);

			}

			$(this).hover(

				/**
				 * Displaying a tooltip
				 */
				function(e) {

					number++;

					var tooltipText = $(this).attr('tipcontent');
					var tooltipTitle = $(this).attr('tiptitle');

					// Tooltip is empty, so it shouldn't be displayed :)
					if(!tooltipText) {

						return null;

					}

					// Tooltip, actually, exists... and there can be only one.
					if($('#tooltip').length > 0) {

						return null;

					} 

					// Create a tooltip window and fill it with content
					$(this).after('<div id="tooltip" class="secureTip" number="' + number + '" style="display: none;"></div>');
					$(this).next('#tooltip')
					.html('<div id="tooltipText">' + tooltipText + '</div>')
					.css('max-width', settings.maxWidth);

					// Inserting a tooltip's title if attribute exists in the DOM
					if(tooltipTitle) {

						$(this).next('#tooltip').prepend('<div id="tooltipTitle">' + tooltipTitle + '</div>');

					}

					// Getting a tooltip sizes
					var tooltipSize = {

						x : $(this).next('#tooltip').width(),
						y : $(this).next('#tooltip').height()

					};

					// Getting a cursor position in the browser window
					var cursor = {

						x : e.pageX,
						y : e.pageY

					};

					var positionX, positionY;
					// Calculating the X-position on the screen
					if((cursor.x + tooltipSize.x + settings.offsetPosition) > browserPage.x) {

						positionX = (browserPage.x - tooltipSize.x) + settings.offsetPosition;

					}
					else {

						positionX = cursor.x + settings.offsetPosition;

					}
					// Calculating the Y-position on the screen
					if((cursor.y + tooltipSize.y + settings.offsetPosition) > browserPage.y) {

						positionY = (browserPage.y - tooltipSize.y) + settings.offsetPosition;

					}
					else {

						positionY = cursor.y + settings.offsetPosition;

					}

					// Applying the rest of styles to the tooltip window :)
					$(this).next('#tooltip')
					.css('position', 'fixed')
					.css('z-index', 1000)
					.css('top', positionY + 'px')
					.css('left', positionX + 'px')
					.fadeIn(settings.fadeInTime); // Instead of setting a display: block;

				},

				/**
				 * Destroying all tooltips
				 */
				function() {

//					$('#tooltip').fadeOut(settings.fadeOutTime, function() {

						// Removing all the tooltips except for the *current* (last) one
						//$('.secureTip[number!="' + $(this).attr('name') + '"]').remove();
						$('#tooltip').remove();
						$('.secureTip[number!="' + number + '"]').remove();

//					});

				}

			);

		});

	};

})(jQuery);
