/*
======================================================
上下浮动和拖拽类:float
参数1:	浮动对象ID
参数2:	浮动对象上边距
参数3:	>0浮动对象左边距;<0浮动对象右边距
参数4:	浮动延迟时间
参数5:	浮动等待时间
调用方法:
1-只浮动new float("objid")
2-浮动并拖拽new float("objid").moveable()
=======================================================
*/

//另一浮动代码
//autoFloat(id) id:浮动对象ID,须为绝对定位对象

function float(){
	if(arguments.length>=1)	this.objID = document.getElementById(arguments[0]);
	if(arguments.length>=2)	this.divTop = arguments[1];
	if(arguments.length>=3) this.divPlane = arguments[2];
	if(arguments.length>=4)	this.scrollDelay = arguments[4];
	if(arguments.length>=5) this.waitTime = arguments[5];
	if(!this.objID){
		alert(""+ arguments[0] +"参数错误");
		this.objID = null; return;
	}else{
		this.objID.style.position="absolute";
		this.objID.style.display="block";
		this.objID.style.zIndex=9999;
		this.objID.style.cursor='move';
		this.objID.title='拖拽 / Drag';
		this.objID.onselectstart=function(){return false;};
	}
	if("" == this.objID.style.top){
		if(isNaN(this.divTop)){
			alert("参数(top)错误"); return;
		}else{
			this.objID.style.top = this.divTop+"px";
		}
	}
	if("" == this.objID.style.left && "" == this.objID.style.right){
		if(isNaN(this.divPlane)){
			alert("参数(left||right)错误"); return;
		}
		if(this.divPlane>0) this.objID.style.left = this.divPlane+"px";
		if(this.divPlane<0) this.objID.style.left = document.documentElement.clientWidth-this.objID.clientWidth+this.divPlane+"px";
	}
	if(this.scrollDelay<15 || isNaN(this.scrollDelay)) this.scrollDelay = 15;
	if(this.waitTime<500 || isNaN(this.waitTime)) this.waitTime = 500;
	if(arguments.length>=1) this.start();
}
float.prototype.start = function(){
	if(null == this.objID) return;
	var objCouplet = this;
	timer = this.scrollDelay;
	objCouplet.lastScrollY = 0;
	objCouplet.timerID = null;
	objCouplet.startID = function(){
		if("block" == objCouplet.objID.style.display){
			objCouplet.run();
		}else{
			clearInterval(objCouplet.timerID);
		}
	}
	objCouplet.Begin = function(){
		objCouplet.timerID = setInterval(objCouplet.startID,timer);
	}
	
	setTimeout(objCouplet.Begin,this.waitTime);
}
float.prototype.run = function(){
	if(document.documentElement && document.documentElement.scrollTop){
		uu_scrY = parseFloat(document.documentElement.scrollTop);
	}else if(document.body){
		uu_scrY = parseFloat(document.body.scrollTop);
	}
	uu_divX = parseFloat(this.objID.style.top.replace("px",""));
	uu_curTop = .1 * (uu_scrY - this.lastScrollY);
	uu_curTop = uu_curTop>0?Math.ceil(uu_curTop):Math.floor(uu_curTop);
	this.objID.style.top = parseFloat(uu_divX + uu_curTop) + "px";
	this.lastScrollY += uu_curTop; 
}
float.prototype.moveable=function(){//拖拽代码
	var iWidth = document.documentElement.clientWidth; 
	var iHeight = document.documentElement.scrollHeight;
	var moveX = 0;
	var moveY = 0;
	var moveTop = 0;
	var moveLeft = 0;
	var moveable = false;
	var docMouseMoveEvent = document.onmousemove;
	var docMouseUpEvent = document.onmouseup;
	var w=parseInt(this.objID.offsetWidth);
	var h=parseInt(this.objID.offsetHeight);
	var obj=this.objID;
	this.objID.onmousedown = function() {
		var evt = getEvent();
		moveable = true; 
		moveX = evt.clientX;
		moveY = evt.clientY;
		moveTop = parseInt(this.style.top);
		moveLeft = parseInt(this.style.left);
		document.onmousemove = function() {
			if (moveable) {
				var evt = getEvent();
				var x = moveLeft + evt.clientX - moveX;
				var y = moveTop + evt.clientY - moveY;
				if ( x > 0 &&( x + w < iWidth) && y > 0 && (y + h < iHeight) ) {
					obj.style.left = x + "px";
					obj.style.top = y + "px";
				}
			}	
		};
		document.onmouseup = function () { 
			if (moveable) { 
				document.onmousemove = docMouseMoveEvent;
				document.onmouseup = docMouseUpEvent;
				moveable = false; 
				moveX = 0;
				moveY = 0;
				moveTop = 0;
				moveLeft = 0;
			} 
		};
	}	
	// 获得Event对象，用于兼容IE和FireFox
    function getEvent() {
	    return window.event || arguments.callee.caller.arguments[0];
    }
}

//另一浮动代码
function autoFloatFun(id){ 
	diffY=document.body.scrollTop+200; 
	percent=.1*(diffY-lastScrollY); 
	if(percent>0)	percent=Math.ceil(percent); 
	else	percent=Math.floor(percent); 
	document.getElementById(id).style.pixelTop+=percent; 
	lastScrollY=lastScrollY+percent; 
}
function autoFloat(id){
	window.setInterval('autoFloatFun('+id+')',1); 
}
