function get(id){ return document.getElementById(id); }

function windowSize() {
  var myWidth = 0, myHeight = 0, IE;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
    IE=0;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
    IE=1;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
    IE=1;
  }
  
  var res=new Array(myWidth,myHeight,IE);
  return res;
}

function center(id,speed)
{
	var el=get(id);
	var size=windowSize();
	var wpivot=el.offsetLeft+(el.offsetWidth/2);
	var hpivot=el.offsetTop+(el.offsetHeight/2);
	var winwpivot=size[0]/2;
	var winhpivot=size[1]/2;

	if(!speed)
	{
		// posizionamento istantaneo
		var buff;
		if(wpivot!=winwpivot)
		{
			buff=Math.floor(winwpivot-(el.offsetWidth/2));
			if(buff<0)	//per non finire sopra la barra degli strumenti
				buff=0;
			el.style.left=buff+"px";
		}
		if(hpivot!=winhpivot)
		{
			buff=Math.floor(winhpivot-(el.offsetHeight/2));
			if(buff<0)
				buff=0;	//per non finire sopra la barra degli strumenti
			el.style.top=buff+"px";
		}
	}
	else
	{
		// posizionamento interpolato
		if(wpivot<(size[0]/2)-speed)
		{
			el.style.left=(el.offsetLeft+speed)+"px";
			arrived=false;
		}
		else
		{
			if(wpivot>(size[0]/2)+speed)
			{
				el.style.left=(el.offsetLeft-speed)+"px";
				arrived=false;
			}
		}

		if(hpivot<(size[1]/2)-speed)
		{
			el.style.top=(el.offsetTop+speed)+"px";
			arrived=false;
		}
		else
		{
			if(hpivot>(size[1]/2)+speed)
			{
				el.style.top=(el.offsetTop-speed)+"px";
				arrived=false;
			}
		}
	}
	setTimeout("center('"+id+"',"+speed+")",speed);
}

