// Add the default callBack-handler:
Widget.addCallBack("Console", function(params) {
	// Set the prompt:
	Widget.getClass("Console").prompt = params.prompt;
	
	// Add the widget to the Console handler:
	Widget.getClass("Console").addWidget(params.widgetId);
});

/**
 * The Console static class.
 * 
 * @version 08-06-2010
 * @author <a href="mailto:r.tennapel@griponservice.nl?SUBJECT=Console script.js">R. ten Napel, ing.</a>
 **/
function Console() {}

// Console vars:
Console.widgets = Array();
Console.prompt = "";

/**
 * Add a widget to the list of Console widgets to be handled when a console is used. If the ID is already
 * stored, it won't be added.
 * 
 * @param widgetId			The widget ID of the widget to handle.
 * 
 * @return					The index of the widget ID.
 **/
Console.addWidget = function(widgetId) {
	// Retrieve the index of the given widget:
	var i = Console.widgets.indexOf(widgetId);
	
	// If no index, add the widget and handler array:
	if (i == -1) {
		Console.widgets.add(widgetId);
		
		i = Console.widgets.length - 1;
	}
	
	return i;
};

/**
 * Submit a command.
 * 
 * @param id				The widget ID of the widget to handle.
 * @param source			The input element.
 **/
Console.submit = function(id, source) {
	// Retrieve the history div of the given widget ID:
	var chain = $(id + "_history");
	
	// Append the html at the end:
	chain.append(source.value + "<br />", false);
	
	// Call the command through ajax:
	var ajax = new Ajax(null, "./Framework/Widgets/Console/ajaxconsole.php?widgetId=" + id + "&command=" + source.value);
	ajax.onSuccess = function(response) {
		chain.append(response + "<br />" + Console.prompt, false);
		
		// Scroll to the bottom:
		chain.$().scrollTop = chain.$().scrollHeight;
	};
	ajax.request();
	
	// Clear the input text:
	source.value = "";
};

//Store the Console static JS class:
Widget.addClass("Console", Console);
