/**
 * @author hedgehog
 */

function EventDelegator(options){
	if(this.__EVENT_DELEGATOR__) return;
	this.__EVENT_DELEGATOR__=true;
	var _subscriptions={};
	
	this.subscribe_to=function(event_key,event_delegate){
		if(!_subscriptions[event_key]){
			_subscriptions[event_key]=[];
		}
		
		_subscriptions[event_key].push(event_delegate);
	};
	
	this.unsubscribe_to=function(event_key,event_delegate){
        if (event_delegate==null){
            _subscriptions[event_key] = null;
        }else {
            $.each(_subscriptions[event_key],function(i,n){
              if(event_delegate==n){
                  _subscriptions[event_key][i]=null;
              }
            });
        }
	};
	
	this.raise_event=function(event_name,args){
        function each_subscription(index,item){
			if(item){
				item(args);
			}
			
		};

		if (_subscriptions[event_name]) {
			$.each(_subscriptions[event_name], each_subscription);
		}
	};
};
