// FreeStyler Library // Copyright 2009 Ian Porter

function fsSequencer()
{
    this.steps = new Array();
    this.dir = 0;
    this.nextIndex = -1;
    return this;
}

fsSequencer.prototype.add = function()
{
    for (var i=0; i<this.add.arguments.length; i++)
        this.steps.push( this.add.arguments[i] );
}

fsSequencer.prototype.isAtStart = function()
{
    return this.nextIndex < 0;
}
fsSequencer.prototype.isAtEnd = function()
{
    return this.nextIndex > this.steps.length-1;
}
fsSequencer.prototype.isBusy = function()
{
    return this.dir;
}

fsSequencer.prototype.go = function(dir)
{
    if(this.isBusy())
        return;
    
    if(dir)
        this.dir = dir;
    else if(this.isAtStart())
        this.dir = 1;
    else if(this.isAtEnd())
        this.dir = -1;
        
    this.next();
}
    
fsSequencer.prototype.next = function()
{
    this.nextIndex = this.nextIndex + this.dir;

    if ( (this.dir>0 && this.isAtEnd()) || (this.dir<0 && this.isAtStart()) )
    {
        this.dir = 0;
        return; // done
    }
   
    var next = this.steps[this.nextIndex];
    switch(typeof next)
    {
        case 'object':
            next.sequence = this;
            next.go(this.dir);
            break;
        case 'function':
            next.call();
            this.next();
            break;
        case 'string':
            eval(next);
            this.next();
            break;
    }
}

