﻿function FloatAD(){}

//显示左侧广告
//HTML：广告的HTML代码
//top：距顶端的距离
FloatAD.prototype.showLeftAD=function(HTML,top){
this.showAD(HTML,top,FloatAD.left);
}
//显示右侧广告
//HTML：广告的HTML代码
//top：距顶端的距离
FloatAD.prototype.showRightAD=function(HTML,top){
this.showAD(HTML,top,FloatAD.right);
}

FloatAD.prototype.showAD=function(HTML,top,position){
oThis=this;
var newAD=document.createElement("div");
FloatAD.addEvent(window,"load",function(){document.body.appendChild(newAD);});
newAD.style.float="left";
newAD.style.position="absolute";
newAD.innerHTML=HTML;

if(position==FloatAD.left) this.play(newAD,top);
if(position==FloatAD.right) this.playRight(newAD,top);
}

FloatAD.prototype.play=function(obj,top){
var oThis=this;
obj.style.left="1px";
if(obj.offsetTop!=(FloatAD.getScrollTop()+top)) {
   var dy=(FloatAD.getScrollTop()+top-obj.offsetTop)*FloatAD.delta;
   dy=(dy>0?1:-1)*Math.ceil(Math.abs(dy));
   obj.style.top=(obj.offsetTop+dy)+"px";
}
if(screen.width<=800) obj.style.display="none";
else obj.style.display="";
setTimeout(function(){oThis.play(obj,top),10})
}

FloatAD.prototype.playRight=function(obj,top){
var oThis=this;
var clientWidth=FloatAD.getClientWidth();
var objWidth=obj.offsetWidth;
obj.style.left=(clientWidth-objWidth-1)+"px";
if(obj.offsetTop!=(FloatAD.getScrollTop()+top)) {
   var dy=(FloatAD.getScrollTop()+top-obj.offsetTop)*FloatAD.delta;
   dy=(dy>0?1:-1)*Math.ceil(Math.abs(dy));
   obj.style.top=(obj.offsetTop+dy)+"px";
}
if(screen.width<=800) obj.style.display="none";
else obj.style.display="";
setTimeout(function(){oThis.playRight(obj,top),10})
}

//方向枚举
FloatAD.left=1;
FloatAD.right=2;

FloatAD.delta=0.15//速率

////////////////////////////////////////////////////////////////////////
//返回窗口可见区域宽度
FloatAD.getClientWidth=function(){
var clientWidth;
if(document.documentElement.clientWidth) clientWidth=document.documentElement.clientWidth;
else clientWidth=document.body.clientWidth;
return clientWidth;
}
//返回滚动条卷入的高度
FloatAD.getScrollTop=function(){
var scrollTop;
if(document.documentElement.scrollTop) scrollTop=document.documentElement.scrollTop;
else scrollTop=document.body.scrollTop;
return scrollTop;
}
//返回滚动条卷入的宽度
FloatAD.getScrollLeft=function(){
var scrollLeft;
if(document.documentElement.scollLeft) scrollLeft=document.documentElement.scrollLeft;
else scrollLeft=document.body.scrollLeft;
return scrollLeft;
}
//添加事件
FloatAD.addEvent=function(element,event,func){
if(element.attachEvent) element.attachEvent("on"+event,func);
else if(element.addEventListener) element.addEventListener(event,func,true);
else element["on"+event]=func;
}


