var hw = document.window;


/*
 * Pop up a help window for the given help ID.
 */
function helpwin(t) {
	var url;
	if (document.URL.indexOf('?') > 0) {
		url = document.URL.slice(0,document.URL.indexOf('?'));
	} else {
		url = document.URL;
	}
	hw = window.open(url+'?action=help;item='+t,
	  'hw',
	  'resizable=yes,scrollbars=yes,toolbar=no,menubar=no,' +
	  'status=no,width=400,height=300'
	);
	setTimeout("hw.focus();", 10);
	setTimeout("hw.focus();", 100);
	return false;
}


/*
 * Pop up a help window with the given content.
 */
function helpwin_content(t) {
	hw = window.open('about:blank',
	  'hw',
	  'resizable=yes,scrollbars=yes,toolbar=no,menubar=no,' +
	  'status=no,width=400,height=300'
	);
	setTimeout("hw.focus();", 10);
	setTimeout("hw.focus();", 100);
	hw.document.write ('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'+'\n'+
'<html><head><title>Pop-up Help</title>'+'\n'+
'<style type="text/css">'+'\n'+
'body { background: white; color: black; }'+'\n'+
'body,div,p,table,th,td,ul,ol,li,dl,dt,dd { font-size: 10pt; font-family: sans-serif; }'+'\n'+
'dt { font-weight: bold; }'+'\n'+
'a:link    { color: #0000FF; }'+'\n'+
'a:visited { color: #4400FF; }'+'\n'+
'a:active  { color: #0000A0; }'+'\n'+
'a:hover   { color: #8888FF; }'+'\n'+
'ul.spaced li { margin-top: 1em; }'+'\n'+
'span.example {'+'\n'+
'	display: block;'+'\n'+
'	margin: 0.2em 1em 0.2em 1em;'+'\n'+
'	padding: 0.2em;'+'\n'+
'	background-color: #ddd;'+'\n'+
'	color: #000;'+'\n'+
'}'+'\n'+
'@media print {'+'\n'+
'	.noprint { display: none; }'+'\n'+
'}'+'\n'+
'</style>'+'\n'+
'</head>'+'\n'+
'<body>'+'\n'+
'<table border="0" width="100%" class="noprint"><tr>'+'\n'+
'<td style="text-align: left;"><b>[<a href="javascript:window.close();">Close</a>]</b></td>'+'\n'+
'<td style="text-align: right;"><b>[<a href="javascript:window.print();">Print</a>]</b></td>'+'\n'+
'</tr></table>'+'\n'+
'<p>'+'\n'+
t+
'<table border="0" width="100%" class="noprint"><tr>'+'\n'+
'<td style="text-align: left;"><b>[<a href="javascript:window.close();">Close</a>]</b></td>'+'\n'+
'<td style="text-align: right;"><b>[<a href="javascript:window.print();">Print</a>]</b></td>'+'\n'+
'</tr></table>'+'\n'+
'</body></html>\n');
	return false;
}


/*
 * Return the style object for the element with the given ID.
 */
function object_get(name) {
	if (document.getElementById) {
		if (document.getElementById(name))
			return document.getElementById(name).style;
		return false;
	} else if (document.all) {
		if (document.all[name])
			return document.all[name].style;
		return false;
	} else if (document.layers) {
		return document.layers[name];
	} else {
		return false;
	}
}


/*
 * Change the HTML content of the given element.
 */
function change_element_content(elem, content) {
	if (!content)
		content = '';
	if (!elem)
		return;
	if (document.all) {
		elem.innerHTML = content;
	} else if (document.getElementById) {
		var r, h;
		r = document.createRange();
		r.setStartBefore(elem);
		h = r.createContextualFragment(content);
		while (elem.hasChildNodes()) {
			elem.removeChild(elem.lastChild);
		}
		elem.appendChild(h);
	}
}


/*
 * Change the HTML content of the element with the given name.
 */
function change_content(elemname, content) {
	if (!elemname)           
		return;
	if (document.getElementById)
		change_element_content(document.getElementById(elemname), content);
}


/*
 * Pop up the element with the given ID at the current pointer position.
 */
function popuphere(id, event, offsetX, offsetY) {
	var elem, scrollTop, scrollLeft, windowWidth, windowHeight, newLeft, newTop;

	elem = document.getElementById(id);
	if (!elem)
		return;

	scrollTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
	scrollLeft = document.body.scrollLeft ? document.body.scrollLeft : document.documentElement.scrollLeft;
	windowWidth = document.body.clientWidth ? document.body.clientWidth : window.innerWidth;
	windowHeight = document.body.clientHeight ? document.body.clientHeight : window.innerHeight;

	/*
 	 * We position the context menu with its top left corner at the
 	 * pointer position, unless the height of the menu would take it off
 	 * the bottom of the visible screen, in which case we position it
 	 * with its bottom left corner at the pointer instead.
	 *
	 * In order to do this we need to calculate the height of the menu,
	 * which can't be done (in Firefox) if it is "display: none", so we
	 * have to set it to "visibility: hidden" and "display: block"
	 * temporarily. In order to stop a scrollbar temporarily appearing
	 * as a result of this, we first set the position to the top left of
	 * the document before moving it to its correct place and then
	 * finally making it visible.
	 */
	elem.style.top = '0px';
	elem.style.left = '0px';
	elem.style.visibility = 'hidden';
	elem.style.display = 'block';

	if (event.clientX + offsetX + elem.offsetWidth >= windowWidth) {
		newLeft = event.clientX + scrollLeft + offsetX - (event.clientX + elem.offsetWidth - windowWidth) - 30;
	} else {
		newLeft = event.clientX + scrollLeft + offsetX;
	}
	if (event.clientY + offsetY + elem.offsetHeight >= windowHeight) {
		newTop = event.clientY + scrollTop + offsetY - elem.offsetHeight;
		if (newTop + elem.offsetHeight + 5 - event.clientY > 0) {
			newTop -= (newTop + elem.offsetHeight + 5  - event.clientY);
		}
	} else {
		newTop = event.clientY + scrollTop + offsetY;
	}

	elem.style.left = new String(newLeft) + 'px';
	elem.style.top = new String(newTop) + 'px';
	elem.style.visibility = 'visible';
}


/*
 * Show a tooltip pop-up with the given content in response to a mouse
 * event.
 */
function tooltip_show(event, content) {
	if (!object_get('tooltip')) {
		var elem;
		elem = document.createElement('div');
		elem.id = 'tooltip';
		document.body.insertBefore(elem,document.body.childNodes[0]);
	}
	change_content('tooltip', content);
	popuphere('tooltip', event, 20, 20);
}


/*
 * Hide the currently active tooltip.
 */
function tooltip_hide() {
	var tt;
	tt = object_get('tooltip');
	if (tt) {
		tt.visibility = 'hidden';
		tt.display = 'none';
	}
}
