/**
 * An animation controller. The current implementation depends on a 0.033 second
 * interval (30 FPS on a fast machine) to move things along their time-based
 * animation paths. Easing is achieved through an adaptation of Robert Penner's
 * ActionScript Easing Equations.
 *
 * @author  Dan Phiffer <dan@phiffer.org>
 * @version 0.1
 */

function animator(instance) {
    
    this.instance = instance;
    this.registry = new Array();
    this.interval = null;
    
    this.register = function(target) {
        for (var i = 0; i < this.registry.length; i++) {
            if (target == this.registry[i]) {
                return false;
            }
        }
        this.registry.push(target);
        
        if (!this.interval) {
            this.interval = setInterval(this.instance + '.iterate();', 33);
        }
        
        return true;
    }
    
    this.unregister = function(target) {
        var new_registry = new Array();
        var found_target = false;
        for (var i = 0; i < this.registry.length; i++) {
            if (target == this.registry[i]) {
                found_target = true;
                continue;
            }
            new_registry.push(this.registry[i]);
        }
        this.registry = new_registry;
        
        if (this.registry.length == 0) {
            clearInterval(this.interval);
            this.interval = null;
        }
        
        return found_target;
    }
    
    this.iterate = function() {
        for (i = 0; i < this.registry.length; i++) {
            if (!this.handle_object(i)) {
                this.unregister(this.registry[i]);
            }
        }
    }
    
    this.handle_object = function(num) {
        var moving = false;
        var target = this.registry[num];
        if (target.animate) {
            var n = 0;
            for (attr in target.animate) {
                moving = moving || this.handle_attr(target, attr, n);
                n++;
            }
        }
        return moving;
    }
    
    this.handle_attr = function(target, attr, n) {
        
        var guide = target.animate[attr];
        var now = new Date();
        var time = now.getTime();
        
        var end_val  = guide[0];
        var duration = guide[1];
        var func     = guide[2];
        var callback = guide[3];
        
        if (guide.length < 6) {
            eval('guide[4] = parseFloat(target.style.' + attr + ');');
            guide[5] = time;
        }
        
        var start_val  = guide[4];
        var start_time = guide[5];
        var end_time   = start_time + duration;
        
        if (time >= end_time) {
            this.set_value(target, attr, end_val);
            if (typeof(callback) == 'function') {
                callback();
            }
            return false;
        } else {
            var val_diff = end_val - start_val;
            var val = func(time - start_time, start_val, val_diff, duration);
            this.set_value(target, attr, val);
            return true;
        }
    }
    
    this.set_value = function(target, param, value) { 
        if (param == 'backgroundColor' || param == 'color') {
            eval('target.style.' + param + ' = "#' + value + '";');
        } else if (param == 'opacity') {
            eval('target.style.' + param + ' = ' + value + ';'); 
        } else {
            eval('target.style.' + param + ' = "' + value + 'px";');
        }
    }
    
    /*
     * The following was adapted from Robert Penner's Easing Equations, which
     * were originally written for Flash. Available at http://www.robertpenner.com/easing/
     *
     * Easing Equations (c) 2003 Robert Penner, all rights reserved.
     * This work is subject to the terms in http://www.robertpenner.com/easing_terms_of_use.html.
     */
    
    this.ease = new Object;
    
    // simple linear tweening - no easing
    // t: current time, b: beginning value, c: change in value, d: duration
    this.ease.linear = function (t, b, c, d) {
        return c * t / d + b;
    }
    
     ///////////// QUADRATIC EASING: t^2 ///////////////////

    // quadratic easing in - accelerating from zero velocity
    // t: current time, b: beginning value, c: change in value, d: duration
    // t and d can be in frames or seconds/milliseconds
    this.ease.quad_in = function (t, b, c, d) {
        return c*(t/=d)*t + b;
    }
    
    // quadratic easing out - decelerating to zero velocity
    this.ease.quad_out = function (t, b, c, d) {
        return -c *(t/=d)*(t-2) + b;
    }
    
    // quadratic easing in/out - acceleration until halfway, then deceleration
    this.ease.quad_both = function (t, b, c, d) {
        if ((t/=d/2) < 1) return c/2*t*t + b;
        return -c/2 * ((--t)*(t-2) - 1) + b;
    }
    
    
     ///////////// CUBIC EASING: t^3 ///////////////////////
    
    // cubic easing in - accelerating from zero velocity
    // t: current time, b: beginning value, c: change in value, d: duration
    // t and d can be frames or seconds/milliseconds
    this.ease.cubic_in = function (t, b, c, d) {
        return c*(t/=d)*t*t + b;
    }
    
    // cubic easing out - decelerating to zero velocity
    this.ease.cubic_out = function (t, b, c, d) {
        return c*((t=t/d-1)*t*t + 1) + b;
    }
    
    // cubic easing in/out - acceleration until halfway, then deceleration
    this.ease.cubic_both = function (t, b, c, d) {
        if ((t/=d/2) < 1) return c/2*t*t*t + b;
        return c/2*((t-=2)*t*t + 2) + b;
    }
    
    
     ///////////// QUARTIC EASING: t^4 /////////////////////
    
    // quartic easing in - accelerating from zero velocity
    // t: current time, b: beginning value, c: change in value, d: duration
    // t and d can be frames or seconds/milliseconds
    this.ease.quart_in = function (t, b, c, d) {
        return c*(t/=d)*t*t*t + b;
    }
    
    // quartic easing out - decelerating to zero velocity
    this.ease.quart_out = function (t, b, c, d) {
        return -c * ((t=t/d-1)*t*t*t - 1) + b;
    }
    
    // quartic easing in/out - acceleration until halfway, then deceleration
    this.ease.quart_both = function (t, b, c, d) {
        if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
        return -c/2 * ((t-=2)*t*t*t - 2) + b;
    }
    
    
     ///////////// QUINTIC EASING: t^5  ////////////////////
    
    // quintic easing in - accelerating from zero velocity
    // t: current time, b: beginning value, c: change in value, d: duration
    // t and d can be frames or seconds/milliseconds
    this.ease.quint_in = function (t, b, c, d) {
        return c*(t/=d)*t*t*t*t + b;
    }
    
    // quintic easing out - decelerating to zero velocity
    this.ease.quint_out = function (t, b, c, d) {
        return c*((t=t/d-1)*t*t*t*t + 1) + b;
    }
    
    // quintic easing in/out - acceleration until halfway, then deceleration
    this.ease.quint_both = function (t, b, c, d) {
        if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
        return c/2*((t-=2)*t*t*t*t + 2) + b;
    }
    
    
    
     ///////////// SINUSOIDAL EASING: sin(t) ///////////////
    
    // sinusoidal easing in - accelerating from zero velocity
    // t: current time, b: beginning value, c: change in position, d: duration
    this.ease.sine_in = function (t, b, c, d) {
        return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
    }
    
    // sinusoidal easing out - decelerating to zero velocity
    this.ease.sine_out = function (t, b, c, d) {
        return c * Math.sin(t/d * (Math.PI/2)) + b;
    }
    
    // sinusoidal easing in/out - accelerating until halfway, then decelerating
    this.ease.sine_both = function (t, b, c, d) {
        return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
    }
    
    
     ///////////// EXPONENTIAL EASING: 2^t /////////////////
    
    // exponential easing in - accelerating from zero velocity
    // t: current time, b: beginning value, c: change in position, d: duration
    this.ease.expo_in = function (t, b, c, d) {
        return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
    }
    
    // exponential easing out - decelerating to zero velocity
    this.ease.expo_out = function (t, b, c, d) {
        return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
    }
    
    // exponential easing in/out - accelerating until halfway, then decelerating
    this.ease.expo_both = function (t, b, c, d) {
        if (t==0) return b;
        if (t==d) return b+c;
        if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
        return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
    }
    
    
     /////////// CIRCULAR EASING: sqrt(1-t^2) //////////////
    
    // circular easing in - accelerating from zero velocity
    // t: current time, b: beginning value, c: change in position, d: duration
    this.ease.circ_in = function (t, b, c, d) {
        return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
    }
    
    // circular easing out - decelerating to zero velocity
    this.ease.circ_out = function (t, b, c, d) {
        return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
    }
    
    // circular easing in/out - acceleration until halfway, then deceleration
    this.ease.circ_both = function (t, b, c, d) {
        if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
        return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
    }
    
    // (Some other functions were a little harder to adapt, so I left them out.)
    
}

