var ReqPool = new Array();
var count = 0;

function getRequestObject()
{
	var req = false;

	if(window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	}
	else if(window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	return req;
}

function forumItem(itemid, full)
{
	var d = document.getElementById("itemCss");
	if(!d) {
		var h = document.getElementsByTagName("head")[0];
		s = document.createElement("link");
		s.id = "itemCss";
		s.rel = "stylesheet";
		s.type = "text/css";
		s.href = "http://forums.lawnpirates.net/itemdb/css_item.css";
		h.appendChild(s);
	}
	
	var goodreq = false;
	var num = -1;
	for(var i = 0; i < ReqPool.length; i++)
	{
		var req = ReqPool[i];
		if(req && (req.readyState == 0 || req.readyState == 4))
		{
			num = i;
			goodreq = req;
			if(req.readyState == 4)
				req.abort();
			break;
		}
	}
	if(!goodreq)
	{
		goodreq = getRequestObject();
		ReqPool.push(goodreq);
	}
	
	var name = "link-"+count++;
	var url = "/itemdb/itemget.php?item=" + itemid + "&full=" + full;
	
	if(full) {
		document.write("<div id=\"" + name + "\" style=\"width: 320px;\">[Loading Item " + itemid + "...]</div>");
	}
	else {
		document.write("<span id=\"" + name + "\">[Loading Item " + itemid + "...]</span>");
	}

	goodreq.onreadystatechange = function() { stateChanged(goodreq, name, full); }
	goodreq.open("GET", url, true);
	goodreq.send(null);
}

function stateChanged(request, name, full)
{
	if(request.readyState == 4)
	{
		if(request.status == 200)
		{
			if(full) {
				var strs = request.responseText.split("|");
				document.getElementById(name).innerHTML = strs[0];
				eval(strs[1]);
			}
			else {
				document.getElementById(name).innerHTML = request.responseText;
			}
		}
		else
		{
			document.getElementById(name).innerHTML = "[Error Getting Data]";
		}
	}
}

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="http://www.wowhead.com/images/icon_border_small.gif"><img src="http://www.wowhead.com/images/icon_border_medium.gif"><img src="http://www.wowhead.com/images/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 style="position: relative"><div class="wowicon" style="background-image: url(http://www.wowhead.com/images/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_AppendTooltip(id, tooltip)
{
	var el = ge(id);
	el.appendChild(g_CreateTooltip(tooltip));
	if(isIE)
		setTimeout("g_FixTooltip(ge('" + id + "'), 1, 1)", 1);
	else
		g_FixTooltip(el, 1, 1);
}

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';

	if(tooltip)
		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;
}