/*

Based on X-Business javascript

*/

var hint_timer = null;
var elCurrentIcon = null;
var close_hint_timer = null;

function showHintBox(elm, str, delay, width) {
	if (str == '') {
		return;
	}

	elCurrentIcon = elm;
	elm.style.cursor= "pointer";
	elm.onmouseout = function () {
		close_hint_timer = setTimeout('hideHintBox(elCurrentIcon)', 100);
	}

	var delay = arguments.length >=2 ? arguments[2] : 0;
	var width = arguments.length >=3 ? arguments[3] : 0;

	if (close_hint_timer != null) {
		clearTimeout(close_hint_timer);
		close_hint_timer = null;
	}
  elHintBox = document.getElementById('idHintBox');
  if (elHintBox)
	 	hideHintBox(elHintBox);

	if( hint_timer != null ) {
		clearTimeout( hint_timer );
    hint_timer = null;
	}

	if( delay > 0 ) {
		hint_timer = window.setTimeout(
    	function() {
				showHintBox(elm, str, 0, width)
			},
			delay);
		return;
	}

	var doc_width = document.body.clientWidth - 30;
	var doc_height = document.body.clientHeight - 30; 

	var hint = window.document.createElement('span');

	hint.id = "idHintBox";
	hint.name = "hint_box";
	hint.className = 'hint_box';
	hint.innerHTML = str;
	hint.style.visibility = "hidden";
	hint.onmouseout = function () {
		hideHintBox(this);
	}
	hint.hasFocus = false;
	hint.testField = 'super';
	hint.onmouseover = function() {
		this.hasFocus = true;
	}
	window.document.body.appendChild(hint);

	var hint_width = hint.offsetWidth;
	if (width > 0) {
		hint_width = width;
	}
	var hint_height = hint.offsetHeight;
	if (hint_width >= doc_width - 50) {
		hint_width = doc_width - 50;
		hint.style.width = hint_width+'px';
		hint_width = hint.offsetWidth;
	}
	if (hint_height >= doc_height - 50) {
		hint_height = doc_height - 50;
		hint.style.height = hint_height+'px';
		hint_height = hint.offsetHeight;
	}
  
	elm.xb_hint = hint;

	var left_offset = elm.offsetLeft;
	var top_offset = elm.offsetTop;
	
	var obj = elm;
	while ((obj = obj.offsetParent) != null) {
		left_offset += obj.offsetLeft;
		top_offset += obj.offsetTop;
	}
   
	left_offset -= window.document.body.scrollLeft;
	top_offset -= window.document.body.scrollTop;

	if (left_offset + hint_width < doc_width) {
		hint.style.left = left_offset + window.document.body.scrollLeft+'px';
	} else {
		hint.style.left = doc_width - hint_width + window.document.body.scrollLeft+'px';
	}

	if (top_offset + elm.offsetHeight + hint_height < doc_height) {
		hint.style.top = top_offset + elm.offsetHeight + window.document.body.scrollTop+'px';
	} else {
		hint.style.top = top_offset - hint_height + window.document.body.scrollTop+'px';
	}

	hint.style.width = hint_width+'px';
	hint.style.visibility = "visible";
} 

function hideHintBox(elm) {
    if( hint_timer != null ) {
        clearTimeout(hint_timer);
        hint_timer = null;
    }
	if (elm.name == 'hint_box') {
		var hint = elm;
	} else {
		var hint = elm.xb_hint;
		if (hint.hasFocus) {
			return;
		}
	}

	if (!hint || !hint.parentNode) {
		return;
	}
	hint.style.visibility = "hidden";
	hint.parentNode.removeChild(hint);
}