/*--------------------------------------------------------------

__  __ `__ \  _ \  ___/  __ `/_  __ \  __ \
_  / / / / /  __/ /__ / /_/ /_  / / / /_/ /
/_/ /_/ /_/\___/\___/ \__,_/ /_/ /_/\____/

	Version: 1.1
	
	Purpose: 	Sequencer for linear function calls as a chain reaction.
				Mostly used to support delays, like data transfert or animations.
	
	Usage:		this._aS = new ActionStack(this);
				var stack = [];
				stack[stack.length] = {func: this.play, wait: true};
				stack[stack.length] = {func: this.myFunction, param: ["param1","param2", ...]}; 
				stack[stack.length] = {trg: this._aS, func: this._aS.setProp, param: [targetMc, "_alpha", 0], wait: true};
				stack[stack.length] = {trg: this._aS, func: this._aS.insertDelay, param: [24, "frame"]};
				this._aS.setStack(stack);
				this._aS.startUp();
					
	
	Modified On: 2009/03/17
	
	Created by:	Patrick Picher
				patrick@mecano.com
							
	Copyright © 2008 Mecano							
	
	=Notes====
	Very DIRTY, needs a good clean up and more helper/wrapper functions
	
	1.1 	- use of startUp instead of start
--------------------------------------------------------------*/

var Delay = new Class({
    initialize: function(mDuration,mListener){
		this._duration = mDuration;
		this._listener = mListener;
		this.startUp();
    },
	startUp: function(){
		var self = this;
		setTimeout(function(){self.onEndDelay();}, this._duration);
	},
	onEndDelay: function(){
		this._listener.onEndDelay(this);
	}
});


var ActionStack = new Class({
    initialize: function(mTrg){
		this._trg = mTrg;
		this._actionStack = [];
    },
	next: function(){
		var self = this;
		if(this._actionStack.length > 0){
			var tAct = this._actionStack.shift();
			if(tAct.trg == null) tAct.trg = this._trg;
			if(tAct.param == null)tAct.param = [];
			tAct.func.apply(tAct.trg,tAct.param);
			if(tAct.wait != true)this.next();
						
		}else{
			//end of stack
		}
	},
	startUp: function(){
		this.next();
	},
	start: function(){//DEPRICATED
		this.startUp();
	},
	setStack: function(mStack){
		this._actionStack = mStack;
	},
	getCurrentStack: function(){
		if(this._actionStack.length < 1) return [];
		else return this._actionStack;
	},
	setProp: function(mTrg,mProp,mValue){
		mTrg[mProp] = mValue;
	},
	setStyle: function(mTrg,mProp,mValue){
		$(mTrg).setStyle(mProp,mValue);
	},
	insertDelay: function(mTime){
		new Delay(mTime,this);
	},
	onEndDelay: function(mRef){
		this.next();
	},
	emptyFunction: function(){
		
	},
	jsAlert: function(mV){
		alert(mV);	
	}
});