﻿/**
*  移动的轨迹可以根据自己的需要 修改 GetNextY GetNextX 方法
*  signId    要移动的对象
*/
function GggMoveSign(signId) {
    eval("this.mSignid=" + signId);      //这样就充许传递字符窜为对象
    this.mStepMove = 1;            //每次移动几个像素
    this.mDelay = 2000;          //每隔多少时间移动一次 

    this.mMoveable = true;         //是否能移到的标记
    this.mTimer;                      //时钟

    //可以浮动的边界
    this.mMaxX;
    this.mMaxY;
    this.mMinX;
    this.mMinY;

    //当前对象的座标位置
    this.mCurrX = 0;
    this.mCurrY = 0;

    //移动的方向
    this.mMoveDirectionX = 1;    //1 表示从左到右 增加 0 减少
    this.mMoveDirectionY = 1;    //1 从上到向 

    //设制能否移动状态
    this.SetMoveable = function(isMoveable) {
        this.mMoveable = isMoveable;
        if (isMoveable)
            this.Run()
        else
            clearTimeout(this.mTimer)
    }

    //得到当前要移动对像的宽
    this.GetSignWidth = function() {
        return (this.mSignid).offsetWidth;
    }

    //当前要移动对像的高
    this.GetSignHeight = function() {
        return (this.mSignid).offsetHeight;
    }

    /**
    *  移到指定位置 x y
    */
    this.MoveTo = function(x, y) {
        //保存当前座标
        this.mCurrX = x;
        this.mCurrY = y;
        (this.mSignid).style.left = this.mCurrX + document.documentElement.scrollLeft;
        (this.mSignid).style.top = this.mCurrY + document.documentElement.scrollTop;
    }


    /**
    *  得到对像移动到下一个 X 座标 的位置
    */
    this.GetNextX = function() {
        //当前对像所在位置
        var curr_pos = this.mCurrX;
        //移动后如果超出最大宽度 弹回 返回

        if (this.mMoveDirectionX == 1) {
            curr_pos += this.mStepMove;
        }
        else
            curr_pos -= this.mStepMove;

        //移动后如果超出最大宽度 弹回 返回
        if (curr_pos > this.mMaxX) {    //碰到右边界 弹回
            this.mMoveDirectionX = 0;
        }
        else if (curr_pos < this.mMinX)            //碰到左边界 弹回
            this.mMoveDirectionX = 1;

        return curr_pos;
    }


    /**
    *  得到对像移动到下一个 y 座标 的位置
    */
    this.GetNextY = function() {
        //当前对像所在位置
        var curr_pos = this.mCurrY;

        if (this.mMoveDirectionY == 1) {
            curr_pos += this.mStepMove;
        }
        else
            curr_pos -= this.mStepMove;

        //移动后如果超出最大宽度 弹回 返回
        if (curr_pos > this.mMaxY) {    //碰到下边界 弹回
            this.mMoveDirectionY = 0;
        }
        else if (curr_pos < this.mMinY)            //碰到上边界 弹回
            this.mMoveDirectionY = 1;

        return curr_pos;
    }

    /**
    *  自动移动
    */
    this.Move = function() {
        if (this.mMoveable)
            this.MoveTo(this.GetNextX(), this.GetNextY());
    }

    /**
    *  timeDelay    每隔多少时间移动一次
    *  stepMove    每次移动几个像素
    */
    this.Run = function(timeDelay, stepMove) {
        var obj_name = this;

        if (timeDelay)
            this.mDelay = timeDelay;

        if (stepMove)
            this.mStepMove = stepMove;


        with (this.mSignid) {
            style.position = "absolute";    //设定为绝对定位
            visibility = "visible";
            onmouseover = function() { obj_name.SetMoveable(false) }; //鼠标经过，停止滚动
            onmouseout = function() { obj_name.SetMoveable(true) }; //鼠标离开，开始滚动
        }

        //设定边界 
        this.mMaxX = document.documentElement.clientWidth - this.GetSignWidth() - 10;
        this.mMaxY = document.documentElement.clientHeight - this.GetSignHeight() - 10;
        this.mMinX = 10;
        this.mMinY = 10;


        this.mTimer = setInterval(function() { obj_name.Move(); }, this.mDelay);        //开启定时器
    }
}
