// Broadcaster - Event Listener, Observer-pattern Object
// Original author Unknown, port to javascript by James Alcide Park (jalcide.com) 
// requires lib: standardECMAprototypes.js
BroadcasterClass = function(){
	this._version = '1.1';
};
BroadcasterClass.prototype.getVersion = function(){
	return this._version;
};
BroadcasterClass.prototype.broadcastMessage = function(func_str, argObj){
	var a = this._listeners;
	var l = a.length;
	for (var i=l-1; i>=0; i--){
		if (typeof a[i] != "undefined") {
			if(typeof argObj != "undefined"){
				//alert("(argObj) broadcasting message: "+func_str+" to: "+a[i]._name+" argObj: "+argObj._name);
				if(typeof a[i][func_str] != "undefined"){
					a[i][func_str](argObj);
				}
			}
			else{
				//alert("(no argObj) broadcasting message: "+func_str+" to: "+a[i]._name);
				if(typeof a[i][func_str] != "undefined"){
					a[i][func_str]();
				}
			}
		}else
		{
			a.splice(i, 1);
		}
	}
};
BroadcasterClass.prototype.addListener = function(obj){
	var a = this._listeners;
	var found = false;
	var i = a.length;
	while (i--){
		if(a[i] == obj){
			found = true;
			break;
		}
	}
	if(found == true){
		return false;
	}
	a.unshift(obj);
	//if(this.broadcastMessage == null){
	//	this.broadcastMessage = this._broadcastMessage;
	//}
	return true;
};
BroadcasterClass.prototype.removeListener = function(obj){
	var a = this._listeners;
	var i = a.length;
	while (i--) {
		if (a[i] == obj) {
			a.splice(i, 1);
			break;
		}
	}
	if (a.length == 0) {
		//this.broadcastMessage = null;
	}
};
BroadcasterClass.prototype.initialize = function(obj){
	delete this._listeners;
	obj.broadcastMessage = this.broadcastMessage;
	obj.addListener = this.addListener;
	obj.removeListener = this.removeListener;
	obj._listeners = [];
};

