/******************************************************************************

	Copyright (C) 2006-2007  John Ha

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

******************************************************************************/

if (!khanh) var khanh = {};
if (!khanh.event) {
	khanh.event = {
		unloadEvents: null,
		domloadEvents: [],
		domLoaded: 0,
	
		init: function() {
			if (!this.unloadEvents) {
				this.unloadEvents = [];
				var that = this;
				this.add(window, 'unload', function() { that.cleanup(that); });
			}
		},
	
		cleanup: function(that) {
			for (var i = 0; i < that.unloadEvents.length; i++)
				with (that.unloadEvents[i])
					that.remove(obj, type, fn, useCapture);
	
			that.unloadEvents = '';
		},
	
		add: function(obj, type, fn, useCapture) {
			if (type == 'domload') {
				this.domloadEvents.push(fn);
				return 1;
			}

			this.init();

			if (obj.addEventListener)
				obj.addEventListener(type, fn, useCapture);
			else if (obj.attachEvent)
				obj.attachEvent('on' + type, fn);
			else return 0;
			this.unloadEvents.push({ obj: obj, type: type, fn: fn, useCapture: useCapture });

			return 1;
		},
	
		wait: function(id, fn, time, showError) {
			var pollTime = 20;
			if (isDefined(time) && time != 0) {
				if (document.getElementById(id)) khanh.event.runJS(fn);
				else {
					setTimeout("khanh.event.wait('" + id + "', '" + fn + "', " +
					(time > 0 ?
						(time > pollTime ? time - pollTime : 0)
					:	(time < 0 && khanh.event.domLoaded ?
						(Math.abs(time) > pollTime ?
							Math.abs(time) - pollTime
						:	0)
					:	time)) +
					", " + showError + ")", pollTime);
				}
			} else if (!isDefined(time)) {
				if (document.getElementById(id)) khanh.event.runJS(fn);
				else setTimeout("khanh.event.wait('" + id + "', '" + fn + "')", pollTime);
			} else if (showError) alert(fn + ' timed out.\n[' + id + '] not found!')
		},

		runJS: function(jsText) {
			if (window.execScript)
				window.execScript(jsText);
			else if (eval.call)
				eval.call(window, jsText);
		},
	
		remove: function(obj, type, fn, useCapture) {
			if (obj.removeEventListener) obj.removeEventListener(type, fn, useCapture);
			else if (obj.detachEvent) obj.detachEvent('on' + type, fn);
			else return 0;
	
			// Event references will all be removed on page unload
			return 1;
		},
	
		src: function(e) {
			e = e || window.event;
			var elm;
			if (e.target)
				elm = e.target.nodeType == 3 ?
						e.target.parentNode
					:	e.target;
			else elm = e.srcElement;
			return elm;
		},
	
		initDomload: function() {
			if (document.addEventListener) // for Mozilla browsers
				document.addEventListener('DOMContentLoaded', this.ondomload, 0);
			else if (khanh.core.browser('msie')) { // for Internet Explorer
				document.write('<script id="__ie_onload" defer src=javascript:void(0)></script>');
				document.getElementById('__ie_onload').onreadystatechange =
					function() {
						if (this.readyState == 'complete')
							khanh.event.ondomload();
					};
			} else if (khanh.core.browser('WebKit')) { // for Safari
				var _timer = setInterval(
					function() {
						if (/loaded|complete/.test(document.readyState)) {
							clearInterval(_timer);
							khanh.event.ondomload();
						}
					}, 10);
			}

			this.add(window, 'load', this.ondomload);
		},
	
		ondomload: function() {
			if(khanh.event.domLoaded) return;
			khanh.event.domLoaded = 1;

			for (var i = 0; i < khanh.event.domloadEvents.length; i++)
				khanh.event.domloadEvents[i]();
		}
	};

	khanh.event.initDomload();
}
