﻿var BOLI = BOLI || {};

BOLI.Core = BOLI.Core || {};

BOLI.Core.ObjectMover = BOLI.Core.ObjectMover || function (attributes) {
    var This = this;
    this.startX;
    this.endX;
    this.onStart = new Function;
    this.onMove = new Function;
    this.onStop = new Function;
    this.timer;
    this.duration = 3000;
    this.reverse = false;

    this.curve = 'linea';

    this.move = function () {
        this.onStart();
        this.newX = this.startX;
        this.reverse = this.startX > this.endX ? true : false;
        this.timer = setInterval(function () { This.calculatePosition() }, 25);
        this.divider = this.duration / 25;
    }
    this.calculatePosition = function () {
        if (this.curve == 'linear') {
            this.newX = this.newX + ((this.endX - this.startX) / this.divider);
            this.onMove({ X: this.newX })

            if (this.reverse) {
                if (this.newX < this.endX) this.stop();
            } else {
                if (this.endX < this.newX) this.stop();
            }
        }
        else {
            var offsetX = (this.endX - this.newX) / (this.divider / 10);
            this.newX = this.newX + offsetX;
            this.onMove({ X: this.newX })
            if (Math.abs(offsetX) < (1 / (this.divider / 10))) this.stop();
        }
    }
    this.stop = function () {
        clearInterval(this.timer);
        this.onMove({ X: this.endX });
        this.onStop();
    }
    this.reset = function () {
        clearInterval(this.timer);
    }
}
