/**
*	Capita loca que se muestra al lado del mouse
*	Se necesita el archivo functions.js
*	Inicializar las coordenadas del mouse (mouseX, mouseY) antes!
*	By CaraJodida ;)
*/

// tiempo que tarda en ocultar DIV
var hideTimeout	= 600;
// ocultar DIV haciendo click
var hideByClick	= false;

var showDiv		= false;
var divRef		= null;
var timeoutRef	= null;
/**
*	@param	referencia al elemento que llama a la funcion
*	@param	ID del div  a mostrar
*/
function showDynDiv(target, divID)
{
	// ocultamos div anterior si existe
	if (divRef && divRef.id != divID) divRef.style.display = 'none';
	// referenciamos nuevo div si existe
	if (!document.getElementById(divID)) return;
	divRef = document.getElementById(divID);
	if (hideByClick)
	{
		if (!IE) document.body.addEventListener('click', function() { hideDynDiv(1); }, false);
		else document.body.attachEvent('onclick', function() { hideDynDiv(1); });
	}
	// flag que indica no ocultar el div
	showDiv = true;
	target.onmouseout = initHide;
	divRef.onmouseover = function() { showDiv = true; }
	divRef.onmouseout = initHide;

	// mostrar capa
	var bodyMax = getMaxXY();
	var scrollMax = getScrollXY();
	divRef.style.display = 'block';
	var w = divRef.offsetWidth;
	var h = divRef.offsetHeight;
	var maxX = bodyMax[0] + scrollMax[0] - w - 20;
	var maxY = bodyMax[1] + scrollMax[1] - h/2;
	if (mouseX > maxX) divRef.style.left = mouseX - w - 20 + 'px';
	else divRef.style.left = mouseX + 20 + 'px';
	if (mouseY > maxY) divRef.style.top = maxY + 'px';
	else divRef.style.top = mouseY - (h/2) + 'px';
}
function initHide()
{
	showDiv = false;
	// si habia un timeout lo cancelamos o te va a andar mal...
	if (timeoutRef) clearTimeout(timeoutRef);
	timeoutRef = setTimeout(hideDynDiv, hideTimeout);
}
function hideDynDiv(override)
{
	// ocultar div
	if (override == 1) showDiv = false;
	if (showDiv == false && divRef)
	{
		divRef.style.display = 'none';
		divRef = null;
	}
}