/**
 * Borrowed from Java SE 1.5:
 * 
 * This class represents an observable object, or "data"
 * in the model-view paradigm. It can be subclassed to represent an 
 * object that the application wants to have observed. 
 * <p>
 * An observable object can have one or more observers. An observer 
 * may be any object that implements <code>update(eventName, eventData)</code> function. 
 * After an observable instance changes, an application calling the 
 * <code>Observable</code>'s <code>notifyObservers</code> method  
 * causes all of its observers to be notified of the change by a call 
 * to their <code>update</code> method. The Observable class will notify 
 * Observers in the order in which they registered.
 * <p>
 * When an observable object is newly created, its set of observers is 
 * empty.
 */
HD.util.Observable = function() {
	this.observers = [];
};

HD.util.Observable.prototype = {
	/**
	 * Sets the list of observers.
	 * 
	 * @param {Array} observers the observers to set
	 */
	setObservers : function(observers) {
		this.observers = observers;
	},
	
	/**
	 * Adds the observer to the list of observers.
	 * 
	 * @param {Object} observer the observer to add
	 */
	addObserver : function(observer) {
		this.observers.push(observer);				
	},
	
	/**
	 * Removes the observer from the list of observers.
	 * 
	 * @param {Object} observer the observer to remove
	 */
	removeObserver : function(observer) {
		var tempObservers = [];
		var observers = this.observers;
		for(var i = 0, len = observers.length; i < len; i++) {
			var tempObserver = observers[i];
			if(tempObserver != observer) {
				tempObservers.push(tempObserver);
			}
		}
		this.setObservers(tempObservers);
	},
	
	/**
	 * Notifies each observer in the list of the event and passes in event data.
	 * 
	 * @param {String} eventName the name of the event to signal 
	 * @param {Object} eventData the data associated with the event
	 */
	notifyObservers : function(eventName, eventData) {
		var observers = this.observers;
		for(var i = observers.length - 1; i > -1; i--) {
			observers[i].update(eventName, eventData);
		}
	}
};
