//---------------------------------------------------------------------------
// LoadEvent.js
//--------------------------------------------------------------------------
//      Copyright (C) 2008 Unique Corporation. All Rights Reserved.
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
// Object
//---------------------------------------------------------------------------
Object.extend = function(destination, source) {
	for (var property in source) {
		destination[property] = source[property];
	}
	return destination;
};


//---------------------------------------------------------------------------
// LoadEvent
//---------------------------------------------------------------------------
if (!window.LoadEvent) var LoadEvent = {};

LoadEvent.cache = {};
Object.extend(LoadEvent, (function () {
	if (window.Prototype && window.Event && Event.observe) {
		return {
			addHandler: function(handler) {
				return Event.observe(element, name, handler);
			}
		};
	}


	var cache = LoadEvent.cache;

	function includeProperty(obj, property, handler) {
		for (var value in obj) {
			if (typeof value == 'object' && value[property] && value[property] == handler) {
				return true;
			}
		}
		return false;
	}

	function getEventID(element) {
		if (element._prototypeEventID) return element._prototypeEventID[0];
		arguments.callee.id = arguments.callee.id || 1;
		return element._prototypeEventID = [++arguments.callee.id];
	}

	function getCacheForID(id) {
		return cache[id] = cache[id] || { };
	}

	function getWrappersForEventName(id, eventName) {
		var c = getCacheForID(id);
		return c[eventName] = c[eventName] || [];
	}

	function createWrapper(element, eventName, handler) {
		var id = getEventID(element);
		var c = getWrappersForEventName(id, eventName);
		if (includeProperty(c, 'handler', handler)) return false;

		var wrapper = function(event) {
			if (event.eventName && event.eventName != eventName) return false;
			handler.call(element, event);
		};

		wrapper.handler = handler;
		c.push(wrapper);
		return wrapper;
	}

	function destroyCache() {
		for (var id in cache) {
			for (var eventName in cache[id]) {
				cache[id][eventName] = null;
			}
		}
	}

	if (window.attachEvent) {
		window.attachEvent('onunload', destroyCache);
	}

	return {
		addHandler: function(handler) {
			var element = window;
			var name = 'load';

			var wrapper = createWrapper(element, name, handler);
			if (!wrapper) return element;

			if (element.addEventListener) {
				element.addEventListener(name, wrapper, false);
			} else {
				element.attachEvent("on" + name, wrapper);
			}

			return element;
		}
	};
} )() );


//---------------------------------------------------------------------------
// End
//---------------------------------------------------------------------------
