/**
 * Warning manager for adding warnings to one global handler
 */
function WarningManager() {
	var warnings = {};
	var callbacks = {};
	
	this.dump = function() { return [warnings, callbacks]; };
	
	var call_cbs = function(group)
	{
		if (callbacks[group] === undefined) { return; }
		for (var key in callbacks[group])
		{
			if(callbacks[group].hasOwnProperty(key) === true) {
				callbacks[group][key](warnings[group]);
			}
		}
	};
	
	this.add_listener = function(group, callback)
	{
		if (callbacks[group] === undefined) { callbacks[group] = []; }
		callbacks[group].push(callback);
	};

	/**
	 * Adds a warning to a specific group and object ref
	 * @param The group to add the warning for
	 * @param The object to add the warning for
	 * @param The warning itself
	 */
	this.add_warning_for = function(group, obj_ref, warning)
	{
		if (warnings[group] === undefined) { warnings[group] = {}; }
		if (warnings[group][obj_ref] === undefined) { warnings[group][obj_ref] = []; }
		
		// Check that the warning is not already presented
		for (var key in warnings[group]) {
			if (warnings[group].hasOwnProperty(key)) {
				if (warnings[group][key] == undefined) continue;
				for (var i = 0; i < warnings[group][key].length; i++) {
					// If the warning is presented we just return
					if (warnings[group][key][i] == warning) return ;
				}
			}
		}
		
		warnings[group][obj_ref].push(warning);
		call_cbs(group);
	};
	
	/**
	 * Removes all warnings for a specific group/obj_ref pair
	 * @param The group
	 * @param The object reference
	 */
	this.remove_warnings_for = function(group, obj_ref)
	{
		if (warnings[group] === undefined) { return; }
		warnings[group][obj_ref] = undefined;
		call_cbs(group);
	};
	
	/**
	 * Retrieves all warnings for one specific group.
	 */
	this.get_group_warnings = function(group)
	{
		if (warnings[group] === undefined) { return {}; }
		return warnings[group];
	};
}

var GlobalWarningManager = new WarningManager();
