(function($){
	$.fn.jTT = function(options) {
		var defaults = {
			time: 500,
			distanceX: -150,
			distanceY: -15,
			hideDelay: 100,
			minWidth: 100,
			maxWidth: 300,
			defaultHeader: 'Примечание:',
			split: '|',
			TTClass: 'jTT',
			hideDelayTimer: null,
			template:
				'<div class="ttip">'+
				'<div>' +
				'<h1>%header%</h1>' +
				'<p>%message%</p>' +
				'</div></div>',
			ttShow: function(tt){
				tt.stop();
				tt.show();
			},
			ttHide: function(tt){tt.hide();},
			ttMove: function(tt){},
			notice: null
		};
	
		var options = $.extend(defaults, options);
		
		return this.each(function() {
			var t = $(this);
			t.data("title", t.attr('title'));
			t.removeAttr('title').addClass(options.TTClass);
			if(options.notice == null) {
				options.notice = $('<div id="tool-tip"></div>').appendTo('body').hide();
			}
			t.mouseover(function(e){
				e.preventDefault();
				e.stopPropagation();
				var temp = t.data('title').split(options.split);
				var header = (temp.length > 1) ? temp[0] : options.defaultHeader;
				var message = (temp.length > 1) ? temp[1] : temp[0];
				var html = options.template;
				html = r(html, /%header%/, header);
				html = r(html, /%message%/, message);
				options.notice.html(html);
				if(options.notice.width() < options.minWidth){options.notice.width(options.minWidth);}
				if(options.notice.width() > options.maxWidth){options.notice.width(options.maxWidth);}
				options.notice.css({
					//top: e.pageY + options.distanceY - options.notice.height(),
					//left: t.offset().left + t.width()/2 - options.notice.width()/2
					//left: e.pageX + options.distanceX
				});
				options.ttShow(options.notice);
			}).mouseout(function(e){
				options.ttHide(options.notice);
			}).mousemove(function(e){
				options.notice.css({
					top: e.pageY + options.distanceY - options.notice.height(),
					//left: t.offset().left + t.width()/2 - options.notice.width()/2
					left: e.pageX + t.width()/2 - options.notice.width()/2
					//left: e.pageX + options.distanceX
				});				
			});	
			
			
			function r(text, expr, val) {
				while(expr.test(text)) {
					text = text.replace(expr, val);
				}
				return text;
			}
		});
		

	}
})(jQuery);