
function Broadcaster () {
	
}

Class(Broadcaster);

Broadcaster.prototype._listeners = new Array();

Broadcaster.initialize = function (o, dontCreateArray) {
	if (o.broadcastMessage != undefined) delete o.broadcastMessage;
	o.addListener = Broadcaster.prototype.addListener;
	o.removeListener = Broadcaster.prototype.removeListener;
	if (!dontCreateArray) o._listeners = new Array();
}

Broadcaster.prototype.addListener = function (o) {
	this.removeListener (o);
	
	if (this.broadcastMessage == undefined) {
		this.broadcastMessage = Broadcaster.prototype.broadcastMessage;
	}
	return this._listeners.push(o);
}

Broadcaster.prototype.removeListener = function (o) {
	var a = this._listeners;	
	var i = a.length;
	while (i--) {
		if (a[i] == o) {
			a.splice (i, 1);
			if (!a.length) this.broadcastMessage = undefined;
			return true;
		}
	}
	return false;
}

Broadcaster.prototype.broadcastMessage = function () {
	var e = String(Array.prototype.slice.call(arguments).shift());
	var a = this._listeners.concat();
	var l = a.length;
	
	for (var i=0; i<l; i++) {
		a[i][e].apply(a[i], arguments);
	}
}

