/**
 * SUBJECT 
 *
 * A javascript class that follows the subject/observers pattern.  It contains a list of observers
 * and notifies them in case of an relevant event occurs.  
 *
 * @param {function} observers A list of functions that needs to be notified when an event occurs
 */

var Subject = Class.create();

Subject.prototype = {
	observers: null,
	
	initialize: function() {},
	
	/* 
	* add a observer to the observers list
	*
	* @param{string} an unique name for the observer function
	* @param{function} a observer function to be invoked in case of an event
	*/
	attachObserver: function(key, observer) {
		if (this.observers==null)
			this.observers = new Object();
		if (!Object.keys(this.observers).include(key)) {
			this.observers[key] = observer;
			return true;
		}
		return false;
	},
	
	detachObserver: function(key) {
		if (this.observers!=null && Object.keys(this.observers).include(key))
			delete this.observers[key];
	},
	
	notifyObservers: function(param) {
		var commitAction = true;
		if (this.observers != null) {
			Object.values(this.observers).each(function(observer) {
				if (commitAction == false)
					return commitAction;
					
				if (!observer(param))
					commitAction = false;
			});
		}
		return commitAction;
	}
}

