﻿
function zxcBAnimator(mde, obj, srt, fin, ms, scale, curve) {
    if (typeof (obj) == 'string') { obj = document.getElementById(obj); }
    if (!obj || (!srt && !fin) || (srt == fin)) return;
    var oop = obj[mde.replace(/\W/g, '') + 'oop'];
    if (oop) {
        clearTimeout(oop.to);
        if (oop.srtfin[0] == srt && oop.srtfin[1] == fin && mde.match('#')) oop.update([oop.data[0], (oop.srtfin[0] == oop.data[2]) ? fin : srt], ms, scale, curve);
        else oop.update([srt, fin], ms, scale, curve);
    }
    else obj[mde.replace(/\W/g, '') + 'oop'] = new zxcBAnimatorOOP(mde, obj, srt, fin, ms, scale, curve);
}

function zxcBAnimatorOOP(mde, obj, srt, fin, ms, scale, curve) {
    this.srtfin = [srt, fin];
    this.to = null;
    this.obj = obj;
    this.mde = mde.replace(/\W/g, '');
    this.update([srt, fin], ms, scale, curve);
}

zxcBAnimatorOOP.prototype.update = function(srtfin, ms, scale, curve) {
    this.time = ms || this.time || 2000;
    this.data = [srtfin[0], srtfin[0], srtfin[1]];
    this.ms = this.time * (!scale ? 1 : Math.abs((srtfin[1] - srtfin[0]) / (scale[1] - scale[0])));
    this.curve = (typeof (curve) == 'string') ? curve.charAt(0).toLowerCase() : (this.curve) ? this.curve : 'x';
    this.inc = Math.PI / (2 * this.ms);
    this.srttime = new Date().getTime();
    this.cng();
}

zxcBAnimatorOOP.prototype.cng = function() {
    var ms = new Date().getTime() - this.srttime;
    this.data[0] = (this.curve == 's') ? Math.floor((this.data[2] - this.data[1]) * Math.sin(this.inc * ms) + this.data[1]) : (this.curve == 'c') ? (this.data[2]) - Math.floor((this.data[2] - this.data[1]) * Math.cos(this.inc * ms)) : (this.data[2] - this.data[1]) / this.ms * ms + this.data[1];
    if (this.mde != 'left' && this.mde != 'top' && this.data[0] < 0) this.data[0] = 0;
    if (this.mde != 'opacity') this.obj.style[this.mde] = this.data[0] + 'px';
    else zxcOpacity(this.obj, this.data[0]);
    if (ms < this.ms) this.to = setTimeout(function(oop) { return function() { oop.cng(); } } (this), 10);
    else {
        this.data[0] = this.data[2];
        if (this.mde != 'opacity') this.obj.style[this.mde] = this.data[0] + 'px';
        else zxcOpacity(this.obj, this.data[0]);
    }
}

function zxcOpacity(obj, opc) {
    if (opc < 0 || opc > 100) return;
    obj.style.filter = 'alpha(opacity=' + opc + ')';
    obj.style.opacity = obj.style.MozOpacity = obj.style.KhtmlOpacity = opc / 100 - .001;
}