﻿/* 定义移动区块类 */
function MoveBlock(moveObj, delay, step) {
    this.moveObj = moveObj; // 移动区块对象
    this.delay = delay; // 移动的延期时间
    this.step = step; // 移动的幅度
    this.moveX = 0; // 移动区块的x坐标
    this.moveY = 0; // 移动区块的y坐标
    this.directionX = true; // 沿x轴正方向移动
    this.directionY = true; // 沿y轴正方向移动
}
// 获取移动区块的宽度
MoveBlock.bWidth = function(moveObj) {
    return moveObj.offsetWidth;
}
// 获取移动区块的高度
MoveBlock.bHeight = function(moveObj) {
    return moveObj.offsetHeight;
}
// 获取页面可见区域的宽度
MoveBlock.cWidth = function() {
    return document.documentElement.clientWidth;
}
// 获取页面可见区域的高度
MoveBlock.cHeight = function() {
    return document.documentElement.clientHeight;
}
// 区块移动的方法
MoveBlock.prototype.move = function() {
    try
    {
        //alert(document.documentElement.scrollTop);
        var bLeft = this.moveX + document.documentElement.scrollLeft;
        var bTop = this.moveY + document.documentElement.scrollTop;
        this.moveObj.style.left = bLeft + "px";
        this.moveObj.style.top = bTop + "px";
        
        if(this.directionX)
        {
            this.moveX = this.moveX + this.step;
            
            if(this.moveX >= (MoveBlock.cWidth() - MoveBlock.bWidth(this.moveObj)))
            {
                this.directionX = false;
            }
        }
        else
        {
            this.moveX = this.moveX - this.step;
            
            if(this.moveX <= 0)
            {
                this.directionX = true;
            }
        }
        
        if(this.directionY)
        {
            this.moveY = this.moveY + this.step;
        
            if(this.moveY >= (MoveBlock.cHeight() - MoveBlock.bHeight(this.moveObj)))
            {
                this.directionY = false;
            }
        }
        else
        {
            this.moveY = this.moveY - this.step;
        
            if(this.moveY <= 0)
            {
                this.directionY = true;
            }
        }
    }
    catch(e)
    {}
}
