OnLoad = function () {
	this.functionsToRun = new Array;
	this.argumentsToPass = new Array;
	this.context = new Array;
	
	this.hasRun = false;
	
	/**
	 * @param Function functionToCall	the function to be called after DOM was initialized
	 * @param Object context			the context from which the supplied function shall be called. if not supplied or null, the context will be the window object
	 * @param [args...]					any number of arguments which will be supplied to the called function
	 */
	this.add = function () {
		if (arguments.length < 1) {
			throw "OnLoad.add: this method requires at least 1 argument";
		}
		
		if (!(arguments[0] instanceof Function)) {
			throw "OnLoad.add: the first argument must be a function";
		}
		
		this.functionsToRun.push(arguments[0]);
		
		if (arguments.length >= 2) {
			this.context[this.functionsToRun.length-1] = arguments[1];
		}
		
		
		var args = new Array;
		var j = 0;
		
		for (var i=2; i<arguments.length; i++) {
			args[j++] = arguments[i];
		}
		
		this.argumentsToPass[this.functionsToRun.length-1] = args;
	};
	
	this.run = function () {
		if (this.hasRun) {
			return;
		}
		
		var func;
		var cont;
		var callString;
		
		for (var i=0; i<this.functionsToRun.length; i++) {
			func = this.functionsToRun[i];
			cont = this.context[i] ? this.context[i] : window;
			callString = "func.call(cont";
			
			for (var j=0; j<this.argumentsToPass[i].length; j++) {
				callString+= ", this.argumentsToPass["+i+"]["+j+"]";
			}
			
			callString+= ")";
			eval(callString);
		}
		
		this.hasRun = true;
	};
	
	
	$(document).one("ready", function () {
		OnLoad.getInstance().run();
	});
};


OnLoad.instance = null;

OnLoad.getInstance = function () {
	if (!OnLoad.instance) {
		OnLoad.instance = new OnLoad();
	}
	
	return OnLoad.instance;
};
