/*
	Smart Hints by Michiel 'El Muerte' Hendriks
	This script will convert all title tag arguments to nicer hints. This will allow you do give
	some additional customization to the hint window but also grant you the power of newlines in
	the hints (only MSIE did it by default, now all browsers will do so).

	All you will have to do is include this script in your webpage. And add the following HTML
	to the end of the document:

	<div id="hintwindow" style="display: none; position: absolute; background-color: InfoBackground; border: 1px solid InfoText; color: InfoText; padding: 1px; font-size: 90%;"><div id="hintwindowtext">&nbsp;</div></div>

	By default the hint window will have the systems default hint window colors. But you can easily
	change it by adding #hintwindow and #hintwindowtext sections to your stylesheet(s).
*/

/*
    Smart Hints
    Copyright (C) 2005  Michiel Hendriks <elmuerte@drunksnipers.com>

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    http://www.fsf.org/licensing/licenses/lgpl.txt
    http://www.gnu.org/licenses/lgpl.txt

*/

/**
	append this style class to modified tags, you can use it to for example change the 
	cursor to a help cursor or add a unline. You must add this style class to your 
	style sheet.
*/
var styleclass = 'withhint';
/** only modify these tags, needs to be uppercase */
var stylemodfor = new Array('SPAN', 'ABBR');
/** never make a hint for these tags */
var neverhintfor = new Array();

/** 
	milliseconds before showing a hint, shouldn't be to low or to high, 
	with 10 the hint will follow the mouse, with 500 it behaves like in MS Windows
*/
var hintShowDelay = 500;
/** time the hint stays visible, can be 0 */
var hintHideDelay = 5000;
/** pixel offset from the cursor to show the hint */
var hintOffset = 22;

// internal variables -- don't touch
var hintwindowtext;
var hintdiv;
var hintCurrentSource;
var hintTimerHide;
var hintTimerVisible;
var hintposX, hintposY;

/** queue the hint for being shown */
function setupHint(event, source)
{
	if (!hintdiv) hintdiv = document.getElementById('hintwindow');
	if (!hintwindowtext) hintwindowtext = document.getElementById('hintwindowtext');
	if (!hintdiv || !hintwindowtext) return; // your forgot something
	if (source.hint == "") return;

	if (hintCurrentSource != source)
	{
		while (hintwindowtext.childNodes.length > 0)
		hintwindowtext.removeChild(hintwindowtext.firstChild);
		hintCurrentSource = source;
		hintlines = source.hint.split(/\n/g);
		for (i = 0; i < hintlines.length; i++)
		{
			hinttext = document.createTextNode(hintlines[i]);
			hintwindowtext.appendChild(hinttext);
			brtag = document.createElement('BR');
			hintwindowtext.appendChild(brtag);
		}
	}	
		
	if (event.pageY)
	{		
		hintposY = (event.pageY+hintOffset);
		hintposX = event.pageX+hintOffset;
	}
	else if (event.clientY)
	{
		hintposY = (event.clientY+hintOffset);
		hintposX = event.clientX+hintOffset;
	}
	
	clearTimeout(hintTimerHide);
	clearTimeout(hintTimerVisible);
	hintTimerVisible = setTimeout('showHint()', hintShowDelay);
}

/** clear all hint stuff */
function clearHint()
{
	clearTimeout(hintTimerHide);
	clearTimeout(hintTimerVisible);
	hideHint();
}

/** actually shows the hint */
function showHint()
{
	hintdiv.style.width = '';
	hintdiv.style.left = (-2*document.body.clientWidth)+'px'; // trick to render the hint properly before showing
	hintdiv.style.top = hintposY+'px';
	hintdiv.style.display = '';
	if (hintdiv.clientWidth > document.body.clientWidth-hintOffset)
	{
		hintdiv.style.width = (document.body.clientWidth-hintOffset)+'px';
	}
	if (hintdiv.clientWidth + hintposX < document.body.clientWidth-2)
		hintdiv.style.left = hintposX+'px';
	else
		hintdiv.style.left = (document.body.clientWidth-hintdiv.clientWidth-2)+'px';
	if (hintHideDelay > 0) hintTimerHide = setTimeout('hideHint()', hintHideDelay);
}

/** will hide the hint */
function hideHint()
{
	hintdiv.style.display = 'none';
}

/** set the hint events for this element */
function SetupHintFor(elm)
{
	for (var i = 0; i < neverhintfor.length; i++)
	{
		if (neverhintfor[i] == elm.nodeName) return;
	}
	for (var i = 0; i < stylemodfor.length; i++)
	{
		if (stylemodfor[i] == elm.nodeName)
		{
			elm.className = elm.className+' '+styleclass;
			break;
		}
	}
	// only want newlines
	elm.hint = elm.title.replace(/\r/g, "");
	elm.title = '';
	elm.oldonmousemove = elm.onmousemove;
	elm.onmousemove = function(event){ if (this.oldonmousemove) this.oldonmousemove(event); return setupHint(event?event:window.event, this); };
	elm.oldonmouseout = elm.onmouseout;
	elm.onmouseout = function(event){ if (this.oldonmouseout) this.oldonmouseout(event); return clearHint(); };	
}

function SetupHints()
{
	allelm = document.getElementsByTagName('*');
	for (var i = 0; i < allelm.length; i++)
	{
		if (allelm[i].title != '')
			SetupHintFor(allelm[i]);
	}
}

var oldWindowOnLoad = window.onload;
window.onload = function(){ if (oldWindowOnLoad) oldWindowOnLoad(); return SetupHints(); };	
