FSite2._helperInitClose = function()
{
	if (!this._timeout)
		this._timeout = setTimeout(FSite2._callRef(FSite2._helperClose, this), 100);
}

FSite2._helperCancelClose = function(helper)
{
	if (this && !helper)
		helper = this;
	if (helper._timeout)
	{
		clearTimeout(helper._timeout);
		helper._timeout = null;
	}
}

FSite2._helperClose = function(helper)
{
	if (!helper)
		helper = this;
	helper.style.display = 'none';
	helper.style.visibility = 'hidden';
	if (helper.parentNode)
		helper.parentNode.removeChild(helper);
	helper._element._helpBox = null;
}

FSite2.extendHelpers = function(node, params)
{
	if (!node)
		node = document.body;
	var bd = FSite2.getElementsByClassName(params.helperClass, node);
	if (!params.helperElementId)
		params.helperElementId = 'FSite2_helper';
	for (var i = 0, a = [], d; d = bd[i++];)
	{
		if (d._helper) continue;
		FSite2.extendHelper(d, params);
	}
}

FSite2.extendHelper = function(d, params)
{
	d._helper=true;
	if (d.getAttribute('title'))
	{
		d._helpText = d.getAttribute('title');
/*		d.setAttribute('alt', d.getAttribute('title'));*/
		d.removeAttribute('title');
	}
	else if (d.getAttribute('alt'))
	{
		d._helpText = d.getAttribute('alt');
		d.removeAttribute('alt');
	}

	d._helperParams = new Object();
	d._helperParams["corner"] = params.helperCorner?params.helperCorner:'TL';
	d._helperParams["elementId"] = params.helperElementId?params.helperElementId:'FSite2_helper';
	d._helperParams["elementClass"] = params.helperElementClass?params.helperElementClass:'';
	d._helperParams["elementTemplate"] = params.helperElementTemplate?params.helperElementTemplate:'###';
	d._helperParams["margin"] = params.helperMargin?params.helperMargin:'0';
	
	d._helperPosition = FSite2._helperPosition;
	if (params.helperTrackClass && RegExp('\\b' + params.helperTrackClass + '\\b', 'gi').test(d.className))
	{
		d._helperTrack = true;
		d.onmousemove = function(event)
		{
			if (!this._helpText || !this._helpBox)
				return;
			if (!event)
				event = window.event;
			this._helperPosition(event);
		}
	}
	if (typeof d.onmouseover == 'function')
		d._onmouseover_helper = d.onmouseover;
	d.onmouseover = function (event) {
		if (!this._helpText)
			return;
		if (!event)
			event = window.event;
		var res;
		if (this._onmouseover_helper) res = this._onmouseover_helper(event);
		if (res === false)
			return res;
		var box;
		if (!(box = document.getElementById(params.helperElementId)))
		{
			box = document.createElement('div');
			document.body.appendChild(box);
			box._helperInitClose = FSite2._helperInitClose;
			box._helperInitCancelClose = FSite2._helperInitCancelClose;
			box._helperCancelClose = FSite2._helperCancelClose;
			box._helperClose = FSite2._helperClose;
			box.id = this._helperParams.elementId;
			box.style.position = 'absolute';

			if (!this._helperTrack)
			{
				box.onmouseover = function()
				{
					this._helperCancelClose();
				}
				box.onmouseout = function()
				{
					this._helperInitClose();
				}
			}
		}

		if (this._helperParams['elementClass'])
			box.className = this._helperParams['elementClass'];
		this._helpBox = box;
		this._helpBox._element = this;
		var content = this._helperParams['elementTemplate'].replace(/###/, this._helpText);
		this._helpBox.innerHTML = content;
		this._helper = box;
		this._helper._helperCancelClose();
		d._helperPosition(event);
	}
	if (d.onclick)
		d._onclick_helper=d.onclick;

	d.onclick = function (ev) {
		var res;
		if (!ev) ev = window.event;
		if (this._onclick_helper) res = this._onclick_helper(ev);
		if (this._helpBox) this._helpBox._helperClose();
		if (res!=null)
			return res;
	}

	if (typeof d.onmouseout == 'function')
		d._onmouseout_helper = d.onmouseout;
	d.onmouseout = function (event) {
		var res;
		if (this._onmouseout_helper) res = this._onmouseout_helper(event);
		if (res === false)
			return res;
		if (this._helper && (typeof this._helper == 'object'))
			this._helper._helperInitClose();
	}

	//if(window.addEventListener)
	//  d.addEventListener('click', d.onmouseout, false);
	//else if(window.attachEvent)
	//  d.attachEvent('onclick', d.onmouseout);
}

FSite2._helperPosition = function(event)
{
	var widthScreen = window.getWindowWidth();
	var positionX = getMouseXY(event)[0];
	var positionY = getMouseXY(event)[1];
	var boxHeight= window.getObjectHeight(this._helpBox);
	var boxWidth = window.getObjectWidth(this._helpBox);

	var posLeft;
	var posTop;
	if(this._helperParams["corner"] == 'TR')
	{
		posLeft = positionX - boxWidth;
		posTop = positionY;
	}
	else if(this._helperParams["corner"] == 'BR')
	{
		posLeft = positionX - boxWidth;
		posTop = positionY - boxHeight;
	}
	else if(this._helperParams["corner"] == 'BL')
	{	
		posLeft = positionX;
		posTop = positionY - boxHeight;
	}
	else if(this._helperParams["corner"] == 'TL')
	{
		posLeft = positionX;
		posTop = positionY;
	}

	if (posLeft < this._helperParams['margin'])
		posLeft = this._helperParams['margin'];
	else if (posLeft + boxWidth > widthScreen - this._helperParams['margin'] - 20)
		posLeft = widthScreen - boxWidth - this._helperParams['margin'] - 20;

	this._helpBox.style.top = posTop + 'px';
	this._helpBox.style.left = posLeft + 'px';
}

