/*

产生漂浮广告
contentMain: 广告的内容
width: 宽度
height: 高度
contentClose: 关闭按钮的内容
allowClose: 允许关闭广告
*/
function RandomFloat (contentMain, width, height, contentClose, allowClose){
    if(typeof(contentMain) != 'string' || contentMain == '')
    {
        return;
    }
    
    var me=this;
    var disabled=false;//本对象是否失效的
    var divClose=null;//显示关闭的Div
    var divContainer=null;//广告容器Div
    this.left=0;//左位置(浮点小数)
    this.top=0;//上位置(浮点小数)
    //主体DIV
    divContainer=document.getElementById(contentMain);
    if(!divContainer){return;}
    divContainer.style.width=width+"px";
    divContainer.style.height=height+"px";
    divContainer.style.overflow="hidden";
    divContainer.style.zIndex=999;
    divContainer.style.position="absolute";
    divContainer.style.left="0px";
    divContainer.style.top="0px";
    
    //关闭IDV
    if(allowClose==true){
        if (contentClose==null || contentClose==""){
            contentClose="<div style='width:50px; font-size:12px; text-align:center; line-height:20px;cursor:pointer;'>关闭</div>";
        }
        divClose=document.createElement("div");
        divClose.innerHTML=contentClose;
        divClose.onclick=close;
        divClose.style.visibility="hidden";
        divClose.style.position="absolute";
        divClose.style.left="0px";
        divClose.style.top="0px";
        document.body.appendChild(divClose);
        divClose.style.left=divContainer.clientWidth-divClose.clientWidth-5+"px";
        divContainer.appendChild(divClose);
        divClose.style.visibility="visible";
    }
    //关闭广告
    function close(){
        disabled=true;
        document.body.removeChild(divContainer);
    }
    //取得视口大小
    function getViewSize(){
        var size=new Object();
        size.width=document.documentElement.clientWidth;
        size.height=document.documentElement.clientHeight;
        return size;
    }
    //漂浮
    var time;//本次移动持续时间(秒)
    var velocity;//速度 (像素/每秒 )
    var angle;//方向 (弧度)
    var angleOld=null;
    var frameCount=0;//帧计数器
    var x;//位移
    var y;//位移
    var isOver=false;//是否到达了边界
    var viewSize=null;//视口大小
    repeatTimerID=window.setInterval(frameTrigger, 40);
    function frameTrigger(){
        if(this.disabled){
            window.clearInterval(repeatTimerID);
            return;
        }
        viewSize=getViewSize();
        if(frameCount<=0){
            time=Math.round(Math.random()*5)+3;
            frameCount=time*25;
            velocity=Math.round(Math.random()*20)+40;
            var l=velocity/25;
            while(true){
                angle=Math.random()*2*Math.PI;
                x=Math.cos(angle)*l;
                y=Math.sin(angle)*l;
                if( (me.left<0 && x<0) || (me.left+width>viewSize.width && x>0) || (me.top<0 && y<0) || (me.top+height>viewSize.height && y>0) ){
                    continue;
                }else if (isOver==false && angleOld!=null){
                    var a=Math.abs(angle-angleOld);
                    if(a>Math.PI)
                        a=Math.abs(a-Math.PI*2)
                    if(a>Math.PI/2)
                        continue;
                }
                angleOld=angle;
                break;
            }
        }
        frameCount--;
        me.left+=x;
        me.top+=y;
        divContainer.style.left=Math.round(me.left+document.documentElement.scrollLeft)+"px";
        divContainer.style.top=Math.round(me.top+document.documentElement.scrollTop)+"px";
        isOver=me.left<0 || me.left+width>viewSize.width || me.top<0 || me.top+height>viewSize.height;
        if(isOver){
            frameCount=0;
        }
    }
}
