var distanceFromDx=426;
var distanceFromTop=221;
var siteWidth=776;
var halign="right";
var valign="top";
var delayspeed=3;
var floatX,floatY,ifloatX,ifloatY,layerwidth,layerheight,lastX,lastY;

var IE4=document.all;
var NS6=(!IE4)?document.getElementById:false;
var NS4=document.layers;

function updateFloatX(){
	var x;
	var bodyWidth=window.getBodyVisibleSize()[0];
	var scroll=window.getScrollAmount()[0];
	if(bodyWidth<siteWidth)x=distanceFromDx+9;
	else x=Math.floor((bodyWidth-siteWidth)/2)+distanceFromDx+((document.all)?9:0);
	return x;
}
function updateFloatY(){
	var y,opZeroH,opZeroVisible;
	var scroll=window.getScrollAmount()[1];
	if(zeroValid){
		opZeroVisible=($("opzioneZero4").style.display!="none");
		opZeroH=(opZeroVisible)?Element.getHeight("opzioneZero4"):0;
	}else{
		opZeroH=0;
	}
	if(document.all){
		y=(scroll<=distanceFromTop+opZeroH)?distanceFromTop+opZeroH:scroll;
//		if(y<distanceFromTop+1)y=distanceFromTop+1;
	}else{
		var y=distanceFromTop+opZeroH-scroll;
		y=(y>=0)?y:0;
	}
	return y;
}

function adjust(q) {
//	writeLog(updateFloatY())
	var esceSotto=Element.withinWindow(q)[2];
	var bwh=window.getBodyVisibleSize()[1];	//bodyVisibleHeight
	var sah=window.getScrollAmount()[1];	//bodyVerticalScrollAmount
	var testY;
	floatX=updateFloatX();
	floatY=updateFloatY();
	if ((NS4) || (NS6)) {
		if (lastX==-1 || delayspeed==0){
			lastX=window.pageXOffset + floatX;
			lastY=window.pageYOffset + floatY;
		}else{
			var dx=Math.abs(window.pageXOffset+floatX-lastX);
			var dy=Math.abs(window.pageYOffset+floatY-lastY);
			var d=Math.sqrt(dx*dx+dy*dy);
			var c=Math.round(d/10);
			if (window.pageXOffset+floatX>lastX) {lastX=lastX+delayspeed+c;}
			if (window.pageXOffset+floatX<lastX) {lastX=lastX-delayspeed-c;}
			if (window.pageYOffset+floatY>lastY) {lastY=lastY+delayspeed+c;}
			if (window.pageYOffset+floatY<lastY) {lastY=lastY-delayspeed-c;}
		}
/*
		if (NS4){
			document.layers[q].pageX = lastX;
			document.layers[q].pageY = lastY;
		}
		if (NS6){
			document.getElementById(q).style.left=lastX;
			document.getElementById(q).style.top=lastY;
		}
*/
	}else if (IE4){
		if (lastX==-1 || delayspeed==0){
			lastX=document.body.scrollLeft + floatX;
			lastY=document.body.scrollTop + floatY;
		}else{
			var dx=Math.abs(document.body.scrollLeft+floatX-lastX);
			var dy=Math.abs(document.body.scrollTop+floatY-lastY);
			var d=Math.sqrt(dx*dx+dy*dy);
			var c=Math.round(d/10);
			if (document.body.scrollLeft+floatX>lastX) {lastX=lastX+delayspeed+c;}
			if (document.body.scrollLeft+floatX<lastX) {lastX=lastX-delayspeed-c;}
			if (document.body.scrollTop+floatY>lastY) {lastY=lastY+delayspeed+c;}
			if (document.body.scrollTop+floatY<lastY) {lastY=lastY-delayspeed-c;}
		}
/*		document.all[q].style.posLeft = lastX;
		document.all[q].style.posTop = lastY;
*/
	}
//	var lastY=(lastY+layerheight-sah<bwh)?lastY:(bwh-layerheight+sah);
//	if(lastY<floatY)lastY=floatY;
//	alert(updateFloatX()+"=="+lastX+" => "+(updateFloatX()==lastX))
	Element.setPos(q,[updateFloatX()-1,lastY]);
//	writeLog([lastX,lastY])
	setTimeout("adjust('"+q+"');",50);
}

function define(){
	if ((NS4) || (NS6)){
		if (halign=="left") {floatX=ifloatX};
		if (halign=="right") {floatX=window.innerWidth-ifloatX-layerwidth-20};
		if (halign=="center") {floatX=Math.round((window.innerWidth-20)/2)-Math.round(layerwidth/2)};
		if (valign=="top") {floatY=ifloatY};
		if (valign=="bottom") {floatY=window.innerHeight-ifloatY-layerheight};
		if (valign=="center") {floatY=Math.round((window.innerHeight-20)/2)-Math.round(layerheight/2)};
	}
	if (IE4){
		if (halign=="left") {floatX=ifloatX};
		if (halign=="right") {floatX=document.body.offsetWidth-ifloatX-layerwidth-20}
		if (halign=="center") {floatX=Math.round((document.body.offsetWidth-20)/2)-Math.round(layerwidth/2)}
		if (valign=="top") {floatY=ifloatY};
		if (valign=="bottom") {floatY=document.body.offsetHeight-ifloatY-layerheight}
		if (valign=="center") {floatY=Math.round((document.body.offsetHeight-20)/2)-Math.round(layerheight/2)}
	}
}

function floatingLayer(q){
	q=$(q);
	layerwidth=Element.getWidth(q);
	layerheight=Element.getHeight(q);
	q.style.position="absolute";
//	alert([updateFloatX(),updateFloatY()])
	Element.setPos(q,[updateFloatX(),updateFloatY()]);// quel -2 resta un mistero! al primo tentativo sbaglia, non so perchè.
	if (NS4){
		if (q.tagName!="LAYER"){
			alert("You need a LAYER, not a "+q.tagName);
			return;
		}
	}if ((IE4) || (NS6)){
		if (q.tagName!="DIV"){
			alert("You need a DIV, not a "+q.tagName);
			return;
		}
	}
	ifloatX=floatX;
	ifloatY=floatY;
	define();
	window.onresize=define;
	lastX=-1;
	lastY=-1;
	adjust(q.id);
}