
var AsyncMacroCommand = function(){
    this.Extends = Notifier;

    /**
     * An array of <code>SimpleCommands</code>
     * or subclasses of
     * @type Array
     */
    this.subCommands = null;
    this.note/*INotification*/=null;
    this.onComplete/*Function*/ = null;
    /**
     * @ignore
     */
    this.initialize = function()
    {
    	this.nextCommand = this.nextCommand.bindWithEvent(this);
    	
    	this.subCommands = [];
    	this.initializeAsyncMacroCommand();
    }

    /**
     * Initialize the <code>MacroCommand</code>.
     *
     * <P>
     * In your subclass, override this method to
     * initialize the <code>MacroCommand</code>'s <i>SubCommand</i>
     * list with <code>ICommand</code> class references like
     * this:</P>
     *
     * <listing>
     *		// Initialize MyMacroCommand
     *		initializeMacroCommand : function()
     *		{
     *			this.addSubCommand(FirstCommand);
     *			this.addSubCommand(SecondCommand);
     *			this.addSubCommand(ThirdCommand);
     *		}
     * </listing>
     *
     * <P>
     * Note that <i>SubCommand</i>s may be any <code>Command</code> implementor,
     * <code>MacroCommand</code>s or <code>SimpleCommands</code> are both acceptable.
     */
    this.initializeAsyncMacroCommand = function(){}

    /**
     * Add a <i>SubCommand</i>.
     *
     * <P>
     * The <i>SubCommands</i> will be called in First In/First Out (FIFO)
     * order.</P>
     *
     * @param {Class} commandClassRef a reference to the <code>Class</code> of the <code>ICommand</code>.
     */
    this.addSubCommand = function(commandClassRef /* Class */)/*void*/{
    	
    	this.subCommands.push(commandClassRef);
    }
    
    this.execute = function(notification/*INotification*/)/*void*/{
    	this.note = notification;
    	this.nextCommand();
    }

    this.nextCommand = function() {
    	
    	if (this.subCommands.length > 0){
    		
			var commandClassRef/*Class*/	=	this.subCommands.shift();
			var commandInstance/*Object*/	=	new commandClassRef();
			var isAsync/*Boolean*/			=	(!(commandInstance.setOnComplete == undefined || commandInstance.setOnComplete == null));
			
			if (isAsync) commandInstance.setOnComplete( this.nextCommand );

			commandInstance.initializeNotifier(this.multitonKey);		
			commandInstance.execute( this.note );
			
			if (!isAsync) this.nextCommand();
			
		}else{
			if( this.onComplete != null ) this.onComplete();
		}
    }
    
    
}
AsyncMacroCommand = new Class(new AsyncMacroCommand());
