

//////////////////////////////////////////////////////////////////////
function $(id) { return document.getElementById(id);}


//////////////////////////////////////////////////////////////////////
function centerDivId ( divId, divWidth, divHeight )
{
	try
	{
		if ( typeof divWidth == "undefined" )	{	divWidth	= 0;	}
		if ( typeof divHeight == "undefined" )	{	divHeight	= 0;	}

		centerDivObj( $( divId ), divWidth, divHeight );
	}
	catch ( e )
	{
	}
}//EndFunction(centerDivId)


//////////////////////////////////////////////////////////////////////
// se non e' possibile autorecuperarle, e' possibile
// forzare le dimensioni dell'oggetto
function centerDivObj ( divObj, divWidth, divHeight )
{
    var dimX		= 0;
    var dimY		= 0;
    var	dimBody		= null;
    var	dimObj		= null;
    
    
    if ( divObj == null )
		return;
		
	dimBody = getBodyDim();
	if ( dimBody == null )
		return;
		
	dimX    = dimBody[0];
    dimY    = dimBody[1];
    
    if ( dimX == 0 && dimY == 0 )
        return;
       
    if ( typeof divWidth == "undefined" )	{	divWidth	= 0;	}
	if ( typeof divHeight == "undefined" )	{	divHeight	= 0;	}
	
    if ( divWidth == 0 || divHeight == 0 )
    {
		dimObj = getDim( divObj );
		if ( dimObj != null )
		{
			divWidth	= dimObj[0];
			divHeight	= dimObj[1];
		}
    }//EndIf(AutoDiv)
    
    if ( dimX > divWidth )
    {
        divObj.style.left = "" + parseInt( Math.round( (dimX - divWidth) / 2 )) + "px";
    }
    
    if ( dimY > divHeight )
    {
        divObj.style.top = "" + parseInt( Math.round( (dimY - divHeight) / 2 )) + "px";
    }
    
}//EndFunction(centerDiv)


/////////////////////////////////////////////////////////////////////////
function getOffsetsFromTop( elm )
{
	try
	{
		var	curleft	= 0;
		var	curtop	= 0;

		do
		{
			curleft	+=	elm.offsetLeft;
			curtop	+=	elm.offsetTop;
			elm		=	elm.offsetParent
		} while ( elm );

		return [curleft,curtop];
	}
	catch ( ex )
	{
		return [0,0];
	}
}//EndFunction(getOffsetsFromTop)


/////////////////////////////////////////////////////////////////////////
function getOffsetsFromParent( elm )
{
	try
	{
		return [elm.offsetLeft, elm.offsetTop];
	}
	catch ( ex )
	{
		return [0,0];
	}
}//EndFunction(getOffsetsFromParent)
	
	
/////////////////////////////////////////////////////////////////////////
function getDim( elm )
{
	try
	{
		var	dimX	= elm.clientWidth;
		var	dimY	= elm.clientHeight;

		return [dimX,dimY];
	}
	catch ( ex )
	{
		return [0,0];
	}
}//EndFunction(getDim)


/////////////////////////////////////////////////////////////////////////
function getBodyDim()
{
	var	dimX	= 0;
	var	dimY	= 0;
		
	try
	{
		try
		{
			dimX    = document.documentElement.clientWidth;
			dimY    = document.documentElement.clientHeight;
		}
		catch ( e )
		{
		}
	    
		if ( dimX == 0 && dimY == 0 )
		{
			try
			{
				dimX    = document.body.clientWidth;
				dimY    = document.body.clientHeight;
			}
			catch ( e )
			{
				return [0,0];
			}
		}

		return [dimX,dimY];
	}
	catch ( ex )
	{
		return [0,0];
	}
}//EndFunction(getBodyDim)
