function tooltips_init() {
	$(document).ready( function() {
		$("body").prepend(getTip());
		$('a, span, img, div').betterTooltip( {
			speed : 150,
			delay : 300
		});
	});
}
getTip = function(id) {
	if (id)
		toadd = 'tip' + id;
	else
		toadd = 'tip';
	var tTip = "<div class='"
			+ toadd
			+ "' style='background: transparent; border: 0px;'>"
			+ "<div style='background: transparent; border: 0px;'>"
			+ "<table width='auto' style='background: transparent; border: 0px;'"
			+ " cellpadding=0 cellspacing=0>"
			+ " <tr style='background: transparent; border: 0px;'>"
			+ "  <td style='background: transparent; border: 0px;'>"
			+ "  <table width='100%' style='background: transparent; border: 0px;'"
			+ "   cellpadding=0 cellspacing=0>"
			+ "   <tr style='background: transparent; border: 0px;'>"
			+ "    <td style='background: transparent; border: 0px;'>"
			+ "    <div class='tipTopLeft'></div>"
			+ "    </td>"
			+ "    <td class='tipTop' style='border: 0px;' width='100%'></td>"
			+ "    <td style='background: transparent; border: 0px;'>"
			+ "    <div class='tipTopRight'></div>"
			+ "    </td>"
			+ "   </tr>"
			+ "  </table>"
			+ "  </td>"
			+ " </tr>"
			+ " <tr style='background: transparent; border: 0px;'>"
			+ "  <td style='background: transparent; border: 0px;'>"
			+ "  <table style='background: transparent; border: 0px;' width='100%'"
			+ "   cellpadding=0 cellspacing=0>"
			+ "   <tr style='background: transparent; border: 0px;'>"
			+ "    <td style='border: 0px;' class='tipMidLeft' width='12'></td>"
			+ "    <td style='border: 0px;' class='tipMid'></td>"
			+ "    <td style='border: 0px;' class='tipMidRight' width='12'></td>"
			+ "   </tr>"
			+ "  </table>"
			+ "  </td>"
			+ " </tr>"
			+ " <tr style='background: transparent; border: 0px;'>"
			+ "  <td style='background: transparent; border: 0px;'>"
			+ "  <table style='background: transparent; border: 0px;' width='100%'"
			+ "   cellpadding=0 cellspacing=0>"
			+ "   <tr style='background: transparent; border: 0px;'>"
			+ "    <td style='background: transparent; border: 0px;'>"
			+ "    <div class='tipBtmLeft'></div>" + "    </td>"
			+ "    <td class='tipBtm' style='border: 0px;' width='100%'></td>"
			+ "    <td style='background: transparent; border: 0px;'>"
			+ "    <div class='tipBtmRight'></div>" + "    </td>" + "   </tr>"
			+ "  </table>" + "  </td>" + " </tr>"
			+ " <tr style='background: transparent; border: 0px;'>"
			+ "  <td class='tipBtmArrow' style='border: 0px;'></td>" + " </tr>"
			+ "</table>" + "</div>" + "</div>";
	return tTip;
}
function str_replace(search, replace, subject, count) {
	var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0, f = []
			.concat(search), r = [].concat(replace), s = subject, ra = r instanceof Array, sa = s instanceof Array;
	s = [].concat(s);
	if (count) {
		this.window[count] = 0;
	}

	for (i = 0, sl = s.length; i < sl; i++) {
		if (s[i] === '') {
			continue;
		}
		for (j = 0, fl = f.length; j < fl; j++) {
			temp = s[i] + '';
			repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
			s[i] = (temp).split(f[j]).join(repl);
			if (count && s[i] !== temp) {
				this.window[count] += (temp.length - s[i].length) / f[j].length;
			}
		}
	}
	return sa ? s : s[0];
}
function htmlspecialchars(text) {
	var chars = Array("&", "<", ">", "\"", "'");
	var replacements = Array("&amp;", "&lt;", "&gt;", "&quot;", "&#039;");
	for ( var i = 0; i < chars.length; i++) {
		text = str_replace(chars[i], replacements[i], text);
	}
	return text;
}
$.fn.betterTooltip = function(options) {
	var defaults = {
		speed : 200,
		delay : 300
	};
	var options = $.extend(defaults, options);
	var tip = $('.tip');
	tip.css( {
		"overflow" : "hidden",
		"display" : "none",
		"position" : "absolute",
		"z-index" : "500",
		"border" : "0px",
		'max-width' : '50%',
		"background" : "transparent"
	});
	var tipInner = $('.tip .tipMid');
	$this_all = $(this);
	$(this).each( function() {
		var $this = $(this);
		if ($this.attr("notooltip") == "yes") {
			return;
		}
		/*if (!$this.attr("tooltip")) {
			var title = htmlspecialchars(($this.attr("title")
					|| $this.attr("alt") ? ($this.attr("title") ? $this
					.attr("title") : ($this.attr("alt") ? $this
					.attr("alt") : "")) : ""));
			if (title)
				$this.attr("tooltip", title);
			else
				return;
		}*/
		if (!$this.attr("tooltip")) {
			return;
		}

		$this.attr("alt", "");
		$this.attr("title", "");
		$this.mouseover( function() {
			$this.mouseout( function() {
				tip.stop(false, true);
				tip.hide();
			});
			tip.hide();
			var tTitle = $this.attr("tooltip");
			var tWidth = $this.width();
			var tHeight = $this.height();
			var offset = $this.offset();
			var tLeft = offset.left - 58;
			var tTop = offset.top;
			tipInner.html(tTitle);
			setTip(tTop, tLeft, $this);
			showTip();
		});
		setTip = function(top, left, $this) {
			var topOffset = tip.height();
			var leftOffset = $this.width() / 2;
			var xTip = (left + leftOffset) + "px";
			var yTip = (top - topOffset) + "px";
			tip.css( {
				'top' : yTip,
				'left' : xTip
			});
		};
		showTip = function() {
			tip.fadeIn(defaults.speed);
		};
	});
};
$.fn.startTooltip = function(options) {
	var defaults = {
		speed : 200,
		delay : 300
	};
	var options = $.extend(defaults, options);
	if (!$('.tip' + $(this).attr("id")).text())
		$("body").prepend(getTip($(this).attr("id")));
	var new_tip = $('.tip' + $(this).attr("id"));
	var new_tipInner = $('.tip' + $(this).attr("id") + ' .tipMid');
	new_tip.css( {
		"overflow" : "hidden",
		"display" : "none",
		"position" : "absolute",
		"z-index" : "500",
		"border" : "0px",
		'max-width' : '50%',
		"background" : "transparent"
	});
	new_setTip = function(top, left) {
		var topOffset = new_tip.height();
		var xTip = (left) + "px";
		var yTip = (top - topOffset) + "px";
		new_tip.css( {
			'top' : yTip,
			'left' : xTip
		});
		new_tip.fadeIn(defaults.speed);
	};
	var $this = $(this);
	var offset = $this.offset();
	var tLeft = offset.left;
	var tTop = offset.top;
	var tWidth = $this.width();
	var tHeight = $this.height();
	var tTitle = ($this.attr("title"));
	$this.attr("title", "");
	new_tipInner.html(tTitle);
	new_setTip(tTop, tLeft, new_tip);
	new_tip.show();
};
$.fn.updateTooltip = function(what) {
	new_setTip = function(top, left) {
		var topOffset = new_tip.height();
		var xTip = (left) + "px";
		var yTip = (top - topOffset) + "px";
		new_tip.css( {
			'top' : yTip,
			'left' : xTip
		});
	};
	var new_tip = $('.tip' + $(this).attr("id"));
	var new_tipInner = $('.tip' + $(this).attr("id") + ' .tipMid');
	new_tipInner.html(what);
	var $this = $(this);
	var offset = $this.offset();
	var tLeft = offset.left;
	var tTop = offset.top;
	new_setTip(tTop, tLeft, new_tip);
};
$.fn.stopTooltip = function() {
	var new_tip = $('.tip' + $(this).attr("id"));
	new_tip.hide();
};

/*
window.onerror = null;
*/

tooltip = {

/* НАЧАЛО НАСТРОЕК */

attr_name: "tooltip", // наименование создаваемого tooltip'ого атрибута
blank_text: "(ссылка откроется в новом окне)", // текст для ссылок с target="_blank"
newline_entity: "~", // укажите пустую строку (""), если не хотите использовать в tooltip'ах многострочность; ежели хотите, то укажите тот символ или символы, которые будут заменяться на перевод строки
max_width: 0, // максимальная ширина tooltip'а в пикселах; обнулите это значение, если ширина должна быть нелимитирована
delay: 0, // задержка при показе tooltip'а в миллисекундах

/* КОНЕЦ НАСТРОЕК */

t: document.createElement("DIV"),
c: null,
g: false,

m: function(e){
if (tooltip.g){
oCanvas = document.getElementsByTagName(
(document.compatMode && document.compatMode == "CSS1Compat") ? "HTML" : "BODY"
)[0];
x = window.event ? event.clientX + oCanvas.scrollLeft : e.pageX;
y = window.event ? event.clientY + oCanvas.scrollTop : e.pageY;
tooltip.a(x, y);
}
},

d: function(){
tooltip.t.setAttribute("id", "tooltip");
//tooltip.t.style.filter = "alpha(opacity=85)"; // buggy in ie5.0
document.body.appendChild(tooltip.t);
a = document.all ? document.all : document.getElementsByTagName("*");
aLength = a.length;
for (var i = 0; i < aLength; i++){

//if (a[i].tagName == "A" || a[i].tagName == "BUTTON" || (a[i].tagName == "INPUT" && (a[i].type == "submit" || a[i].type == "button" || a[i].type == "reset"))) a[i].onclick = self.focus;

if (!a[i]) continue;

tooltip_title = a[i].getAttribute("title"); // returns form object if IE & name="title"; then IE crashes; so...
if (tooltip_title && typeof tooltip_title != "string") tooltip_title = "";

tooltip_alt = a[i].getAttribute("alt");
tooltip_blank = a[i].getAttribute("target") && a[i].getAttribute("target") == "_blank" && tooltip.blank_text;
if (tooltip_title || tooltip_blank){
a[i].setAttribute(tooltip.attr_name, tooltip_blank ? (tooltip_title ? tooltip_title + " " + tooltip.blank_text : tooltip.blank_text) : tooltip_title);
if (a[i].getAttribute(tooltip.attr_name)){
a[i].removeAttribute("title");
if (tooltip_alt && a[i].complete) a[i].removeAttribute("alt");
tooltip.l(a[i], "mouseover", tooltip.s);
tooltip.l(a[i], "mouseout", tooltip.h);
}
}else if (tooltip_alt && a[i].complete){
a[i].setAttribute(tooltip.attr_name, tooltip_alt);
if (a[i].getAttribute(tooltip.attr_name)){
a[i].removeAttribute("alt");
tooltip.l(a[i], "mouseover", tooltip.s);
tooltip.l(a[i], "mouseout", tooltip.h);
}
}
if (!a[i].getAttribute(tooltip.attr_name) && tooltip_blank){
//
}
}
document.onmousemove = tooltip.m;
window.onscroll = tooltip.h;
tooltip.a(-99, -99);
},

s: function(e){
d = window.event ? window.event.srcElement : e.target;
if (!d.getAttribute(tooltip.attr_name)) return;
s = d.getAttribute(tooltip.attr_name);
if (tooltip.newline_entity){
s = s.replace(/\&/g,"&amp;");
s = s.replace(/\</g,"&lt;");
s = s.replace(/\>/g,"&gt;");
s = s.replace(eval("/" + tooltip.newline_entity + "/g"), "<br />");
tooltip.t.innerHTML = s;
}else{
if (tooltip.t.firstChild) tooltip.t.removeChild(tooltip.t.firstChild);
tooltip.t.appendChild(document.createTextNode(s));
//tooltip.t.innerText = s;
}
tooltip.c = setTimeout("tooltip.t.style.visibility = 'visible';", tooltip.delay);
tooltip.g = true;
},

h: function(e){
tooltip.t.style.visibility = "hidden";
if (!tooltip.newline_entity && tooltip.t.firstChild) tooltip.t.removeChild(tooltip.t.firstChild);
clearTimeout(tooltip.c);
tooltip.g = false;
tooltip.a(-99, -99);
},

l: function(o, e, a){
if (o.addEventListener) o.addEventListener(e, a, false); // was true--Opera 7b workaround!
else if (o.attachEvent) o.attachEvent("on" + e, a);
else return null;
},

a: function(x, y){
oCanvas = document.getElementsByTagName(
(document.compatMode && document.compatMode == "CSS1Compat") ? "HTML" : "BODY"
)[0];

w_width = oCanvas.clientWidth ? oCanvas.clientWidth + oCanvas.scrollLeft : window.innerWidth + window.pageXOffset;
w_height = window.innerHeight ? window.innerHeight + window.pageYOffset : oCanvas.clientHeight + oCanvas.scrollTop; // should be vice verca since Opera 7 is crazy!

tooltip.t.style.width = ((tooltip.max_width) && (tooltip.t.offsetWidth > tooltip.max_width)) ? tooltip.max_width + "px" : "auto";

t_width = tooltip.t.offsetWidth;
t_height = tooltip.t.offsetHeight;

tooltip.t.style.left = x + 6 + "px";
tooltip.t.style.top = y + 16 + "px";

if (x + t_width > w_width-8) tooltip.t.style.left = w_width - t_width + "px";
if (y + t_height > w_height-8) tooltip.t.style.top = w_height - t_height + "px";
}
}

var root = window.addEventListener || window.attachEvent ? window : document.addEventListener ? document : null;
if (root){
if (root.addEventListener) root.addEventListener("load", tooltip.d, false);
else if (root.attachEvent) root.attachEvent("onload", tooltip.d);
}

