// --------------------------------------------------------------------
// Class Controller
// (C) Copyright by Andreas Doelling (www.a-doelling.de)
// This file is part of the JS-Pocket-Lib.
// The JS-Pocket-Lib is licenced under the CC-GNU LGPL 
// (http://creativecommons.org/licenses/LGPL/2.1/).
//
// Version: 4.0
// Date: 2006-03-04
// Author: Andreas Doelling (doelling@a-doelling.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
	var self = this;
	self.clients = new Array();
	
	
	// -----------------------------------------------------
	// Method registerEvents
	// Adds event listeners to the document body giving
	// its own distribute() method as handler.
	// -----------------------------------------------------
	self.registerEvents = function(eventTitles) {
		var bodyObj = document.getElementsByTagName("BODY")[0];
		var bodyObj = document;
		for(var i=0; i<eventTitles.length; i++) {
			if(document.addEventListener) {
				if(eventTitles[i] == 'unload' && window.onunload != null) {
					window.removeEventListener(eventTitles[i], self.distribute, false);
					window.addEventListener(eventTitles[i], self.distribute, "true");
				} else {
					bodyObj.removeEventListener(eventTitles[i], self.distribute, false)
					bodyObj.addEventListener(eventTitles[i], self.distribute, "true");
				}
			} else {
				if(eventTitles[i] == 'unload' && window.onunload != null) {
					window.detachEvent("on"+eventTitles[i], self.distribute);
					window.attachEvent("on"+eventTitles[i], self.distribute);
				} else {
					bodyObj.detachEvent("on"+eventTitles[i], self.distribute);
					bodyObj.attachEvent("on"+eventTitles[i], self.distribute);
				}
			}
		}
	}

	
	// -----------------------------------------------------
	// Method registerClient
	// Adds client object to clients array.
	// -----------------------------------------------------
	self.registerClient = function(client) {
		self.clients[self.clients.length] = client;
	}

	
	// -----------------------------------------------------
	// Method distribute
	// Delegates event to those client objects that have an
	// adequate method to handle it.
	// -----------------------------------------------------
	self.distribute = function(evt) {
		var returnValue = true;
		try {
			for(var i=0; i<self.clients.length; i++) {
				if(typeof(self.clients[i][evt.type]) == "function") {
					var handleEvt = self.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;
	}
	
}



