var IE = document.all ? true : false;

var mouse_xpos = 0;
var mouse_ypos = 0;
var mouse_pos_init = false;
var mouse_help_delay = 300 ;

var max_x = 0;

var max_y = 0;

var help_active = 0;

var mousehelp = null;

var top_scroll = 0;

var left_scroll = 0;

var stringer_width = 0;
var default_width = 300;
stringer_width = default_width;


var stringer_height = 0;

var vector_x = 20;
var vector_y = 20;

var mhint_min_width = 40;



//if (!IE) document.captureEvents(Event.MOUSEMOVE);

function get_mouse_xy ( e ) {
	if ( ( help_active == 1 ) || ( mouse_pos_init == false ) ) {
		prepare_scrolls();
		if (IE) {
			mouse_xpos = event.clientX + left_scroll;
			mouse_ypos = event.clientY + top_scroll;
		} else {
			mouse_xpos = e.pageX;
			mouse_ypos = e.pageY;
		}
		// x left position
		if ( mouse_xpos < 0 ) mouse_xpos = 0;
		else if ( mouse_xpos > max_x - stringer_width - 2 * vector_x )
			mouse_xpos = mouse_xpos - stringer_width - vector_x;
		else mouse_xpos = mouse_xpos + vector_x ;
		// y top position
		if ( mouse_ypos < 0 ) mouse_ypos = 0;
		else if ( mouse_ypos < top_scroll + stringer_height + vector_y ) mouse_ypos = mouse_ypos + vector_y;
		else mouse_ypos = mouse_ypos - stringer_height - vector_y ;
		// set positions
		if ( help_active == 1 ) mousehelp_pos();
		mouse_pos_init = true;
	}
}
document.onmousemove = get_mouse_xy;



get_top_scroll = function()
{
	try
	{
		if (document.documentElement && document.documentElement.scrollTop)
			top_scroll = document.documentElement.scrollTop;
		else
			top_scroll = document.body.scrollTop;
	}
	catch(e) { top_scroll = 0; }

	return top_scroll;
}


get_left_scroll = function()
{
	try
	{
		if (document.documentElement && document.documentElement.scrollLeft)
			left_scroll = document.documentElement.scrollLeft;
		else
			left_scroll = document.body.scrollLeft;
	}
	catch(e) { left_scroll = 0; }

	return left_scroll;
}


prepare_scrolls = function()
{
	get_top_scroll();
	get_left_scroll();
}



function getWindowSize ( what ) {
	var browser_width = 0;
	var browser_height = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
			browser_width = window.innerWidth;
			browser_height = window.innerHeight;
	} else if (
		document.documentElement
		&& ( document.documentElement.clientWidth || document.documentElement.clientHeight )
	) {
			//IE 6+ in 'standards compliant mode'
			browser_width = document.documentElement.clientWidth;
			browser_height = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			browser_width = document.body.clientWidth;
			browser_height = document.body.clientHeight;
	}
	switch ( what ) {
		case 'width' : return browser_width; break;
		case 'height' : return browser_height; break;
	}
}


function mousehelp_size () {
	if ( help_active == 1 ) {
		stringer_width = default_width;
		var test_width = document.getElementById('mousehelp_text').offsetWidth;
		if ( test_width < stringer_width ) stringer_width = test_width;
		if ( ( test_width > stringer_width ) && ( test_width < default_width ) ) stringer_width = test_width;
		stringer_height = document.getElementById('mousehelp_text').offsetHeight;
		if ((mhint_min_width > 0) && (stringer_width < mhint_min_width))
		{
			stringer_width = mhint_min_width;
		}
		mousehelp.style.width = stringer_width + 'px';
		mousehelp.style.height = stringer_height + 'px';
	}
}


function mousehelp_on ( show_text ) {
	try {
		if ( mouse_pos_init == false ) return;
		mousehelp_off();
		prepare_scrolls();
		max_x = getWindowSize('width') + left_scroll;
		max_y = getWindowSize('height') + top_scroll;
		if ( !mousehelp )
		{
			mousehelp = document.getElementById('mousehelp');
			mousehelp.style.position = 'absolute' ;
		}
		var stringer = "";
		switch ( show_text ) {
			case 'null': stringer= ''; break;
			default: stringer = show_text; break;
		}
		mousehelp.innerHTML = '<table border="0"><tr><td id="mousehelp_text" nowrap="nowrap"><b id="mousehelp_head">' + txtmousehelphead + '</b>' + stringer + '</td></tr></table>';
		help_active = 1;
		try {
			get_mouse_xy(event);
		} catch (e) { }
		mousehelp_size();
		mousehelp_pos();
		window.setTimeout("if ( help_active == 1 ) mousehelp.style.visibility = 'visible';",mouse_help_delay);
		//window.setTimeout("mousehelp_pos()",mouse_help_delay/2);
	}
	catch ( e )
	{ }
}

mousehelp_pos = function()
{
	mousehelp.style.left = mouse_xpos + 'px';
	mousehelp.style.top = mouse_ypos + 'px';
}

mousehelp_off = function()
{
	var mhObj = document.getElementById('mousehelp');

	if(mhObj && (typeof(mhObj) == 'object'))
	{
		mhObj.style.visibility = 'hidden';
		mhObj.innerHTML = '';
		mhObj.style.width = 'auto';
		mhObj.style.height = 'auto';
		mhObj.style.left = '0px';
		mhObj.style.top = '0px';
	}
	help_active = 0;
}

function MH_Hint(obj, hint)
{
	if(hint == '') return;

	try
	{
		obj.style.cursor = "help";
		obj.onmouseout = mousehelp_off;

		mousehelp_on(hint);
	}
	catch(e) { }
}

function MH_HintNoCursor(obj, hint)
{
	if(hint == '') return;

	try
	{
		obj.onmouseout = mousehelp_off;

		mousehelp_on(hint);
	}
	catch(e) { }
}

