"use strict";

var ScriptedEventManager = function(objMgr, dlgMgr){
	this.events = [];
	this.objMgr = objMgr;
	this.dlgMgr = dlgMgr;
}
	ScriptedEventManager.prototype.run = function(eventname,cacheVars,objMgr, dlgMgr){
		if (!eventname || typeof(eventname)!='string') return;
		objMgr = objMgr ? objMgr : this.objMgr;
		dlgMgr = dlgMgr ? dlgMgr : this.dlgMgr;
		cacheVars = cacheVars ? cacheVars : {};

		// shorthands for purely dialogue events
		if (eventname.substr(0,3)=="GD_"){
			cacheVars.dialogueName = eventname.substr(3);
			this.events.push(new ScriptedEvent(DATA_SDS_SCRIPTED_EVENTS["Generic_dialogue"],cacheVars,objMgr,dlgMgr));
		}
		else if (eventname.substr(0,6)=="DIARY_"){
			cacheVars.diaryName = eventname.substr(6);
			this.events.push(new ScriptedEvent(DATA_SDS_SCRIPTED_EVENTS["Save_dialogue"],cacheVars,objMgr,dlgMgr));
		}
		else if (DATA_SDS_SCRIPTED_EVENTS[eventname]){
			this.events.push(new ScriptedEvent(DATA_SDS_SCRIPTED_EVENTS[eventname],cacheVars,objMgr,dlgMgr));
		}
	}

	ScriptedEventManager.prototype.Update = function(){
		for (var i=0; i<this.events.length; i++){
			if (this.events[i].Update()){
				this.events.splice(i,1);
				i--;
			}
		}
	}