(function($) {
	$.extend($.fn, {
		vTip: function ( options ) {
			options = $.extend({
				'path' : 'images',
				'xOffset' : -10,
				'yOffset' : 25
			}, options);

			this.each(function() {
				$(this).unbind('hover').hover(function( e ) {
					e.preventDefault();

					this.t = $(this).find('.vtipcontainer').html() || this.title || '';
					this.title = '';

					if (this.t == '')
						return false; 

					this.top = (e.pageY + options.yOffset);
					this.left = (e.pageX + options.xOffset);

					$('body').append('<p id="vtip"></p>');

					this.element = $('p#vtip');

					this.element.html('<img id="vtipArrow" />' + this.t);

					if (this.id != undefined)
						this.element.addClass(this.id);

					$('p#vtip #vtipArrow').css({ "left" : "5px", "right" : "" });

					var imgSource = "/vtip_arrow.png";

					if (this.left + this.element.width() >= $(window).width()) {
						this.left = e.pageX + (this.element.width() * -1) + options.xOffset;

						$('p#vtip #vtipArrow').css({ "left" : "", "right" : "5px" });
					}

					$('p#vtip #vtipArrow').css({ "top" : "-10px", "bottom" : "" });

					if (this.top + this.element.height() > $(document).height()) {
						$('p#vtip #vtipArrow').css({ "top" : "", "bottom" : "-10px" });

						this.top = e.pageY + (this.element.height() * -1) + (options.yOffset * -1);

						imgSource = "/vtip_arrow_rotated.png";
					}
                     
					$('p#vtip #vtipArrow').attr("src", options.path + imgSource);
					$('p#vtip').css({ "top" : this.top + "px", "left" : this.left  + "px" }).fadeIn("slow");
				}, function() {
					if (this.title == '')
						this.title = this.t;

					$("p#vtip").fadeOut("slow").remove();
				}).click(function() {
					$("p#vtip").fadeOut("slow").remove();
				}).mousemove(function ( e ) {
					if ($.type(this.element) != 'object')
						return;

					this.top = (e.pageY + options.yOffset);
					this.left = (e.pageX + options.xOffset);

					$('p#vtip #vtipArrow').css({ "left" : "5px", "right" : "" });
         
					if (this.left + this.element.width() >= $(window).width()) {
						this.left = e.pageX + (this.element.width() * -1) + options.xOffset;

						$('p#vtip #vtipArrow').css({ "left" : "", "right" : "5px" });
					}

					$('p#vtip #vtipArrow').css({ "top" : "-10px", "bottom" : "" });

					if (this.top + this.element.height() > $(document).height()) {
						this.top = e.pageY + (this.element.height() * -1) + (options.yOffset * -1);

						$('p#vtip #vtipArrow').css({ "top" : "", "bottom" : "-10px" });
					}
                    
					this.element.css({ "top" : this.top + "px", "left" : this.left + "px" });
				});  
			});

			return this;
		}
	});
})(jQuery);

