/**
 * This is the main class of the W@W client-side architecture.
 */
var WAWController = function(){};

// The list of the pending calls
WAWController.calls = new Hash();

// Make a call
WAWController.call = function(qPath, options) {
	if (options == null) {
		options = new Hash();
	}
	
	if (options["form"] != null) {
		var form = $(options["form"]);
		form = $(form);
		if (form != null && Form.getElements(form) != null) {
			var elements = Form.getElements(form);
			for (i=0; i<elements.length; i++) {
				var name = elements[i].name;
				var value = elements[i].getValue();
				options["params"][name] = value;
			}
		}
	}
	
	var action = WAWResource.get(qPath);
	if (action != null && action.execute != null) {
		WAWLogger.info("WAWController.call", "Action '"+qPath+"' executing");
		// Save the xml http request associated to the action 
		var xhr = action.execute(options["params"]);
		if (xhr != null) {
			WAWController.calls.set(xhr,action);
		}
	} else {
		WAWLogger.warning("WAWController.call", "Action '"+qPath+"' not found");
	}
}

WAWController.error = function(response)
{
	alert(response.responseText);
	var action = WAWController.calls.get(response);
	if (action == null || action.qPath == null) {
		WAWLogger.warning("WAWController", "A failure has been received for an unknow action...");
	} else {
		WAWLogger.warning("WAWController", "A failure has been received for action : '"+action.qPath+"'");
		WAWController.calls.unset(response);
		delete response;
	}
}

WAWController.update = function(response)
{
	var action = WAWController.calls.get(response);
	if (action == null || action.qPath == null) {
		WAWLogger.fatal("WAWController", "A response has been received for an unknow action...");
	} else {
		WAWLogger.info("WAWController", "Response received for action : '"+action.qPath+"'");
	}

	var doc = response.responseXML;
	if (doc == null) {
		WAWLogger.warning("WAWController", "An action has returned an incorrect answer...");
		return WAWController.error(response);
	}

	// Takes the qPath and the result of the executed action
	var action = doc.getElementsByTagName("action")[0];
	var action_qPath = action.getElementsByTagName("qPath")[0].firstChild.nodeValue;
	var action_result = action.getElementsByTagName("result")[0].firstChild.nodeValue;

	// Fetch the ajax-module code generation, and evaluate it
	var generation = doc.getElementsByTagName("generation")[0];
	eval(generation.firstChild.nodeValue);
	
	// Fetch the code of views to update
	var root = doc.getElementsByTagName("views")[0];
	var views = root.getElementsByTagName("view");
	// For each refreshed view update HTML
	for (i = 0; i<views.length; i++)
	{
		var content = views[i].firstChild.nodeValue;
		var container_id = views[i].attributes.getNamedItem("containerID").nodeValue;
		var container = $(container_id);
		if (container != null)
		{
			container.update(content);
		} else {
			WAWLogger.fatal("WAWController.update", "An ajax response indicate a unexistant container to update : '"+container_id+"'.");
		}
	}
	WAWEvent.fire("wevt://"+action_qPath+"::"+action_result);
	WAWController.calls.unset(response);
	delete response;
}
