﻿
//谷木人图层动画库
//GmrGuestbook.js,
//版权所有谷木人科技(北京)有限公司,www.gumuren.com
//版本1.0
//创建时间2008.12.06
//修改时间2008.12.06
//修改人

/// <reference path="GmrJsLib.js" />
Gmr.Motion = new Object();
Gmr.Motion.ObjEnter = function(obj, ox, oy, direction, speed, mode)
{
    /// <summary>对象出现动画,</summary>
    /// <param name="ox">初始点x坐标</param>
    /// <param name="oy">初始点y坐标</param>
    /// <param name="direction">出现方向,分别为"xleft","xright","xboth","ytop","ybottom","yboth","both"</param>
    /// <param name="speed">速度</param>
    /// <param name="mode">动作方式</param>
    
    var obj = $(obj);
    var tWidth = parseInt(Gmr.Style.GetRuntimeStyle(obj, "width"));
    var tHeight = parseInt(Gmr.Style.GetRuntimeStyle(obj, "height"));
    switch (direction)
    {
        case ("xright"):
            obj.style.width = "0px";
            var f = function()
            {
                var nw = obj.obj.offsetWidth + 2;
                obj.style.width = nw + "px";
                if (nw < tWidth)
                {
                    setTimeout(f);
                }

            }
            f();
            break;
    }

}

Gmr.Motion.ResizeWidth = function(obj, startWidth, endWidth, speed, feature)
{
    /// <summary>改变对象的宽度</summary>
    /// <param name="startWidth">起始宽度</param>
    /// <param name="endWidth">目标宽度</param>
    /// <param name="speed">速度,单位毫秒</param>
    /// <param name="feature">特征值</param>
    /// <param name="obj">要改变的对象</param>

    var obj = $(obj);
    if (!obj)
    {
        return false;
    }
    var timespan = parseInt(Gmr.Pub.GetFeature(feature, "timespan", 20));
    var direction = Gmr.Pub.GetFeature(feature, "direction:right;", "right");
    var step = Math.ceil((endWidth - startWidth) / speed * timespan);
    var ov = 0;
    if (direction == "right")
    {
        //ov = obj.offsetLeft + obj.offsetWidth;
        obj.style.width = startWidth + "px";
        //obj.style.right = ov + "px";
    }
    else
    {
        obj.style.width = startWidth + "px";
    }
    var f = function()
    {
        var nw = obj.offsetWidth + step;
        if (step > 0)
        {
            if (nw > endWidth) { nw = endWidth; }
            obj.style.width = nw + "px";
            if (nw < endWidth)
            {
                setTimeout(f, timespan);
            }
        }
        else
        {
            if (nw < endWidth) { nw = endWidth; }
            obj.style.width = nw + "px";
            if (nw > endWidth)
            {
                setTimeout(f, timespan);
            }
        }
    }
    setTimeout(f, timespan);
}
Gmr.Motion.ResizeHeight = gmrMoveHeight = function (obj, startHeight, endHeight, speed, feature, funEnd)
{
    /// <summary>改变对象的高度</summary>
    /// <param name="startHeight">起始高度</param>
    /// <param name="endHeight">目标高度</param>
    /// <param name="speed">速度,单位毫秒</param>
    /// <param name="feature">特征值</param>
    /// <param name="obj">要改变的对象</param>

    var obj = $(obj);
    var timer = null;
    if (!obj)
    {
        return false;
    }
    var timespan = parseInt(Gmr.Pub.GetFeature(feature, "timespan", 20));
    var direction = Gmr.Pub.GetFeature(feature, "direction", "down");
    var step = Math.ceil((endHeight - startHeight) / speed * timespan);
    var ov = 0;

    if (direction == "down")
    {
        obj.style.height = startHeight + "px";
    }
    else
    {
        obj.style.height = startHeight + "px";
    }
    var f = function ()
    {
        var nh = obj.offsetHeight + step;
        if (step > 0)
        {
            if (nh > endHeight) { nh = endHeight; }
            obj.style.height = nh + "px";
            if (nh < endHeight)
            {
                timer = setTimeout(f, timespan);
            }
            else if (funEnd != null)
            {
                funEnd();
            }
        }
        else
        {
            if (nh < endHeight) { nh = endHeight; }
            obj.style.height = nh + "px";
            if (nh > endHeight)
            {
                timer = setTimeout(f, timespan);
            }
            else if (funEnd != null)
            {
                funEnd();
            }
        }
    }
    timer = setTimeout(f, timespan);
    return {
        stop: function (isExecFunEnd)
        {
            /// <summary>停止动画</summary>
            /// <param name="isExecFunEnd">停止后是否立即执行结束回调函数，默认不执行</param>
            clearTimeout(timer);
        }
    }
}
Gmr.Motion.ResizeSize = function(objs,newWidths,newHeights,speed,feature)
{
    /// <summary>改变对象的尺寸</summary>
    /// <param name="objs">要改变的对象数组</param>
    /// <param name="speed">速度值,单位毫秒</param>    
    /// <param name="newWidths">新的宽度值数组</param>
    /// <param name="newHeights">新的高度值数组</param>
    /// <param name="feature">特征值</param>


}
Gmr.Motion.Move = gmrMove = function (objs, posStarts, posEnds, params)
{
    var timespan = (params && params.timespan) ? params.timespan : 20;
    var totalTime = (params && params.totaltime) ? params.totaltime : 1000;
    var isStop = (params && params.stop) ? params.stop : false; //是否强制停止之前的动画
    if (objs.constructor != window.Array)
    {
        var o = objs;
        objs = new Array();
        objs.push(o);
        posStarts = new Array(posStarts);
        posEnds = new Array(posEnds);
    }
    for (var i = 0; i < objs.length; i++)
    {
        var obj = objs[i] = $(objs[i]);
        if (obj.gmrMoveGoing)
        {
            if (isStop)
            {
                obj.gmrMoveObjs.Remove(obj);
            }
            else
            {
                objs.Remove(obj); continue;
            }
        }
        var posStart = posStarts[i], posEnd = posEnds[i];
        var p = new Object(); //起点坐标
        if (posStart && posStart.x) { p.x = posStart.x } else { p.x = obj.offsetLeft; };
        if (posStart && posStart.y) { p.y = posStart.y } else { p.y = obj.offsetTop; };
        if (posStart && posStart.w) { p.w = posStart.w } else { p.w = obj.offsetWidth };
        if (posStart && posStart.h) { p.h = posStart.h } else { p.h = obj.offsetHeight };
        var p2 = posEnd;
        var steps = calPath(p, p2, totalTime, timespan);
        obj.gmrMoveSteps = steps;
        obj.gmrMoveIndex = 0;
        obj.gmrMoveGoing = true;
        obj.gmrMoveObjs = objs;
        m();
    }
    function m()
    {
        //步进运动
        var isEnd = false;
        // if (objs == null) return;
        for (var i = 0; i < objs.length; i++)
        {
            var obj = objs[i];
            var ps = obj.gmrMoveSteps, pi = obj.gmrMoveIndex;
            var p = ps[pi];
            obj.style.left = p.x + "px", obj.style.top = p.y + "px";
            pi++;
            obj.gmrMoveIndex = pi;
            if (pi >= ps.length) { isEnd = true; }
        }
        if (!isEnd)
        {
            setTimeout(m, timespan);
        }
        else
        {
            for (var i = 0; i < objs.length; i++)
            {
                var obj = objs[i];
                obj.gmrMoveSteps = null;
                obj.gmrMoveIndex = null;
                obj.gmrMoveGoing = null;
                obj.gmrMoveObjs = null;
            }
        }
    }
    function calPath(ptStart, ptEnd, totalTime, timespan)
    {
        //缓动函数，根据输入值计算路径数组
        var ps = new Array(); //坐标数组
        var sx = ptEnd.x - ptStart.x, sy = ptEnd.y - ptStart.y; //x,y方向总路程
        var count = parseInt(totalTime / timespan); //总运动次数
        var vx = sx / count, vy = sy / count; //速度
        for (var i = 0; i < count; i++)
        {
            ps.push({ x: ptStart.x + vx * i, y: ptStart.y + vy * i });
        }
        ps.push({ x: ptEnd.x, y: ptEnd.y });
        return ps;
    }
}
Gmr.Motion.MoveH =gmrMoveH= function(objs, newLeft, newRight, speed, feature, funEnd)
{
    /// <summary>水平移动对象,要求对象的position值为absolute或realtive</summary>
    /// <param name="newLeft">新的左坐标值</param>
    /// <param name="newRight">新的右坐标值,当newLeft参数为null时此参数有效,暂时不支持此参数</param>
    /// <param name="speed">速度值,单位毫秒</param>    
    /// <param name="feature">特征值</param>
    /// <param name="objs">对象数组</param>
    var timespan = parseInt(Gmr.Pub.GetFeature(feature, "timespan", 20));
    var steps = new Array();
    for (var i = 0; i < objs.length; i++)
    {
        var obj = $(objs[i]);
        objs[i] = obj;
        var cLeft = obj.offsetLeft;
        var cRight = obj.parentNode.offsetWidth - (obj.offsetLeft + obj.offsetWidth);
        if (newLeft != null)
        {
            steps[i] = Math.ceil((newLeft - cLeft) / speed * timespan);
        }
        else if (newRight != null)
        {
            steps[i] = Math.ceil((newRight - cRight) / speed * timespan);
            return;
        }
        else
        {
            return;
        }
    }
    var sf = function()
    {
        for (var j = 0; j < objs.length; j++)
        {
            var step = steps[j];
            var o = objs[j];
            if (step > 0 && o.offsetLeft + step > newLeft)
            {
                o.style.left = newLeft + "px";
                if (funEnd) { funEnd(); }
            }
            else if (step < 0 && o.offsetLeft + step < newLeft)
            {
                o.style.left = newLeft + "px";
                if (funEnd) { funEnd(); }
            }
            else
            {
                o.style.left = o.offsetLeft + step + "px";
                setTimeout(sf, timespan);
            }
        }
    }
    setTimeout(sf, timespan);

}
function ResizeObjSize(tagIDArray, newWidthArray, newHeightArray, timeSpan, stepCount)
{
    var obj;
    var sWidthArray = new Array();
    var sHeightArray = new Array();
    var i;

    for (i = 0; i < tagIDArray.length; i++)
    {
        obj = $(tagIDArray[i]);
        sWidthArray[i] = GetInt((newWidthArray[i] - obj.offsetWidth) / stepCount);
        sHeightArray[i] = GetInt((newHeightArray[i] - obj.offsetHeight) / stepCount);
    }
    var timeName = "ResizeMainNav";
    var evCmd = "var tObj,isEnd=false,tWidth,tHeight;";
    var j = 0;
    for (j = 0; j < tagIDArray.length; j++)
    {
        evCmd +=
             "   tObj=document.getElementById(\'" + tagIDArray[j] + "\');" +
             "   tWidth=tObj.offsetWidth + " + sWidthArray[j] + ";" +
             "   tHeight=tObj.offsetHeight + " + sHeightArray[j] + ";" +
             "   if((tWidth-" + newWidthArray[j] + ")* " + sWidthArray[j] + ">=0 || (tHeight - " + newHeightArray[j] + ")* " + sHeightArray[j] + ">=0)" +
             "   {" +
             "      tWidth=" + newWidthArray[j] + ";" +
             "      tHeight=" + newHeightArray[j] + ";";
        if (j == 0) evCmd +=
             "      isEnd=true;";
        evCmd +=
             "   }" +
             "   else" +
             "   {isEnd=false;}" +
             "   tObj.style.width=tWidth;" +
             "   tObj.style.height=tHeight;";
    };
    Resize2(evCmd, timeSpan);

}
function Resize2(evCmd, timeSpan)
{
    var isEnd = false;
    eval(evCmd);
    if (isEnd == false)
    {
        setTimeout("Resize2(\"" + evCmd + "\"," + timeSpan + ")");
    }
}


