// --------------------------------------------------------------------
// Class Controller
// (C) Copyright by Publicform GmbH (www.publicform.de)
//
// Version: 3.1
// Date: 2004-07-28
// Author: Andreas Doelling (doelling@publicform.de)
//
// Listens to events that are registered to the instance.
// Delegates the events to client objects that have methods to
// handle the respective events, e.g. a method click() or mouseover().
// --------------------------------------------------------------------

function Controller() {
	// the objects to delegate the events to
	this.clients = new Array();
	
	
	// -----------------------------------------------------
	// Method registerEvents
	// Adds event listeners to the document body giving
	// its own distribute() method as handler.
	// -----------------------------------------------------
	this.registerEvents = function() {
		var bodyObj = document.getElementsByTagName("BODY")[0];
		var args = this.registerEvents.arguments;
		for(var i=0; i<args.length; i++) {
			if(bodyObj.addEventListener) {
				if(args[i] == 'unload' && window.onunload != null) {
					window.removeEventListener(args[i], this.distribute, false);
					window.addEventListener(args[i], this.distribute, "true");
				} else {
					bodyObj.removeEventListener(args[i], this.distribute, false)
					bodyObj.addEventListener(args[i], this.distribute, "true");
				}
			} else {
				if(args[i] == 'unload' && window.onunload != null) {
					window.detachEvent("on"+args[i], this.distribute);
					window.attachEvent("on"+args[i], this.distribute);
				} else {
					bodyObj.detachEvent("on"+args[i], this.distribute);
					bodyObj.attachEvent("on"+args[i], this.distribute);
				}
			}
		}
	}

	
	// -----------------------------------------------------
	// Method registerClient
	// Adds client object to clients array.
	// -----------------------------------------------------
	this.registerClient = function(client) {
		this.clients[this.clients.length] = client;
	}

	
	// -----------------------------------------------------
	// Method distribute
	// Delegates event to those client objects that have an
	// adequate method to handle it.
	// -----------------------------------------------------
	this.distribute = function(evt) {
		var returnValue = true;
		if(this != arguments.callee.scope){
			if(typeof arguments.callee.apply == "function") {
			  	return arguments.callee.apply(arguments.callee.scope, arguments);
			} else {
		  		return arguments.callee.scope.distribute(evt);
			}
	    }
		try {
			for(var i=0; i<this.clients.length; i++) {
				if(typeof(this.clients[i][evt.type]) == "function") {
					var handleEvt = this.clients[i][evt.type];
					returnValueTemp = handleEvt(evt);
					returnValue = (returnValue == false)? false : returnValueTemp;
				}
			}
			if(returnValue == false) {
				evt.cancelBubble = true;
				if (evt.stopPropagation) evt.stopPropagation();
				if(evt.preventDefault) evt.preventDefault();
			}
		}
		catch(err) {}
		return returnValue;
	}
	this.distribute.scope = this;
}




