// Positioning utility functions in JavaScript for BatMUD maps
// by Matti 'ccr' Hamalainen (Ggr Pupunen) <ccr@tnsp.org>

var currLoc = "";

// This function was stolen from http://www.quirksmode.org/js/findpos.html
function findPos(obj) {
	var curleft = curtop = 0;

	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}

	return [curleft,curtop];
}


function gotoElem(obj)
{
	var winW = 0, winH = 0;
	if (typeof(window.innerWidth) == 'number') {
		// Non-MSIE
		winW = window.innerWidth;
		winH = window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		// MSIE 6+ in 'standards compliant mode'
		winW = document.documentElement.clientWidth;
		winH = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		// MSIE 4 compatible
		winW = document.body.clientWidth;
		winH = document.body.clientHeight;
	}

	c = findPos(obj);
	window.scrollTo(c[0]-(winW/2), c[1]-(winH/2));
}


function gotoOnLoadPos()
{
	var s = window.location.href;
	if (s.indexOf("#") >= 0) {
		var n = unescape(s.substr(s.indexOf("#")+1).toLowerCase());
		var e = document.getElementById(n);
		if (e) {
			e.style.background = "red";
			gotoElem(e);
			currLoc = n;
		}
	}
}


function gotoPos()
{
	var newLoc = document.forms[0].slocation.value;
	if (currLoc != newLoc) {
		var e = document.getElementById(newLoc);
		if (e) {
			e.style.background = "red";
			e.style.color = "black";
			gotoElem(e);
			var e = document.getElementById(currLoc);
			if (e) {
				e.style.background = "white";
			}
			currLoc = newLoc;
		}
	}
}

function toggleLabels()
{
	var labVis = document.forms[0].shide.checked;
	if (document.all) {
		var cl = document.styleSheets[0].rules[0];
	} else {
		var cl = document.styleSheets[0].cssRules[0];
	}
	cl.style.visibility = labVis ? "visible" : "hidden";
}

