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

var myhl = -1;
var mytt = null;
var myx = 0;
var myy = 0;
document.onmousemove = utt;

// Change background style of given element
function shc(n, k)
{
	document.getElementById(n).style['background'] = k;
}


// Hilite map and list elements of given number
function qh(n)
{
	if (myhl >= 0) qn(myhl);
	shc('maploc'+n, 'white');
	shc('listloc'+n, 'white');
	myhl = n;
}

// Unlite
function qn(n)
{
	shc('maploc'+n, 'black');
	shc('listloc'+n, 'black');
	myhl = -1;
	htt();
}


// Update the tooltip box
function utt(e)
{
	x = (document.all) ? window.event.x + document.body.scrollLeft : e.pageX;
	y = (document.all) ? window.event.y + document.body.scrollTop  : e.pageY;
	if (mytt != null) {
		var winW = 0, winH = 0;
		var boxW = mytt.clientWidth + 25, boxH = mytt.clientHeight + 25;

		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;
		}

		if (x + boxW + 15 >= winW)
			x -= boxW;
		else
			x += 15;

		if (y + boxH + 15 >= winH)
			y -= boxH;
		else
			y += 15;

		myx = x; myy = y;

		mytt.style.left = x + "px";
		mytt.style.top 	= y + "px";
	}
}

// Show tooltip
function stt(id)
{
	if (mytt) {
		mytt.style.display = "none";
	}

	mytt = document.getElementById("tt"+id);
	
	mytt.style.left = myx + "px";
	mytt.style.top 	= myy + "px";
	mytt.style.display = "block";
}

// Hide tooltip
function htt() {
	if (mytt) {
		mytt.style.display = "none";
	}
}


// Highlight one location (by index number) during page load
var httID;
var httST = 0;
function httBlink(id)
{
	httST = !httST;
	var mcolor = httST ? "white" : "black";
	shc("maploc"+id, mcolor);
	shc("listloc"+id, mcolor);
}


function httOnLoad()
{
	var s = window.location.href;
	if (s.indexOf("?") >= 0) {
		var n = unescape(s.substr(s.indexOf("?")+1).toLowerCase());
		httID = setInterval("httBlink('"+n+"')", 500);
	}
}

