function event_queue(type, f)
{
	var typeQueue = type + "Queue";

	if (typeof(window[typeQueue]) == "undefined")
	{
		window[typeQueue] = [];
		if (window[type])
			event_queue(type, window[type]);

		window[type] = function()
		{
			for (var i = window[typeQueue].length - 1; i >= 0; i--)
				window[typeQueue][i]();
		}
	}

	window[typeQueue][window[typeQueue].length] = f;
}

var site_subnav_last = null;
var site_subnav_info = {};

function site_subnav_show(parent, id)
{
	var subnav = document.getElementById(id);

	subnav.style.display = "block";
	subnav.style.left = (parent.offsetLeft + parent.offsetParent.offsetLeft) + "px";
	subnav.style.top = (parent.offsetParent.offsetTop + parent.offsetTop + parent.offsetHeight) + "px";

	subnav._nav_showing = true;

	if (parent.className != "hard-selected")
		parent.className = "selected";

	function onclose()
	{
		subnav.style.display = "none";
		subnav._nav_showing = false;

		if (parent.className != "hard-selected")
			parent.className = "";
	}

	site_subnav_lateclose(parent, subnav, onclose);

	if (site_subnav_last && site_subnav_last != subnav)
		if (site_subnav_last._nav_showing)
		{
			site_subnav_last._lateclose_want = true;
			site_subnav_last._lateclose_block = 0;
			site_subnav_last.tryClose();

			if (site_subnav_last._lateclose_parent.className != "hard-selected")
				site_subnav_last._lateclose_parent.className = "";
		}
	site_subnav_last = subnav;
}

function site_subnav_show_secondary(parent, id)
{
	var subnav = document.getElementById(id);

	subnav.style.display = "block";
	subnav.style.left = (parent.offsetParent.offsetLeft + parent.offsetParent.offsetWidth) + "px";
	subnav.style.top = (parent.offsetParent.offsetTop + parent.offsetTop) + "px";

	if (typeof(subnav._nav_showing) == "undefined" || !subnav._nav_showing)
	{
		subnav._nav_showing = true;
		subnav._parent_nav = parent.parentNode.parentNode;

		if (typeof(subnav._parent_nav._lateclose_block) == "undefined")
			subnav._parent_nav._lateclose_block = 0;
		subnav._parent_nav._lateclose_block++;
	}

	if (parent.className != "hard-selected")
		parent.className = "selected";

	if (typeof(subnav._parent_nav._nav_children) == "undefined")
		subnav._parent_nav._nav_children = [];
	subnav._parent_nav._nav_children[subnav._parent_nav._nav_children.length] = subnav;

	subnav.onclose = function ()
	{
		subnav.style.display = "none";
		subnav._nav_showing = false;

		if (subnav._parent_nav._lateclose_block > 0)
			subnav._parent_nav._lateclose_block--;

		if (parent.className != "hard-selected")
			parent.className = "";

		if (subnav._parent_nav._lateclose_block == 0)
		{
			if (subnav._parent_nav.tryClose)
				subnav._parent_nav.tryClose();
		}

		if (typeof(subnav._nav_children) != "undefined")
			for (var i = 0; i < subnav._nav_children.length; i++)
			{
				if (subnav._nav_children[i]._nav_showing)
					subnav._nav_children[i].tryClose();
			}
	}

	site_subnav_lateclose(parent, subnav, subnav.onclose);

	if (site_subnav_last && site_subnav_last._parent_nav == subnav._parent_nav && site_subnav_last != subnav)
		if (site_subnav_last._nav_showing)
		{
			site_subnav_last._lateclose_want = true;
			site_subnav_last._lateclose_block = 0;
			site_subnav_last.tryClose();

			if (site_subnav_last._lateclose_parent.className != "hard-selected")
				site_subnav_last._lateclose_parent.className = "";
		}
	site_subnav_last = subnav;
}

function site_subnav_lateclose(parent, subnav, onclose)
{
	subnav._lateclose_want = false;
	if (typeof(subnav._lateclose_block) == "undefined")
		subnav._lateclose_block = 0;

	subnav._lateclose_parent = parent;

	subnav._lateclose_onclose_next = onclose;
	subnav._lateclose_onclose = function ()
	{
		this.onmouseout = function () {};
		this.onmouseover = function () {};

		this._lateclose_onclose_next();
	}

	subnav.tryClose = function ()
	{
		if (this._lateclose_want == false || this._lateclose_block > 0 || this._lateclose_parent._lateclose_block)
			return;

		this._lateclose_want = false;
		this._lateclose_onclose();
	}

	subnav.onmouseout = function ()
	{
		this._lateclose_want = true;

		var subnav = this;
		window.setTimeout(function ()
		{
			subnav.tryClose();
		}, 500);
	}

	subnav.onmouseover = function ()
	{
		this._lateclose_want = false;
	}

	parent._lateclose_child = subnav;

	parent._lateclose_onmouseout = parent.onmouseout;
	parent.onmouseout = function ()
	{
		this._lateclose_block = false;

		this._lateclose_child.onmouseout();

		if (this._lateclose_onmouseout)
		{
			this.onmouseout = this._lateclose_onmouseout;
			this._lateclose_onmouseout = null;

			this.onmouseout();
		}
	}

	parent._lateclose_onmouseover = parent.onmouseover;
	parent.onmouseover = function ()
	{
		this._lateclose_block = true;

		if (this._lateclose_onmouseover)
		{
			this.onmouseover = this._lateclose_onmouseover;
			this._lateclose_onmouseover = null;

			this.onmouseover();
		}
	}
}

event_queue("onload", site_position_cat_later);
event_queue("onresize", site_position_cat);

function site_position_cat()
{
	if (!document.getElementById("site-container"))
		return;

	var cat = document.getElementById("site-helpful-moca");
	var w = document.getElementById("site-container").clientWidth;
	var maxW = 293;

	document.getElementById("site-footer-lines").style.width = w + "px";
	document.getElementById("site-footer").style.width = w + "px";

	if (cat)
	{
		if (w - cat.offsetLeft > maxW)
			cat.style.width = maxW + "px";
		else
			cat.style.width = (w - cat.offsetLeft) + "px";
	}
}

function site_position_cat_later()
{
	site_position_cat();
	window.setTimeout(site_position_cat, 0);
}

function site_toggle_inside_img(parent, toggle)
{
	var imgs = parent.getElementsByTagName ? parent.getElementsByTagName("img") : parent.all.tags.img;

	for (var i = 0; i < imgs.length; i++)
		site_toggle_img(imgs[i], toggle);
}

function site_toggle_img(img, toggle)
{
	if (toggle)
		img.src = img.src.replace("_off.", "_over.");
	else
		img.src = img.src.replace("_over.", "_off.");
}
