var Queue = new Class({

	/**
	 * whether the queue is currently processes
	 */
	processing:	false,
	
	/**
	 * the primary queue
	 */
	primary:	[],
	
	/**
	 * the secondary queue
	 */
	secondary:	[],


	/**
	 * if the queue is not processed right now
	 * add the command passed to the primary queue.
	 * Otherwise add it to the secondary queue to be
	 * processed in the second pass
	 */
	add:	function(cmnd)
	{
		if (! this.processing)
		{
			this.primary.push(cmnd);
		}
		else
		{
			this.secondary.push(cmnd);
		}
	},
	
	/**
	 * if the queue is not processed right now
	 * start the processing now.
	 * Otherwise do nothing.
	 */
	run:	function()
	{
		if (! this.processing) 
		{
			this.process();
		}
		else
		{
			return;
		}		
	},
	
	/**
	 * execute all the cmnds in the primary queue.
	 * When done, see if there are cmnds in the secondary queue.
	 * If so, move those cmnds to the now empty primary queue
	 * and run this method again.
	 */
	process:	function()
	{
		this.processing = true;
		
		this.primary.each(function(item)
		{
			if (typeof item == 'function')
			{
				item();
			}
			else if (typeof item == 'string')
			{
				$exec(item);
			}
		});
		
		this.primary = [];
		
		if (this.secondary.length > 0)
		{
			this.primary = this.secondary;
			this.secondary = [];
			this.process();
		}
		else
		{
			this.processing = false;
		}
		
	}

});

/**
 * instantiate the Queue class
 */
var queue = new Queue();