/*
originally written by paul sowden <paul@idontsmoke.co.uk> | http://idontsmoke.co.uk
modified and localized by alexander shurkayev <alshur@narod.ru> | http://htmlcoder.visions.ru
*/
function getNameBrouser() {
 var ua = navigator.userAgent.toLowerCase();
 // Определим Internet Explorer
 if (ua.indexOf("msie") != -1 && ua.indexOf("opera") == -1 && ua.indexOf("webtv") == -1) {
   return "msie"
 }
 // Opera
 if (ua.indexOf("opera") != -1) {
   return "opera"
 }
 // Gecko = Mozilla + Firefox + Netscape
 if (ua.indexOf("gecko") != -1) {
   return "gecko";
 }
 // Safari, используется в MAC OS
 if (ua.indexOf("safari") != -1) {
   return "safari";
 }
 // Konqueror, используется в UNIX-системах
 if (ua.indexOf("konqueror") != -1) {
   return "konqueror";
 }
 return "unknown";
}

var tooltip = {

	/* НАЧАЛО НАСТРОЕК */
	options: {
		attr_name: "tooltip", // наименование создаваемого tooltip'ого атрибута
		blank_text: "  <small><b class=clreds>*откроется в новом окне</b></small>", // текст для ссылок с target="_blank"
		newline_entity: "  ", // укажите пустую строку (""), если не хотите использовать в tooltip'ах многострочность; ежели хотите, то укажите тот символ или символы, которые будут заменяться на перевод строки
		max_width: 350, // максимальная ширина tooltip'а в пикселах; обнулите это значение, если ширина должна быть нелимитирована
		delay: 500, // задержка при показе tooltip'а в миллисекундах
		skip_tags: ["link", "style"], // теги, у которых не обрабатываем атрибуты alt и title
		_brows: ""
	},
	/* КОНЕЦ НАСТРОЕК */
	t: document.createElement("div"),
	t1: document.createElement("div"),
	t2: document.createElement("div"), 
	c: null,
	g: false,
	canvas: null,

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

	d: function(){
		tooltip.options._brows = getNameBrouser();
		tooltip.canvas = document.getElementsByTagName(document.compatMode && document.compatMode == "CSS1Compat" ? "HTML" : "BODY")[0];
		tooltip.t.style.visibility = 'hidden';
		tooltip.t.setAttribute("id", "tooltip");
		tooltip.t1.setAttribute("id", "tooltipback");
		tooltip.t2.setAttribute("id", "tooltipface");
		//tooltip.t2.setAttribute("_width", "100px");
		//tooltip.t2.style.width="auto";
		tooltip.t1.appendChild(tooltip.t2);
		tooltip.t.appendChild(tooltip.t1);
		document.body.appendChild(tooltip.t);

		if (tooltip.options.max_width) tooltip.t.style.maxWidth = tooltip.options.max_width + "px"; // all but ie
		var a = document.all && !window.opera ? document.all : document.getElementsByTagName("*"); // in opera 9 document.all produces type mismatch error
		var l = a.length;
		for (var i = 0; i < l; i++){

			if (!a[i] || tooltip.options.skip_tags.in_array(a[i].tagName.toLowerCase())) continue;
			//if (a[i].tagName.toLowerCase()=="a") {
			//	if (a[i].getAttribute("rel").indexOf("lightbox") != -1)  continue;
			//}

			var 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 = "";

			var tooltip_alt = a[i].getAttribute("alt");
			var tooltip_blank = a[i].getAttribute("target") && a[i].getAttribute("target") == "_blank" && tooltip.options.blank_text;
			if (tooltip_title || tooltip_blank){
				a[i].setAttribute(tooltip.options.attr_name, tooltip_blank ? (tooltip_title ? tooltip_title + " " + tooltip.options.blank_text : tooltip.options.blank_text) : tooltip_title);
				if (a[i].getAttribute(tooltip.options.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.options.attr_name, tooltip_alt);
				if (a[i].getAttribute(tooltip.options.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.options.attr_name) && tooltip_blank){
				//
			}
		}
		document.onmousemove = tooltip.m;
		window.onscroll = tooltip.h;
		tooltip.a(-3000, -3000);
	},
	
	_: function(s){
		s = s.replace(/\&/g,"&amp;");
		//s = s.replace(/\</g,"&lt;");
		//s = s.replace(/\>/g,"&gt;");
		return s;
	},

	s: function(e){
		if (typeof tooltip == "undefined") return;
		var d = window.event ? window.event.srcElement : e.target;
		if (!d.getAttribute(tooltip.options.attr_name)) return;
		tooltip.t.style.visibility = 'hidden';
		var s = d.getAttribute(tooltip.options.attr_name);
		if (tooltip.options.newline_entity){
			var s = tooltip._(s);
			s = s.replace(eval("/" + tooltip._(tooltip.options.newline_entity) + "/g"), "<br />");
			tooltip.t2.innerHTML = s;
		}else{
			if (tooltip.t2.firstChild) tooltip.t2.removeChild(tooltip.t2.firstChild);
			tooltip.t2.appendChild(document.createTextNode(s));
		}
		tooltip.t2.style.width="auto";
		tooltip.t1.style.width="auto";
		tooltip.t.style.width="auto";

		//tooltip.t1.innerHTML =tooltip.t1.innerHTML + s;
		tooltip.c = setTimeout(function(){
			tooltip.t.style.visibility = 'visible';
		}, tooltip.options.delay);
		tooltip.g = true;
	},

	h: function(e){
		if (typeof tooltip == "undefined") return;
		tooltip.t.style.visibility = "hidden";
		if (!tooltip.options.newline_entity && tooltip.t2.firstChild) tooltip.t2.removeChild(tooltip.t2.firstChild);
		clearTimeout(tooltip.c);
		tooltip.g = false;
		//tooltip.a(-99, -99);
		tooltip.a(-1000, -1000);
		//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){

		//var w_width = tooltip.canvas.clientWidth ? tooltip.canvas.clientWidth + (tooltip.canvas.scrollLeft || document.body.scrollLeft) : window.innerWidth + window.pageXOffset;
		//var w_width = xxw ? xxw + (tooltip.canvas.scrollLeft || document.body.scrollLeft) : window.innerWidth + window.pageXOffset;

		tooltip.canvas = document.getElementsByTagName(document.compatMode && document.compatMode == "CSS1Compat" ? "HTML" : "BODY")[0];

		
		if (document.all && document.all.item && !window.opera){
			// Не опера
			var w_height = window.innerHeight ? window.innerHeight + window.pageYOffset : tooltip.canvas.clientHeight + (tooltip.canvas.scrollTop || document.body.scrollTop); // should be vice verca since Opera 7 is crazy!
			var w_width = window.innerWidth ? window.innerWidth + window.pageXOffset : tooltip.canvas.clientWidth + (tooltip.canvas.scrollLeft || document.body.scrollLeft); // should be vice verca since Opera 7 is crazy!

			tooltip.t2.style.width="auto";
			tooltip.t1.style.width="auto";
			tooltip.t.style.width="auto";
			if (tooltip.options.max_width>0){
				if ((tooltip.canvas.clientWidth > tooltip.options.max_width)&&(tooltip.t2.offsetWidth>tooltip.options.max_width)){
				 	tooltip.t2.style.width = tooltip.options.max_width + "px";
				 	tooltip.t1.style.width = tooltip.options.max_width + "px";
				 	tooltip.t.style.width = tooltip.options.max_width + "px";
				}
			}
			//tooltip.t2.style.width =tooltip.options.max_width && tooltip.t2.canvas.clientWidth > tooltip.options.max_width ? tooltip.options.max_width + "px" : tooltip.t2.canvas.clientWidth+"px"; //"auto";
			//tooltip.t1.style.width = tooltip.options.max_width && tooltip.t1.offsetWidth > tooltip.options.max_width ? tooltip.options.max_width + "px" : tooltip.t1.canvas.clientWidth+"px"; //"auto";
			var dummy = tooltip.t2.offsetWidth; 
			dummy = tooltip.t2.offsetHeight; 
			dummy = tooltip.t1.offsetWidth; 
			dummy = tooltip.t1.offsetHeight; 
			dummy = tooltip.t.offsetWidth; 
			
			var ww=tooltip.t2.scrollWidth;
			//while (aa=tooltip.t2.firstChild) tooltip.t2.removeChild(tooltip.t2.firstChild);


			var wh=tooltip.t2.scrollHeight;

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

			//ww= tooltip.options.max_width && tooltip.t2.offsetWidth > tooltip.options.max_width ? tooltip.options.max_width + "px" : tooltip.t2.scrollWidth; //?tooltip.t2.style.width:"auto";//"auto";
			//tooltip.t1.style.width=tooltip.t2.style.width;
			//tooltip.t2.style.width = ww+"px";
			//tooltip.t1.style.width = "100%";//ww+"px";
			//tooltip.t.style.width = tooltip.options.max_width && tooltip.t.offsetWidth > tooltip.options.max_width ? tooltip.options.max_width + "px" : tooltip.t2.style.width; //?tooltip.t2.style.width:"auto";//"auto";

			//tooltip.t.style.width = tooltip.options.max_width && tooltip.t.offsetWidth > tooltip.options.max_width ? tooltip.options.max_width + "px" : tooltip.t2.style.width; //?tooltip.t2.style.width:"auto";//"auto";

			//tooltip.t2.style.width = tooltip.t.style.width;
			//tooltip.t1.style.width = tooltip.t2.style.width;
			if ((x+8+t_width)>w_width) { xx=w_width-t_width-8; }else{xx=x+8;}
			if (xx<0) { xx=0;}
			//if ((y+8+t_height)>w_height) { yy=w_height - t_height-16;}else{yy=y+8;}
			yy=y+8; if (yy + t_height > w_height) {
				//yy = w_height - t_height;
				yy = y- t_height-8;
			}
			if (yy<1) { yy=1;}
		}else{
			//Для оперы
			tooltip.t2.style.width="auto";
			tooltip.t1.style.width="auto";
			tooltip.t.style.width="auto";

			var w_width = tooltip.canvas.clientWidth ? tooltip.canvas.clientWidth + (tooltip.canvas.scrollLeft || document.body.scrollLeft) : window.innerWidth + window.pageXOffset;
			var w_height = window.innerHeight ? window.innerHeight + window.pageYOffset : tooltip.canvas.clientHeight + (tooltip.canvas.scrollTop || document.body.scrollTop); // should be vice verca since Opera 7 is crazy!

			//if (document.all && document.all.item && !window.opera) tooltip.t.style.width = tooltip.options.max_width && tooltip.t.offsetWidth > tooltip.options.max_width ? tooltip.options.max_width + "px" : "auto";
		
			var t_width = tooltip.t.offsetWidth;
			var t_height = tooltip.t.offsetHeight;

			xx=x+8; if (xx+ t_width > w_width) xx = w_width - t_width-16;
			yy=y+8; if (yy + t_height > w_height) {
				//yy = w_height - t_height;
				yy = y- t_height-8;
				if (yy<1) { yy=1;}
			}
				
		}
//			alert("x:"+x+" Y:"+y+" W:"+tooltip.t.offsetWidth+" H:"+tooltip.t.offsetHeight);
		

		tooltip.t.style.left = xx + "px";
		tooltip.t.style.top = yy + "px";

	}
}

Array.prototype.in_array = function(value){
	var l = this.length;
	for (var i = 0; i < l; i++)
		if (this[i] === value) return true;
	return false;
};

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);
}
