"use strict";

var DialogueManager = function(){
	this.dialogues = [];
	this.blockerCount = 0;
	this.pauserCount = 0;

	this._heap = {};
};
	DialogueManager.prototype.open = function(dlgName,callback,auto){
		if (DATA_SDS_DIALOGUE[dlgName]){
			this.dialogues.push(new Dialogue(DATA_SDS_DIALOGUE[dlgName],new Controls(),callback,auto));
			if (DATA_SDS_DIALOGUE[dlgName].pauses){
				this.pauserCount++;
			}
			if (DATA_SDS_DIALOGUE[dlgName].blocks){
				this.blockerCount++;
			}
		}
	};

	DialogueManager.prototype.isPausing = function(){return (this.pauserCount>0);};
	DialogueManager.prototype.isBlocking = function(){return (this.blockerCount>0);};

	DialogueManager.prototype.Update = function(){
		for (this._heap.i=0; this._heap.i<this.dialogues.length; this._heap.i++){
			if (this.dialogues[this._heap.i].Update()){
				if (this.dialogues[this._heap.i].pauses){
					this.pauserCount--;
				}
				if (this.dialogues[this._heap.i].blocks){
					this.blockerCount--;
				}
				this.dialogues.splice(this._heap.i,1);
				this._heap.i--;
			}
		}
		for (this._heap.i=0; this._heap.i<this.dialogues.length-1; this._heap.i++){
			if (this.dialogues[this._heap.i].controls==GLOBAL_CONTROLS){
				this.dialogues[this._heap.i].controls=new Controls();
			}
		}
		if (this.dialogues.length>0){
			this.dialogues[this.dialogues.length-1].controls=GLOBAL_CONTROLS;
		}
	};

	DialogueManager.prototype.drawIntoCanvas = function(canvas){
		for (this._heap.i=0; this._heap.i<this.dialogues.length; this._heap.i++){
			this.dialogues[this._heap.i].drawIntoCanvas(canvas);
		}
	};
	