var Fire = Class.create();
Fire.prototype = {

	timer: null,
	loaded: false,
	
	onload: function() {},

	initialize: function() {
		if(arguments[0]) { this.add(arguments[0]); }
	},
		
	start: function() {
		var spark = this.spark.bind(this);
		if(document.addEventListener) { document.addEventListener("DOMContentLoaded", spark, null); }		
		if(Prototype.Browser.IE) {
			document.write('<script id="__ie_onload" defer="true" src="javascript:void(0);"></script>');
			// 	document.getElementById("__ie_onload").onreadystatechange = function() {
			// 		if (/loaded|complete/.test(this.readyState)) { spark(); }
			// 	};
			var ie_onload = document.getElementById("__ie_onload");
			this.timer = setInterval(function() { if (/loaded|complete/.test(ie_onload.readyState)) { spark(); } }, 10);			
		}
    	if(/WebKit/i.test(navigator.userAgent)) { // sniff for Safari
    		this.timer = setInterval(function() { if (/loaded|complete/.test(document.readyState)) { spark(); } }, 10);
    	}
		Event.observe(window, 'load', spark);
	},
	
	spark: function() {
		if(typeof this.onload == 'function' && !this.loaded) { this.onload(); this.reset(); }
	},
	
	reset: function() {
		if(!this.loaded) {
			if(this.timer) { clearInterval(this.timer); this.timer = null; }
			var spark = this.spark.bind(this);
			if(document.removeEventListener) { document.removeEventListener("DOMContentLoaded", spark, null); }
			this.onload = function() {};
			this.loaded = true;
		}
	},

	add: function(func){
		var oldonload = this.onload;
		if(typeof this.onload != 'function') {
			this.onload = func;
		} else {
			this.onload = function() { oldonload(); func(); }
		}
	}

}
