/**@file
	Yualah: sede
	(c) Yualah, Roberto González Vázquez, Jose Manuel Pérez Fernández - all rights reserved
	_yualah@yahoo.es_
	@author Roberto González Vázquez,  Jose Manuel Pérez Fernández
	@version 1.1
*/

function YH_SEDE_popup ( obj, ancho, alto, usar_visibility )
{
	var tam = { x:0, y:0 };
	
	if ( ( ancho==null ) || ( alto==null ) ) tam = YH_SEDE_tamano (obj);
	if ( ancho != null ) tam.x = ancho;
	if ( alto != null ) tam.y = alto;
	
	var pos_x = ( document.body.clientWidth - tam.x ) / 2 + document.body.scrollLeft;
	var pos_y = ( document.body.clientHeight - tam.y ) / 2 + document.body.scrollTop;
	
	YH_SEDE_posicion ( obj, pos_x, pos_y );
	YH_SEDE_tamano ( obj, tam.x, tam.y );

	if ( usar_visibility )	obj.style.visibility = 'visible';
	else					obj.style.display = 'block';
	
	obj.cerrar = true;
	obj.onmousedown = function () { obj.cerrar = false; }
	document.onmousedown = function () { YH_SEDE_popup_ocultar(obj,usar_visibility) }
}

function YH_SEDE_popup_ocultar ( obj, usar_visibility )
{
	if ( !obj.cerrar )
	{
		obj.cerrar = true;
		return;
	}

	if ( usar_visibility )	obj.style.visibility = 'hidden';
	else					obj.style.display = 'none';
	
	document.onmousedown = null;
}


function YH_SEDE_menu_mostrar ( obj_pivote, obj_menu )
{
	var pos		= YH_SEDE_posicion ( obj_pivote );
	var tamano	= YH_SEDE_tamano ( obj_pivote );
	
	YH_SEDE_posicion ( obj_menu, pos.x, pos.y+tamano.y );
	obj_menu.style.display = 'block';
	obj_menu.contador = 1;
	obj_menu.onclick = function () { obj_menu.contador++; }
	document.onclick = function () { YH_SEDE_menu_ocultar(obj_menu) }
}


function YH_SEDE_menu_ocultar ( obj_menu )
{
	obj_menu.contador--;
	if (obj_menu.contador<0)
	{
		obj_menu.style.display='none';
		document.onclick=null;
	}
}


function YH_SEDE_posicion ( obj, x, y )
{
	if ( x==null && y==null )
	{
		var x = y = 0;
		while ( obj != null )
		{
			x += obj.offsetLeft	;
			y += obj.offsetTop	;
			obj = obj.offsetParent;
		}
		return { x:x, y:y }
	}

	if ( x != null ) obj.style.left	=  x + "px" ;
	if ( y != null ) obj.style.top	=  y + "px" ;
}



function YH_SEDE_tamano ( obj, x, y )
{
	if ( x==null && y==null )
		return { x:obj.offsetWidth, y:obj.offsetHeight }
	
	if ( x != null ) obj.style.width	= x + "px" ;
	if ( y != null ) obj.style.height	= y + "px" ;
}     



function YH_SEDE_onkey (suceso)
{
	var codigo  = (suceso) ? suceso.which : window.event.keyCode;
	var nombre = String.fromCharCode(codigo);
	return {codigo:codigo , nombre:nombre };
}


function YH_SEDE_onmouse (suceso)
{
	var e;
	if (!suceso) var suceso = window.event;
	
	try 
	{
		if (suceso.pageX || suceso.pageY)
			return	{	pagina_x	: suceso.pageX , 
						pagina_y 	: suceso.pageY
					};
		else 
			return	{	pagina_x	: suceso.clientX + document.body.scrollLeft,  
						pagina_y 	: suceso.clientY + document.body.scrollTop
					};
		
	}
	
	catch (e)
	{
		return {pagina_x:0, pagina_y:0}
	}
}


function YH_SEDE_false()
{
	return false;
}