function Qscheduler(fun, interval) {
    var fail = fun;
    var work = new Array();
    var slots = new Array();
    var schedule = null;

    function action() {
        var current = slots.shift();
        if(current instanceof Array)
            work.push.apply(work, current);
        while(work.length > 0)
            try {
                var f = work.shift();
                f();
            } 
            catch(e) {
                fail(e);
            }
    }

    this.timeslots = slots;
    this.queue = work;
    this.timestep = interval || 100;

    schedule = window.setInterval(action, this.timestep);
}

Qscheduler.prototype.asap = function(f) {
    this.queue.push(f);
}

Qscheduler.prototype.schedule = function(ms, f) {
    var index = Math.floor(ms / this.timestep);
    var slot = this.timeslots[index] || new Array();
    slot.push(f);
    this.timeslots[index] = slot;
}

Qscheduler.prototype.steps = function(count, ms, f) {
    while(count > 0)
        this.schedule(ms * count--, f);
}

Qscheduler.prototype.steps_asap = function(count, ms, f) {
    this.steps(count - 1, ms, f);
    this.asap(f);
}

Qscheduler.prototype.periodical = function(ms, f) {
    var host = this;
    var future = function() {
        host.schedule(ms, future);
        f();
    }
    host.asap(future);
}

/*
 * Software License
 * 
 * Copyright (c) 2006 Juha M. Pohjalainen.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * 3. The end-user documentation included with the redistribution, if any, must
 *    include the following acknowledgment:
 * 
 *       "This product includes software developed by Juha M. Pohjalainen."
 * 
 *    Alternately, this acknowledgment may appear in the software itself, if and
 *    wherever such third-party acknowledgments normally appear.
 * 
 * 4. The name "Juha M. Pohjalainen" must not be used to endorse or promote
 *    products derived from this software without prior written permission.
 *    For written permission, please contact jmp@iki.fi.
 * 
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL JUHA M.
 * POHJALAINEN BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */
