// --------------------------------------------------------------------
// Class Popup
// 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) {
				bodyObj.removeEventListener(args[i], this.distribute, false)
				bodyObj.addEventListener(args[i], this.distribute, "true");
			} 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;
		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);
			}
	    }
		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();
		}
		return returnValue;
	}
	this.distribute.scope = this;
}




