/**
 * 左右浮动广告的处理.
 * @version 1.00, 08/11/18
 * @author Mike
 */
 
 //全局变量，用以记录最后的Y轴数值
var lastScrollY=0;

/**
* 改变指定元素保持在浏览器可视范围内
* @param adLeft 左边广告的容器id值
* @param adRight 右边广告的容器id值
*/
function heartBeat(adLeft,adRight){
    var diffY;
    if (document.documentElement && document.documentElement.scrollTop) {
        diffY = document.documentElement.scrollTop;
    } else if (document.body) {
        diffY = document.body.scrollTop
    } else {
        /*Netscape stuff*/
    }

    percent=.1*(diffY-lastScrollY); 
    if(percent>0) {
        percent=Math.ceil(percent); 
    } else {
        percent=Math.floor(percent); 
    }
    document.getElementById(adLeft).style.top=parseInt(document.getElementById(adLeft).style.top)+percent+"px";
    document.getElementById(adRight).style.top=parseInt(document.getElementById(adRight).style.top)+percent+"px";

    lastScrollY=lastScrollY+percent;
}

/**
* 调用heartBeat来调整指定元素的位置，并指定下次调用的间隔
* @param adLeft 左边广告容器的id值
* @param adRight 右边广告容器的id值
*/
function engineStart(adLeft,adRight) {
    
    window.setInterval("heartBeat('" + adLeft + "','" + adRight + "')",1);
}
 
 /**
 * 两个广告显示容器显示页面随机互换
 * @param frameOne 第一个广告容器id值
 * @param frameTwo 第二个广告容器id值
 */
function fnRandomAds(frameOne, frameTwo) {

    var sIframeOne = document.getElementById(frameOne);
    var sIframeTwo = document.getElementById(frameTwo);
    
    var aAds = new Array(sIframeOne.src, sIframeTwo.src);

    var iRandomNum1 = Math.floor(Math.random() * aAds.length);
    var iRandomNum2 = aAds.length - 1 - iRandomNum1;

    sIframeOne.src = aAds[iRandomNum1];
    sIframeTwo.src = aAds[iRandomNum2];  

}

// for Wander Obj in IE
var floatImageDiv;
/**
 * Wander对象类
 * 在页面的一定范围内元素浮动，带关闭浮动功能
 */
function Wander(divId, divWidth, divHeight, xSpeed, ySpeed, divZIndex, appear, rXmax, rXmin , rYmax, rYmin) {
    this.id = divId;
    this.width = divWidth;
    this.height = divHeight;
    //可选参数
    var winSize = getBrowserWindowSize();
    var scrollPos;
    this.xSpeed = xSpeed ? xSpeed : 4;
    this.ySpeed = ySpeed ? ySpeed : -3;
    this.index = divZIndex ? divZIndex : 10;
    this.isAppear = appear ? appera : true;
    this.xMin = (rXmin ? rXmin : 0.16) * winSize.width;
    this.yMin = (rYmin ? rYmin : 0.25) * winSize.height;
    this.xMax = (rXmax ? rXmax : 0.54) *winSize.width;
    this.yMax = (rYmax ? rYmax : 0.91) * winSize.height;
    this.x = rand(this.xMax - this.width);
    this.y = rand(this.yMax - this.height);
    
    var floatObj = this;
    this.timeStamp = null;
    
    /**
     * 产生一个[0, maxNum]之间的随机整数
     */
    function rand(maxNum){
        return Math.round(maxNum*Math.random());
    }
    
    
    /**
    * 横向反弹
    */
    this.bounceX = function() { 
        this.xSpeed = -1 * this.xSpeed;      
    };
    
    /**
    * 纵向反弹
    */
    this.bounceY = function() {
        this.ySpeed = -1 * this.ySpeed;
    };
    
    /**
    * 坐标变换
    */
    this.move = function() {
        this.x = this.x + this.xSpeed;
        this.y = this.y + this.ySpeed;
        
        if ((this.x + this.width) > this.xMax){
            this.x = this.xMax - this.width;
            this.bounceX();
        } else if (this.x < this.xMin) {
            this.x = this.xMin;
            this.bounceX();
        } else if ((this.y + this.height) > this.yMax){
            this.y = this.yMax - this.height;
            this.bounceY();             
        } else if (this.y < this.yMin) {
            this.y = this.yMin;
            this.bounceY();             
        }
        
        this.render();
    };
    
    /**
    * 位置渲染
    */
    this.render = function() {
        floatImageDiv = $(this.id);
        floatImageDiv.style.left = this.x + "px";
        floatImageDiv.style.top = this.y + "px";           
    };
    
    /**
     * Wander对象初始启动
     */
    this.init = function() {
        floatImageDiv = $(this.id);
        floatImageDiv.style.position = "absolute";       
        floatImageDiv.style.left = this.x + "px";
        floatImageDiv.style.top = this.y + "px";
        floatImageDiv.style.height = this.height + "px";
        floatImageDiv.style.width= this.width + "px";
        floatImageDiv.style.zIndex = this.index;
        
        addEvent(window, 'scroll', function() {
            scrollPos = getDocumentScrollPosition();
            floatObj.yMin = scrollPos.top + (rYmin ? rYmin : 0.25) *winSize.height;
            floatObj.yMax = scrollPos.top + (rYmax ? rYmax : 0.91) * winSize.height;
        });
        
        addEvent(window, 'resize', function() {
            winSize = getBrowserWindowSize();
            floatObj.xMin = (rXmin ? rXmin : 0.16) * winSize.width;
            floatObj.yMin = (rYmin ? rYmin : 0.25) *winSize.height;
            floatObj.xMax = (rXmax ? rXmax : 0.54) *winSize.width;
            floatObj.yMax = (rYmax ? rYmax : 0.91) * winSize.height;
        });
        
        addEvent(floatImageDiv, 'mouseover', function() {
            floatObj.timeStamp = clearInterval(floatObj.timeStamp);
        });
        
        addEvent(floatImageDiv, 'mouseout', function() {
           floatObj.timeStamp = setInterval(function() {
                floatObj.move();
        }, 100);
        });
        
        /**
         * 若对象init方法有参数id，设置为对象关闭按键
         */
        if(arguments.length > 0) {
            addEvent($(arguments[0]), 'click', function() {
                $(floatObj.id).style.display = "none";
                floatObj.timeStamp = clearInterval(floatObj.timeStamp);
            });
        }
        
        this.timeStamp = setInterval(function() {
            floatObj.move();
        }, 100);
    }
    
}
