﻿/* FloatAd是一个使特定id的元素随着浏览器滚动条动态浮动的类
	
	调用方法：
		1.当要浮动的元素根据top属性进行绝对定位时(必须在元素的style属性中设置position,top,height)
			HTML代码：
				<div id="x" style="position:absolute; background-color:#6633FF; height:100px; width:100px; top:0px;">xxx</div>
			JS代码：
				var FloatAd_X = new FloatAd('x');
				window.setInterval(function() {
					FloatAd_X.start();
				}, 1);
		2.当要浮动的元素根据bottom属性进行绝对定位时(注意第二个参数一定要为bottom,必须在元素的style属性中设置position,bottom,height)
			HTML代码：
				<div id="x2" style="position:absolute; background-color:#6633FF; height:100px; width:100px; bottom:50px; left:500px;"></div>
			JS代码：
			var FloatAd_X2 = new FloatAd('x2', 'bottom');		
			window.setInterval(function() {
				FloatAd_X2.start();
			}, 1);
*/
function FloatAd() {
	this.ID = document.getElementById(arguments[0]);
	if (!this.ID) {
		return ;
	}
	if (typeof(arguments[1]) == "string" && arguments[1] == "bottom") {
		this.ID.style.top = document.body.scrollTop + (document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body.clientHeight) - parseInt(this.ID.style.height) - parseInt(this.ID.style.bottom) + 'px';
	}
	this.adlastScrollY = 0;
}

FloatAd.prototype.start = function() {
	var diffY;
	if (document.documentElement && document.documentElement.scrollTop) {
		diffY = document.documentElement.scrollTop;
	} else if (document.body) {
		diffY = document.body.scrollTop;
	}
	percent = 0.1 * (diffY - this.adlastScrollY); 
	if(percent > 0) {
		percent = Math.ceil(percent); 
	} else {
		percent = Math.floor(percent); 
	}
	this.ID.style.top = parseInt(this.ID.style.top) + percent + "px";
	this.adlastScrollY = this.adlastScrollY + percent; 
}
