var TimeOut         = 300;
var currentLayer    = null;
var currentLayerNum = 0;
var currentHelpAnch = null;
var currentHelpDiv = null;
var closeTimer      = null;

function doShowHelp(n, helpDiv, helpAnch)
{
	if (typeof(helpDiv) == 'undefined')
		helpDiv = 'inLineHelp';
		
	if (typeof(helpAnch) == 'undefined')
		helpAnch = 'helpAnch';

 	div2a(helpDiv + n, helpAnch + n);
    var l  = document.getElementById(helpDiv+n);
    if(l)
    {
        stopHideHelp();
        l.style.visibility='visible';

        if(currentLayer && (currentLayerNum != n || currentHelpAnch != helpAnch || currentHelpDiv != helpDiv))
            currentLayer.style.visibility='hidden';

        currentLayer = l;
        currentLayerNum = n;			
		currentHelpAnch = helpAnch;
		currentHelpDiv = helpDiv;
    }
    else if(currentLayer)
    {
        currentLayer.style.visibility='hidden';
        currentLayerNum = 0;
        currentLayer = null;
	}
}

function div2a(div, anch)
{
	var d = document.getElementById(div);
	var a = document.getElementById(anch);
    if (d && a)
	{
		var p = getItemPos(a);
//		d.style.top = (p.y + 40) + "px";
		d.style.top = ((p.y + d.offsetHeight + 40 - document.body.scrollTop) > document.body.offsetHeight)? (p.y - d.offsetHeight - 40) + "px" : (p.y + 40) + "px";
		d.style.left = ((p.x + d.offsetWidth + 40 - document.body.scrollLeft) > document.body.offsetWidth)? (document.body.offsetWidth - document.body.scrollLeft - d.offsetWidth - 40) + "px" : (p.x + 30) + "px";
	}
}

// Turn On Close Timer
function startHideHelp()
{
    closeTimer = window.setTimeout(doHideHelp, TimeOut);
}

// Cancel Close Timer
function stopHideHelp()
{
    if(closeTimer)
    {
        window.clearTimeout(closeTimer);
        closeTimer = null;
    }
}

// Close Showed Layer
function doHideHelp()
{
    if(currentLayer)
    {
        currentLayer.style.visibility='hidden';
        currentLayerNum = 0;
        currentLayer = null;
    }

    currentLayer = null;
}

function getItemPos(elm)
{
	var x = 0, y = 0;
	var base_w, base_h;
	while( elm && elm != document.body )
	{
		x += elm.offsetLeft;
		y += elm.offsetTop;
		elm = elm.offsetParent;
	}
	return { x: x, y: y };
}

// Close Layer Then Click-out
document.onclick = doHideHelp; 
