/*======================================================================
JSBroadcaster.js - Timothy Groves - desk [at] brandspankingnew.net
http://www.brandspankingnew.net/archive/2006/01/jsbroadcaster_b.html

02/17/06 - B.Liddell - modified to support multiple object initialization
======================================================================*/
//**************************************************
//
// Dependencies: mm.js, utils.js
//
//**************************************************

var JSBroadcaster = {};

JSBroadcaster.initialize = function (){
	if(arguments.length>0){
		for(var i=0; i<arguments.length; i++){
			
			var obj = arguments[i];
			
			if (typeof obj == "string")
				obj = getDOMElementByID(obj);
			if (obj == null)
				return false;
				
			obj._listeners = new Array();
			
			obj.addListener = function (obj){
				for (var i=0;i<this._listeners.length; i++) {
					if ( this._listeners[i] == obj)
						return false;
				}
				this._listeners.push( obj );
				return true;
			}
			
			obj.removeListener = function ( obj ){
				// remove obj from _listeners array
				for (var i=0;i<this._listeners.length; i++) {
					if ( this._listeners[i] == obj) {
						this._listeners.splice(i,1);
						return true;
					}
				}
				return false;
			}
			
			obj.broadcastMessage = function (func, str){
				// loop through listeners calling function
				for (var i=0;i<this._listeners.length; i++) {
					if ( typeof this._listeners[i][func] == "function" )
						this._listeners[i][func]( str );
				}
			}
		}
	}
}

/*
Usage:
---------------------------------------------
The initialize function turns the object passed as it's argument into a broadcaster.
We can now add an onload function that tells our broadcaster to send a message. 

var onLoadObj = {};
JSBroadcaster.initialize( onLoadObj );

window.onload = function () { onLoadObj.broadcastMessage( "onLoad" ); }

---------------------------------------------
Removing listeners:

var ldGraphic = {};
ldGraphic.onRequest = function () {
	// called when the AJAX function starts the request
	// and broadcasts "onRequest"
}
ldGraphic.onSuccess = function () {
	// called when the AJAX function receives data
	// and broadcasts "onSuccess"
	ajaxObj.removeListener( ldGraphic );
}
//Sample implementation, ajaxObj is fictional
function getData() {
	ajaxObj.addListener( ldGraphic );
	ajaxObj.doRequest( "foobar" );
}
---------------------------------------------
To become listeners, objects register themselves with a broadcaster 
using the public method broadcaster.addListener( obj ). 

var myListener = {};
myListener.onLoad = function () {
	// change background color
	var arr = document.getElementsByTagName("body");
	arr[0].style.backgroundColor = "#eee";
}
onLoadObj.addListener( myListener );
*/
