var _, g_layers, g_tooltip, g_tooltipTable, g_tooltipTd;

function ge(z){return document.getElementById(z)}
function gE(z,y){return z.getElementsByTagName(y)}
function ce(z){return document.createElement(z)}
function ac(z){var a=0,b=0;while(z){a+=z.offsetLeft;b+=z.offsetTop;z=z.offsetParent}return [a,b]}

function g_Init()
{
	_ = ce('div');
	_.className = 'layers';
	document.body.appendChild(_);
	g_layers = _;

	_ = g_CreateTooltip();
	_.style.cursor = 'default';
	_.style.position = 'absolute';
	_.style.left = _.style.top = '-2323px';

	document.body.appendChild(_);
	g_tooltip      = _;
	g_tooltipTable = gE(_, 'table')[0];
	g_tooltipTd    = gE(g_tooltipTable, 'td')[0];

	if(isIE6)
	{
		_ = ce('iframe');
		_.src = 'javascript:0;';
		_.frameBorder = 0;
		g_layers.appendChild(_);
		g_iframe = _;
		document.write('<div style="visibility: hidden; position: absolute; left: -2323px; top: -2323px"><img src="/img/icon_border_small.gif"><img src="/img/icon_border_medium.gif"><img src="/img/map_party.gif"></div>');
	}
}

function g_FixTooltip(ttContainer, noShrink, visible)
{
	var tooltipTd = gE(ttContainer, 'td')[0];
	var c = tooltipTd.childNodes;

	if(c.length >= 2 && c[0].nodeName == 'TABLE' && c[1].nodeName == 'TABLE')
	{
		var m;
		if(c[1].offsetWidth > 300)
			m = Math.max(300, c[0].offsetWidth) + 20;
		else
			m = Math.max(c[0].offsetWidth, c[1].offsetWidth) + 20;

		var t = tooltipTd.parentNode.parentNode.parentNode,
		    d = t.parentNode;

		if(m > 20)
		{
			d.style.width = m + 'px';
			c[0].width = c[1].width = '100%';

			if(!noShrink && d.offsetHeight > document.body.clientHeight)
				t.className = 'shrink';
		}
	}
	
	if(visible)
		ttContainer.firstChild.style.visibility = 'visible';
}

function g_ShowTooltip(_this, text, icon)
{
	g_tooltip.style.width = 'auto';
	g_tooltip.style.left = '-2323px';
	g_tooltip.style.top  = '-2323px';
	g_tooltipTd.innerHTML = text;
	g_tooltip.style.display = '';

	g_FixTooltip(g_tooltip, 0, 0);

	if(icon != null)
		g_tooltipTd.innerHTML = '<div id="test" style="position: relative"><div class="wowicon" style="background-image: url(http://www.worldofraids.com/wowhead/icons/medium/' + icon + '.jpg)"><div class="wowtile"></div></div></div>' + g_tooltipTd.innerHTML;

	g_MoveTooltip(_this, g_tooltip, 0, 0, g_tooltipTable.offsetWidth, g_tooltipTable.offsetHeight, 'contents');
}

function g_MoveTooltip(_this, tooltip, x, y, tow, toh, clip)
{
	var c = ac(_this);

	var left = c[0],
	    top  = c[1],
	    minx = 0,
	    miny = 0,
	    w1   = _this.offsetWidth  + x,
	    h1   = _this.offsetHeight + y,
	    bsl  = document.body.scrollLeft,
	    bst  = document.body.scrollTop,
	    bch  = document.body.clientHeight,
	    bcw  = document.body.clientWidth;

	tooltip.style.width = tow + 'px';

	if(clip != null)
	{
		_ = ge(clip);
		if(_)
		{
			c = ac(_);
			minx = c[0];
			miny = c[1];

			if(_.offsetWidth + minx <= bsl + bcw)
				bcw = _.offsetWidth  + minx - bsl;

			if(_.offsetHeight + miny <= bst + bch)
				bch = _.offsetHeight + miny - bst;
		}
	}

	if(left + w1 + tow > bcw)
		left = Math.max(left - tow - x, minx);
	else
		left += w1;

	if(left < minx)
		left = minx;
	else if(left + tow > bsl + bcw)
		left = bsl + bcw - tow;

	if(top - toh - y > Math.max(bst, miny))
		top = top - toh - y;
	else
		top += h1;

	if(top < miny)
		top = miny;
	else if(top + toh > bst + bch)
		top = Math.max(bst, bst + bch - toh);

	tooltip.style.left = left + 'px';
	tooltip.style.top  = top  + 'px';

	tooltip.style.visibility = 'visible';

	if(isIE6)
	{
		_ = g_iframe;
		_.style.left = left + 'px';
		_.style.top  = top + 'px';
		_.style.width  = tow + 'px';
		_.style.height = toh + 'px';
		_.style.display = '';
		_.style.visibility = 'visible';
	}
}

function g_HideTooltip()
{
	g_tooltip.style.display = 'none';
	g_tooltip.style.visibility = 'hidden';
	g_tooltipTable.className = '';
	if(isIE6)
		g_iframe.style.display = 'none';
}

function g_CreateTooltip(tooltip)
{
	var d = ce('div'), t = ce('table'), tb = ce('tbody'), tr1 = ce('tr'), tr2 = ce('tr'), td = ce('td'), th1 = ce('th'), th2 = ce('th'), th3 = ce('th');
	
	d.className = 'wowtooltip';
	
	t.cellSpacing = 0;
	t.cellPadding = 0;
	
	th1.style.backgroundPosition = 'top right';
	th2.style.backgroundPosition = 'bottom left';
	th3.style.backgroundPosition = 'bottom right';
	
	td.innerHTML = tooltip;
	tr1.appendChild(td);
	tr1.appendChild(th1);
	tb.appendChild(tr1);
	tr2.appendChild(th2);
	tr2.appendChild(th3);
	tb.appendChild(tr2);
	t.appendChild(tb);
	d.appendChild(t);
	return d;
}