var BT_GENERIC = 0;
var BT_MSIE = 1;
var BT_MSIEMAC = 2;
var BT_MSIE7 = 3;
var BT_FIREFOX = 4;
var BT_SAFARI = 5;

function browserType()
{
	var ua = navigator.userAgent;
	if (ua.indexOf("Firefox") != -1)
		return BT_FIREFOX;
	if (ua.indexOf("Safari") != -1)
		return BT_SAFARI;
	if (ua.indexOf("MSIE 7") != -1)
		return BT_MSIE7;
	if (ua.indexOf("MSIE") != -1)
		return navigator.platform.indexOf("Mac") == -1 ? BT_MSIE : BT_MSIEMAC;
	return BT_GENERIC;
}

function resize()
{
	var bt = browserType();
	if (bt == BT_MSIEMAC)
		return;
	var pane = document.getElementById("leftTAB");
	var h;
	switch (bt)
	{
	case BT_MSIEMAC:
		h = document.body.clientHeight - pane.offsetTop - 144;
		break;
	case BT_SAFARI:
		h = window.innerHeight - pane.offsetTop - 144;
		break;
	default:
		h = document.documentElement.clientHeight - pane.offsetTop - 144;
	}
	var nav = document.getElementById("navigation");
	if (h < nav.clientHeight + 60)
		h = nav.clientHeight + 60;
	var ie = bt == BT_MSIE || bt == BT_MSIEMAC;
	if (ie)
		pane.style.height = h + "px";
	else
		pane.style.minHeight = h + "px";
}

function doScroll()
{
	if (browserType() == BT_MSIEMAC)
		return;
	var elt = document.getElementById("navigation");
	var sy = document.body.scrollTop, sh = document.body.scrollHeight;
	if (!sy && document.documentElement)
	{
		sy = document.documentElement.scrollTop;
		sh = document.documentElement.scrollHeight;
	}
	if (sy > sh - elt.clientHeight - 268)
		sy = sh - elt.clientHeight - 268;
	elt.style.top = sy + "px";
	// pageYOffset, body.scrollTop, documentElement.scrollTop, scrollY
}

function enlarge(obj)
{
	var bt = browserType();
	var div = document.getElementById("image_popup");
	var elt = div.firstChild;
	while (elt.nodeName != "IMG")
		elt = elt.nextSibling;
	elt.src = "images/loading.gif";
	if (bt == BT_MSIEMAC)
		elt.src = obj.parentNode.href;
	else
		setTimeout(function() { elt.src = obj.parentNode.href; }, 1);
	var pos = getPositionRel(obj, document.body);
	var ie = bt == BT_MSIE || bt == BT_MSIEMAC || bt == BT_MSIE7 || bt == BT_FIREFOX;
	div.style.left = (pos[0] - (ie ? 0 : 1) + (bt == BT_FIREFOX || bt == BT_MSIE || bt == BT_MSIE7 ? 4 : 0)) + "px";
	div.style.top = pos[1] + "px";
	div.style.display = "block";
}

function reduce(obj)
{
	var div = document.getElementById("image_popup");
	div.style.display = "none";
}

function getPosition(elt)
{
	var x = 0, y = 0;
	while (elt.offsetParent)
	{
		x += elt.offsetLeft;
		y += elt.offsetTop;
		elt = elt.offsetParent;
	}
	return [x, y];
}

function getPositionRel(elt, rel)
{
	var pos = getPosition(elt);
	var pos_rel = getPosition(rel);
	return [pos[0] - pos_rel[0], pos[1] - pos_rel[1]];
}



// Henry code below


function $(id)
{
	return document.getElementById(id);
}

function hasWord(sentence, word)
{
	return (" " + sentence + " ").indexOf(" " + word + " ") != -1;
}

function hasClass(elt, cls)
{
	return hasWord(elt.className, cls);
}

function addClass(elt, cls)
{
	elt.className += (elt.className ? " " : "") + cls;
}

function removeClass(elt, cls)
{
	var reg = new RegExp(" ?" + cls + "\\b|\\b" + cls + " ?");
	elt.className = elt.className.replace(reg, "");
}

function addChildClass(elt, cls)
{
	var list = elt.childNodes;
	for (var i = 0; i < list.length; ++i)
		if (list[i].nodeType == 1)
			addClass(list[i], cls);
}

function removeChildClass(elt, cls)
{
	var list = elt.childNodes;
	for (var i = 0; i < list.length; ++i)
		if (list[i].nodeType == 1)
			removeClass(list[i], cls);
}

function eventSrc(e)
{
	var elt = e.target;
	if (!elt)
		elt = e.srcElement;
	return elt;
}




function info(text)
{
	status = text;
}

function changeImage(e)
{
	var elt = eventSrc(e);
	var img;
	for (img = $("image").firstChild; img != null; img = img.nextSibling)
		if (img.nodeType == 1)
			break;
	removeChildClass(elt.parentNode, "active");
	addClass(elt, "active");
	img.src = elt.href;
	return false;
}

