// Chris Pyper 2006
// Central control interface

// Instantiated interface object
var editorInterface = false;

// This class acts as a interface between the editor and and outside user interface functionality
// It manages the the event handlers for the various events.  It is designed to work as two way 
// channel of coummunication between the editor and editor interface.  For instance, a function
// could be registed by the editor to trigger something in the interfce, of the interface
// could register a function to trigger something in the editor.  By managing all communication
// like this it is very easy to implement features such as context sensitive menus.
function EditorInterface()
{
	this.callBacks = new Array();

	// Register a function to a event
	this.registerCallBack = function(callBackKey, callBackFunctionReference)
	{
		if(this.callBacks[callBackKey])
			debug('registerCallBack() call back exists = ' + callBackKey, 'info');
		else
		{
			debug('registerCallBack(): Adding event handler - ' + callBackKey, 'warn');
			this.callBacks[callBackKey] = callBackFunctionReference;
		}
	}

	// Delete a callback
	this.unregisterCallBack = function(callBackKey)
	{
		debug('registerCallBack(): Removing event handler - ' + callBackKey, 'warn');
		this.callBacks[callBackKey] = false;
	}

	// Delete all CallBacks
	this.unregisterAllCallBacks = function()
	{
		debug('unregisterAllCallBacks(): Removing all event handlers', 'warn');
		this.callBacks = new Array();
	}

	// Fetch a callback for a event
	this.fetchCallBack = function(callBackKey)
	{
		debug('fetchCallBack(): Fetching event handler - ' + callBackKey, 'info');
		return this.callBacks[callBackKey];
	}

	// Trigger a callback
	// The reason for all the var is that I want to support variable arguaments.  This is possible in javascript
	// but only works with one functiion, chaining two together is not possible, this hack will allow that
	this.triggerCallBack = function(callBackKey, var0, var1, var2, var3, var4, var5, var6, var7, var8, var9)
	{
		if(!this.callBacks[callBackKey])
			debug('triggerCallBack() call back does not exist = ' + callBackKey, 'warn');
		else
		{
			debug('triggerCallBack(): Triggering event handler - ' + callBackKey, 'info');
			return this.callBacks[callBackKey](var0, var1, var2, var3, var4, var5, var6, var7, var8, var9);
		}
	}

	// test
	this.testCallBack = function()
	{
		alert('Call Back Test!');
	}
}

// Factory method to manage Editor Interface Object
function fetchInterface()
{
	if(!editorInterface)
		editorInterface = new EditorInterface();
	return editorInterface;
}

