﻿/// <reference path="../K2046.js" />
/// pointF,pointT,elementF,elementT,color,width,step,delay,
K2046.MovingBox = function(option) {
    option.elementF = K2046.$(option.elementF);
    option.elementT = K2046.$(option.elementT);
    if ((option.pointF || option.elementF) && (option.pointT || option.elementT)) {
        this.PointF = option.pointF ? option.pointF : X.GetPosition(option.elementF);
        this.PointT = option.pointT ? option.pointT : X.GetPosition(option.elementT);
        this.SizeF = option.sizeF ? option.sizeF : [option.elementF.offsetWidth, option.elementF.offsetHeight];
        this.SizeT = option.sizeT ? option.sizeT : [option.elementT.offsetWidth, option.elementT.offsetHeight];
        this.Step = option.step || 20;
        this.Delay = option.delay || 50;
        this.Color = option.color || "#0000ff";
        this.Width = option.width || "1px";
        this.Index = 0;
        this.StepWidth = (this.SizeF[0] - this.SizeT[0]) / this.Step;
        this.StepHeight = (this.SizeF[1] - this.SizeT[1]) / this.Step;
        this.StepX = (this.PointF.x - this.PointT.x) / this.Step;
        this.StepY = (this.PointF.y - this.PointT.y) / this.Step;
        this.Style = option.style;
    }
};

K2046.MovingBox.Moving = function(option) {
    new K2046.MovingBox(option).Move();
}

K2046.MovingBox.prototype = {
    Move: function() {
        var el = this.Element = document.createElement("div");
        document.body.appendChild(el);
        el.style.position = "absolute";
        el.style.width = this.SizeF[0] + "px";
        el.style.height = this.SizeF[1] + "px";
        el.style.left = this.PointF.x + "px";
        el.style.top = this.PointF.y + "px";
        el.style.border = "solid " + this.Width + " " + this.Color;
        if (this.Style) {
            for (var c in this.Style) {
                el.style[c] = this.Style[c];
            }
        }
        this.Next();
    }

    , Next: function() {
        this.Timer = setTimeout(function(me) { return function() { me.NextStep(); } } (this), this.Delay);
    }

    , NextStep: function() {
        clearTimeout(this.Timer);
        if (this.Step <= 0) {
            X.Dom.Remove(this.Element);
            return;
        }
        this.Step--;
        this.Element.style.width = (this.SizeT[0] + this.Step * this.StepWidth) + "px";
        this.Element.style.height = (this.SizeT[1] + this.Step * this.StepHeight) + "px";
        this.Element.style.left = (this.PointT.x + this.Step * this.StepX) + "px";
        this.Element.style.top = (this.PointT.y + this.Step * this.StepY) + "px";
        this.Next();
    }
};