
function Float(){};

Float.prototype.target=null;
Float.prototype.speed=1;
Float.prototype.frame=1;
Float.prototype.type=0;
Float.prototype.interval=null;
Float.prototype.UD="DOWN";
Float.prototype.LR="RIGHT";
Float.prototype.randomInit=true;

Float.prototype.init=function(){
	Float.setDefault(this);
	if(this.randomInit)
		Float.randomSet(this);
};

Float.prototype.start=function(){
	var base=this;
	base.interval=setInterval(function(){
		Float.doFloat(base);
		try{
			Float.doCollision(base);
		}
		catch(e){}
	},base.speed);
};

Float.prototype.stop=function(){
	clearInterval(this.interval);
};

Float.prototype.open=function(){
	Float.$(this.target).style.display="block";
};

Float.prototype.close=function(){
	Float.$(this.target).style.display="none";
};

Float.setDefault=function(float){
	var t=this.$(float.target);
	if(!t)
		return this.onError(0);
	t.style.position="absolute";
	if(t.style.zIndex==0)
		t.style.zIndex=1;
	if(!t.style.top || t.style.top=="")
		t.style.top=0;
	if(!t.style.left || t.style.left=="")
		t.style.left=0;
	return true;
};

Float.randomSet=function(float){
	var LR=["LEFT","RIGHT"];
	var UD=["UP","DOWN"];
	float.LR=LR[parseInt(Math.random()*2)];
	float.UD=UD[parseInt(Math.random()*2)];
	var t=this.$(float.target);
	if(document.compatMode && document.compatMode!="BackCompat"){
		t.style.top=parseInt(Math.random()*(document.documentElement.clientHeight-t.offsetHeight))+1+"px";
		t.style.left=parseInt(Math.random()*(document.documentElement.clientWidth-t.offsetWidth))+1+"px";
	}
	else{
		t.style.top=parseInt(Math.random()*(document.body.clientHeight-t.offsetHeight))+1+"px";
		t.style.left=parseInt(Math.random()*(document.body.clientWidth-t.offsetWidth))+1+"px";
	}
};

Float.doFloat=function(float){
	var t=this.$(float.target);
	var top=parseInt(t.style.top);
	var left=parseInt(t.style.left);
	var tW=t.offsetWidth;
	var tH=t.offsetHeight;
	var cW,cH,sW,sH;
	if(float.type==0){
			sW=0;
			sH=0;
			cW=document.body.scrollWidth;
			cH=document.body.scrollHeight;
	}
	else{
		if(document.compatMode && document.compatMode!="BackCompat"){
			sW=document.documentElement.scrollLeft;
			sH=document.documentElement.scrollTop;
			cW=document.documentElement.clientWidth+sW;
			cH=document.documentElement.clientHeight+sH;
		}
		else{
			sW=document.body.scrollLeft;
			sH=document.body.scrollTop;
			cW=document.body.clientWidth+sW;
			cH=document.body.clientHeight+sH;
		}
	}
	if(float.UD.toUpperCase()=="DOWN"){
		if(top+t.offsetHeight+float.frame<cH)
			top+=float.frame;
		else{
			top=cH-t.offsetHeight;
			float.UD="UP";
		}
	}
	else{
		if(top-float.frame>sH)
			top-=float.frame;
		else{
			top=sH;
			float.UD="DOWN";
		}
	}
	if(float.LR.toUpperCase()=="RIGHT"){
		if(left+t.offsetWidth+float.frame<cW)
			left+=float.frame;
		else{
			left=cW-t.offsetWidth;
			float.LR="LEFT";
		}
	}
	else{
		if(left-float.frame>sW)
			left-=float.frame;
		else{
			left=sW;
			float.LR="RIGHT";
		}
	}
	t.style.top=top+"px";
	t.style.left=left+"px";
};

Float.addCollision=function(i,f){
	try{
		this.floats.push({group:i,float:f});
		f.group=i;
	}
	catch(e){
		return this.onError(1);
	}
};

Float.$=function(t){
	if(typeof(t)!="object")
		t=document.getElementById(t);
	return t;
};

Float.onError=function(e){
	switch(e){
		case 0:alert("target!");break;
		case 1:alert("");break;
		default:alert(e);
	}
	return false;
}
