//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/classes/timeline [rev. #0]

TimeLine = function(fps, f){
    this.fps = fps, this.frames = f;
};
with({o: TimeLine, $: TimeLine.prototype}){
    o.timers = [];
    $.running = !!($.current = +(o.timer = $.time = null));
    o.run = function(){
        var o = this;
        o.timer || (o.timer = setInterval(function(){
            for(var h, d = +(new Date), t = o.timers, i = t.length; i--;){
                (!t[i].running || ((d - t[i].time) / (1e3 / t[i].fps) > t[i].current + 1 &&
                t[i].onframe(++t[i].current), t[i].current >= t[i].frames)) &&
                (h = t.splice(i, 1)[0], h.stop(1));
            }
        }, 1));
    };
    $.start = function(c){
        var _ = this;
        if(_.running) return;
        _.running = true, _.current = c || 0;
        _.time = new Date, _.onstart && _.onstart();
        if(!_.onframe || _.frames <= 0 || _.fps <= 0)
            return _.stop(1);
        o.timers.push(this), o.run();
    };
    $.stop = function(){
        var _ = this;
        _.running = false;
        if(!o.timers.length)
            o.timer = clearInterval(o.timer), null;
        arguments.length && _.onstop && _.onstop();
    };
}
