var preimg = new Object();
function preload(dir, states)
{
	var len = arguments.length;
	for (var i = 2; i < len; i++)
	{
		var iname = arguments[i];
		var im = preimg[iname] = [];
		iname = dir + iname;
		for (var s = 0; s < states; s++)
		{
			im[s] = new Image();
			im[s].src = iname + "_" + s + ".gif";
		}
	}
}

function swap_img(iname, state)
{
	var i = document.images[iname];
	var p = preimg[iname];
	if (i && p) i.src = p[state].src;
}

function btn_swap(btn, idx, s)
{
	var i = document.images["id-" + btn + "-img-" + idx];
	var p = preimg[btn];
	if (i && p) i.src = p[s].src;
}

function btn_out(btn, idx)  {btn_swap(btn, idx, 0);}
function btn_over(btn, idx) {btn_swap(btn, idx, 1);}
function btn_up(btn, idx)   {btn_swap(btn, idx, 1);}
function btn_down(btn, idx) {btn_swap(btn, idx, 2);}

function swap_top_0(elm)
{
	if (elm.className) elm.className = "t-top-0";
}

function swap_top_1(elm)
{
	if (elm.className) elm.className = "t-top-1";
}

function win_stat(s)
{
	window.status = (s ? s : " ");
	return true;
}

function fix_dec(x, d)
{
	x = new String(Math.round(x * Math.pow(10, d)));
	if (d == 0) return x;

	// left-pad with 0 to make at least 0dd
	var len = x.length;
	var m = d + 1;
	for (; len < m; len++) x = "0" + x;

	m = len - d;
	return x.substr(0, m) + "." + x.substr(m);
}

function do_onload()
{
	var links = document.links;
	var len = links.length;
	for (var i = 0; i < len; i++)
	{
		var link = links[i];
		if (link.onmouseout) link.onmouseout();
		link.onfocus = link.blur;
		link.ondblclick = function() {return false;}
	}
	
	if (window.do_onload_extra) do_onload_extra();
	
	write_menu_bag();
	
	if (document.all)
	{
		var d = document.body.style;
		d.display = "none";
		d.display = "block";
	}
}

function do_onunload() {if (window.do_onunload_extra) window.do_onunload_extra();}

function write_menu_bag()
{	
	if (window.bag && (location.href == parent.location.href))
	{
		var n = bag.get_num_items();
		str = (n <= 0 ? "" : "<b>&nbsp;&bull; " + n + (n == 1 ? " ITEM</b>" : " ITEMS</b>"));
		xWrite("id-menu-bag", str);
	}
}

function xGetElm(e)
{
	if (typeof(e) != 'string')
		return e;
	if (document.getElementById)
		return document.getElementById(e);
 	if (document.all)
		return document.all[e];
	return null;
}

function xShow(e)
{
	if (e = xGetElm(e)) e.style.visibility = 'inherit';
}

function xHide(e)
{
	if (e = xGetElm(e)) e.style.visibility = 'hidden';
}

function xExpand(e)
{
	if (e = xGetElm(e)) e.style.display = 'block';
}

function xCollapse(e)
{
	if (e = xGetElm(e)) e.style.display = 'none';
}

function xLeft(e, x)
{
	if (! (e = xGetElm(e)))
		return;

	if (typeof(e.style.left) == "string")
	{
		if (arguments.length > 1)
			e.style.left = x + "px";
		else
		{
			x = parseInt(e.style.left);
			if (isNaN(x)) x = 0;
		}
	}
	else if (typeof(e.style.pixelLeft) != "undefined")
	{
		if (arguments.length > 1)
			e.style.pixelLeft = x;
		else
			x = e.style.pixelLeft;
	}

	return x;
}

function xTop(e, y)
{
	if (! (e = xGetElm(e)))
		return;

	if (typeof(e.style.top) == "string")
	{
		if (arguments.length > 1)
			e.style.top = y + "px";
		else
		{
			y = parseInt(e.style.top);
			if (isNaN(y)) y = 0;
		}
	}
	else if (typeof(e.style.pixelTop) != "undefined")
	{
		if (arguments.length > 1)
			e.style.pixelTop = y;
		else
			y = e.style.pixelTop;
	}

	return y;
}

function xWidth(e, w)
{
	if (e = xGetElm(e))
	{
		if (typeof(e.style.width) == "string")
			e.style.width = w + "px";
		else if (typeof(e.style.pixelWidth) != "undefined")
			e.style.pixelWidth = w;
	}
}

function xHeight(e, h)
{
	if (e = xGetElm(e))
	{
		if (typeof(e.style.height) == "string")
			e.style.height = h + "px";
		else if (typeof(e.style.pixelHeight) != "undefined")
			e.style.pixelHeight = h;
	}
}

function xMoveTo(e, x, y)
{
	if (e = xGetElm(e))
	{
		xLeft(e, x);
		xTop(e, y);
	}
}

function xColor(e, c)
{
	if (e = xGetElm(e)) e.style.color = c;
}

function xBackground (e, c)
{
	if (e = xGetElm(e)) e.style.backgroundColor = c;
}

function xScrollLeft()
{
	if (typeof(window.pageXOffset) != "undefined")
		return window.pageXOffset;
  	if (document.documentElement && document.documentElement.scrollLeft)
  		return document.documentElement.scrollLeft;
  	if (document.body && typeof(document.body.scrollLeft) != "undefined")
  		return document.body.scrollLeft;
  	return 0;
}

function xScrollTop()
{
	if (typeof(window.pageYOffset) != "undefined")
		return window.pageYOffset;
  	if (document.documentElement && document.documentElement.scrollTop)
  		return document.documentElement.scrollTop;
  	if (document.body && typeof(document.body.scrollTop) != "undefined")
  		return document.body.scrollTop;
  	return 0;
}

function xWrite(e, h)
{
  	if (e = xGetElm(e))
	{
		if ((! document.getElementById) && document.all) e.innerHTML = '';
		e.innerHTML = h;
	}
}
