var tooltip = function() {
	var id = 'tt';
	var top = 3;
	var left = 3;
	var maxw = 300;
	var defaultW = 100;
	var speed = 10;
	var timer = 20;
	var endalpha = 95;
	var alpha = 0;
	// var tt,t,c,b,h;
	var tt, tl, t, tr, middle, content, bl, b, br, h, r, l;
	var ie = document.all ? true : false;
	return {
		show : function(element, v, w) {
			if (tt == null) {
				this.createTooltip();
			}

			tt.style.display = 'block';

			if (v && v.length > 0) {
				content.innerHTML = v;
			} else {
				content.innerHTML = '&nbsp;'
			}

			this.setStyle(element, w);

			if (tt.timer) {
				clearInterval(tt.timer);
			}
			tt.timer = setInterval( function() {
				tooltip.fade(1)
			}, timer);

		},
		setStyle:function(element, w) {
			// setting width
			if (!w) {
				w = defaultW;
			}
			tt.style.width = w + 'px'

			if (tt.offsetWidth > maxw) {
				tt.style.width = maxw + 'px'
			}

			t.style.width = (parseInt(tt.offsetWidth) - (9 + 15)) + 'px';
			b.style.width = (parseInt(tt.offsetWidth) - (9 + 15)) + 'px';
			content.style.width = (parseInt(tt.offsetWidth) - (9 + 15)) + 'px';
			l.style.height = content.offsetHeight + 'px';
			r.style.height = content.offsetHeight + 'px';

			h = parseInt(tt.offsetHeight) + top;
			var posY = this.findPosY(element);
			var posX = this.findPosX(element);
			tt.style.top = (posY - h) + 'px';
			tt.style.left = (posX + left - tt.offsetWidth) + 'px';
		},
		createTooltip:function(){
			tt = document.createElement('div');
			tt.setAttribute('id', id);
			tt.setAttribute('onclick', 'tooltip.hide();');
			tt.onclick = tooltip.hide;

			tl = document.createElement('div');
			t = document.createElement('div');
			tr = document.createElement('div');
			middle = document.createElement('div');
			r = document.createElement('div');
			l = document.createElement('div');
			content = document.createElement('div');
			bl = document.createElement('div');
			b = document.createElement('div');
			br = document.createElement('div');

			tl.setAttribute('id', id + 'tl');
			t.setAttribute('id', id + 't');
			tr.setAttribute('id', id + 'tr');
			middle.setAttribute('id', id + 'middle');
			r.setAttribute('id', id + 'r');
			l.setAttribute('id', id + 'l');
			content.setAttribute('id', id + 'content');
			bl.setAttribute('id', id + 'bl');
			b.setAttribute('id', id + 'b');
			br.setAttribute('id', id + 'br');

			tt.appendChild(tl);
			tt.appendChild(t);
			tt.appendChild(tr);

			middle.appendChild(l);
			middle.appendChild(content);
			middle.appendChild(r);
			tt.appendChild(middle);

			tt.appendChild(bl);
			tt.appendChild(b);
			tt.appendChild(br);

			document.body.appendChild(tt);
			tt.style.opacity = 0;
			tt.style.filter = 'alpha(opacity=0)';

			r.innerHTML = '&nbsp;';
			l.innerHTML = '&nbsp;';

			if (ie) {
				this.setIeFilters();
			}
		},
		setIeFilters:function() {
			var pre = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='";
			var post = "', sizingMethod='scale')";
			tl.style.filter = pre + tooltipLinks[0] + post;
			t.style.filter  = pre + tooltipLinks[1] + post;
			tr.style.filter = pre + tooltipLinks[2] + post;
			l.style.filter  = pre + tooltipLinks[3] + post;
			r.style.filter  = pre + tooltipLinks[4] + post;
			bl.style.filter = pre + tooltipLinks[5] + post;
			b.style.filter  = pre + tooltipLinks[6] + post;
			br.style.filter = pre + tooltipLinks[7] + post;
		},
		pos:function(e){
			var posY = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
			var posX = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
			tt.style.top = (posY - h) + 'px';
			tt.style.left = (posX + left - tt.offsetWidth) + 'px';
		},
		fade : function(d) {
			var a = alpha;
			if ((a != endalpha && d == 1) || (a != 0 && d == -1)) {
				var i = speed;
				if (endalpha - a < speed && d == 1) {
					i = endalpha - a;
				} else if (alpha < speed && d == -1) {
					i = a;
				}
				alpha = a + (i * d);
				tt.style.opacity = alpha * .01;
				tt.style.filter = 'alpha(opacity=' + alpha + ')';
			} else {
				if (tt.timer) {
					clearInterval(tt.timer);
				}
				if (d == -1) {
					tt.style.display = 'none'
				}
			}
		},
		hide : function() {
			if (tt) {
				if (tt.timer) {
					clearInterval(tt.timer);
				}
				tt.timer = setInterval( function() {
					tooltip.fade(-1)
				}, timer);
			}
		},
		findPosX:function(obj) {
			var curleft = 0;
			if(obj.offsetParent)
			while(1) {
				curleft += parseInt(obj.offsetLeft);
				if(!obj.offsetParent)
					break;
				obj = obj.offsetParent;
			}
			else if(obj.x)
				curleft += parseInt(obj.x);
			return curleft;
		},
		findPosY:function (obj) {
			var curtop = 0;
			if(obj.offsetParent)
			while(1)
			{
				curtop += parseInt(obj.offsetTop);
				if(!obj.offsetParent)
					break;
				obj = obj.offsetParent;
			}
			else if(obj.y)
				curtop += parseInt(obj.y);
			return curtop;
		}
	};
}();