Parallel.INTERVAL_LENGTH = 30;

function Parallel(effects) {
	this.effects = effects;

	this.numIntervals = 0;
	for (var ii = 0; ii < this.effects.length; ii++) {
		this.numIntervals = Math.max(this.numIntervals, this.effects[ii].numIntervals);
	}
}

Parallel.prototype.registerCallback = function(functionRef) {
	this.callback = functionRef;
}

Parallel.prototype.start = function() {
	var obj = this;
	var functionReference = this.increment;
	var callback = function() { functionReference.call(obj) };
	this.interval = setInterval(callback, Parallel.INTERVAL_LENGTH);
}

Parallel.prototype.stop = function() {
	clearInterval(this.interval);
}

Parallel.prototype.end = function() {
	clearInterval(this.interval);
	for (var ii = 0; ii < this.effects.length; ii++) {
		this.effects[ii].end();
	}
	if (this.callback) {
		this.callback();
	}
}

Parallel.prototype.cancel = function() {
	clearInterval(this.interval);
	for (var ii = 0; ii < this.effects.length; ii++) {
		this.effects[ii].cancel();
	}
}

Parallel.prototype.increment = function() {
	this.numIntervals --;
	if (this.numIntervals > 0) {
		for (var ii = 0; ii < this.effects.length; ii++) {
			this.effects[ii].increment();
		}
	} else {
		this.end();
	}
}