/*----------------------------------------------------------------------------------------------------*\
	
	MEL.Tipper - jQuery Widget
	
	Version:	1.2.1
	Author:		Melnaron (http://melnaron.net)
	
	Этот небольшой виджет позволяет очень просто добавить
	мгновенно-всплывающие подсказки (tooltips) к любому елементу на странице.
	
	Для добавления всплывающей подсказки к любому элементу необходимо
	просто добавить атрибут tip с текстом подсказки:
		<img src="img.jpg" tip="My tooltip!" />
	
	Если же вы создаете элемент динамически и хотите добавить к нему тултип,
	то для этого существует функция tip('ваш текст'):
		$('<p>Check It!</p>').tip('My tooltip!').appendTo('body');
	
	Чтобы удалить тултип у элемента нужно вызвать функцию tip() без параметра:
		$('.notip').tip();
	
	Для того чтобы мгновенно изменить стиль отображения тултипа используется
	функция $.styleTip() с массивом {ключ_стиля: значение} в качестве параметра:
		$.styleTip({
			border: '3px solid #999',
			background: '#333',
			color: '#fff'
		});
	
	Если вам вдруг потребуется ре-инициализировать тултипы всех элементов
	на странице, то вы можете вызвать функцию $.initTips().
	
\*----------------------------------------------------------------------------------------------------*/

jQuery.showTip = function(tip, x, y) {
	if (tip != null) {
		var t   = $('#divTip');
		var ox  = 16;
		var oy  = 16;
		var twp = parseInt(t.css('padding-left')) + parseInt(t.css('padding-right'));
		var twb = parseInt(t.css('border-left-width')) + parseInt(t.css('border-right-width'));
		var thp = parseInt(t.css('padding-top')) + parseInt(t.css('padding-bottom'));
		var thb = parseInt(t.css('border-top-width')) + parseInt(t.css('border-bottom-width'));
		
		if ($.browser.msie) {
			t.html(tip).show().css({width: ''});
			var tw  = t.width();
			var twm = parseInt(t.css('max-width'));
			if (tw > twm) {
				t.css('width', twm);
				tw = twm + twp + twb;
			} else {
				tw = tw + twp + twb;
			}
		} else {
			t.html(tip).show();
			var tw  = t.width() + twp + twb;
		}
		
		var th  = t.height() + thp + thb;
		
		x = (x + tw + ox > $(document).width()) ? x = x - tw - (ox/2) + 'px' : x = x + ox + 'px';
		y = (y + th + oy > $(document).height()) ? y = y - th - (oy/2) + 'px' : y = y + oy + 'px';
		
		t.css({left: x, top: y});
	}
}

jQuery.hideTip = function() {
	$('#divTip').hide();
}

jQuery.styleTip = function(style) {
	$('#divTip').css(style);
}

jQuery.fn.addClassTip = function(class_name) {
	var tip = $('#divTip');
	return this.each(function() { 
		jQuery(this).mouseover(function(){ tip.addClass(class_name); });
		jQuery(this).mouseout(function(){ tip.removeAttr( 'class' ); });
	});
}

jQuery.initTips = function() {
	$('*[tip]')
		.hover(
			function(e) {
				$.showTip($(this).attr('tip'), e.pageX, e.pageY);
			},
			function() {
				$.hideTip();
			}
		)
		.mousemove(function(e) {
			$.showTip($(this).attr('tip'), e.pageX, e.pageY);
		})
	;
}

jQuery.fn.tip = function(tip, e) {
	if (tip == null) {
		$(this).removeAttr('tip');
		$.hideTip();
	} else {
		$(this)
			.attr('tip', tip)
			.hover(
				function(e) {
					$.showTip(tip, e.pageX, e.pageY);
				},
				function() {
					$.hideTip();
				}
			)
			.mousemove(function(e) {
				$.showTip(tip, e.pageX, e.pageY);
			})
		;
		if (e != null) {
			$.showTip(tip, e.pageX, e.pageY);
		}
	}
	return this;
}

$(document).ready(function() {
	$('<div id="divTip"></div>')
		.css({
			position:	'absolute',
			display:	'none',
			border:		'1px solid #fff',
			background:	'#3e98d7',
			font:		'11px Tahoma',
			color:		'#fff',
			padding:	'10px',
			maxWidth:	'300px',
			opacity:	'0.99',
			zIndex:		'999999'
		})
		.mouseover(function(){$.hideTip()})
		.appendTo('body')
	;
	
	$.initTips();
});