﻿var _AO = function(FPS, FramesN, Object, Property, BeginValue, EndValue, ValueSuffix) {
    this.FPS = FPS;
    this.FramesN = FramesN;
    this.AnimationSpeed = (1000 / FPS);
    this.ValueIncrement = (BeginValue - EndValue) / FramesN;
    this.Object = Object;
    this.Property = Property;
    this.BeginValue = BeginValue;
    this.EndValue = EndValue;
    if (ValueSuffix) {
        this.ValueSuffix = ValueSuffix;
    }
    else { this.ValueSuffix = ""; }
    this.CurrentFrame = 0;

    var _this = this;
    var IntFunc = function() {
        _this.CurrentFrame = _this.CurrentFrame + 1;
        var NextValue = _this.BeginValue - (_this.ValueIncrement * _this.CurrentFrame);
        NextValue = NextValue.toString();
        NextValue = NextValue + _this.ValueSuffix;
        if (_this.Property == "opacity") {
            if (navigator.userAgent.indexOf("MSIE") > -1) {
                _this.Object.style.filter = "alpha(opacity=" + (NextValue * 100) + ")";
            }
            else { _this.Object.style[_this.Property] = NextValue; }
        }
        else {
            _this.Object.style[_this.Property] = NextValue;
        }
        if (_this.CurrentFrame == _this.FramesN) { _this.Dispose(); };
    };
    this._IntervalFunction = IntFunc;
    Animation.Add(this);
};
_AO.prototype.constructor = _AO;
_AO.prototype.FPS = 0;
_AO.prototype.AnimationSpeed = 0;
_AO.prototype.ValueIncrement = 0;
_AO.prototype.FramesN = 0;
_AO.prototype.Object = null;
_AO.prototype.Property = null;
_AO.prototype.BeginValue = 0;
_AO.prototype.EndValue = 0;
_AO.prototype.ValueSuffix = null;
_AO.prototype._Interval = null;
_AO.prototype.CurrentFrame = 0;
_AO.prototype._IntervalFunction = null;
_AO.prototype.Start = function() {
    var FN = this._IntervalFunction;
    var AS = this.AnimationSpeed;
    this._Interval = setInterval(FN, AS);
};
_AO.prototype.Dispose = function() {
    clearInterval(this._Interval);
    this.CurrentFrame = this.FramesN;
    this.AnimationSpeed = null;
    this.FramesN = null;
    this.Object = null;
    this.Property = null;
    this.BeginValue = null;
    this.EndValue = null;
    this.FInterval = null;
};

var AnimationObject = function(FPS, FramesN, Object, Property, BeginValue, EndValue, ValueSuffix) { return new _AO(FPS, FramesN, Object, Property, BeginValue, EndValue, ValueSuffix); }

var Animation = {
    Queue: [],
    Find: function(FPS, FramesN, Object, Property, BeginValue, EndValue, OptionalValueEnd) {

    },
    Add: function(AO) {
        for (var i = 0; i < this.Queue.length; i++) {
            if (this.Queue[i].Object == AO.Object) {
                this.Queue[i].Dispose();
                i = this.Queue.length;
            }
        };
        this.Queue[this.Queue.length] = AO;
    }
}