var ModuleManager = Class.create({

	initialize: function() {
		this.moduleLoadCalls = [];
		this.moduleLoadOnLoadCalls = [];
		this.loaded = false;
		this.loadedCallBacks = false;
		this.loadedOnLoadCallBacks = false;
	},
	
	// Use this function for day-to-day use
	registerModuleLoad: function(callback) {
		if (!callback) throw new Error("You must supply a callback to run for [registerModuleLoad]");
		if (typeof(callback) != "function") throw new Error("You must supply a valid function for the [registerModuleLoad] callback");
		this.moduleLoadCalls[this.moduleLoadCalls.length] = callback;
	},
	
	// Use this function ONLY for things like FCKEditor which need the complete DOM available
	registerModuleLoadOnLoad: function(callback) {
		if (!callback) throw new Error("You must supply a callback to run for [registerModuleLoadOnLoad]");
		if (typeof(callback) !== "function") throw new Error("You must supply a valid function for the [registerModuleLoadOnLoad] callback");
		this.moduleLoadOnLoadCalls[this.moduleLoadOnLoadCalls.length] = callback;
	},

	loadModules: function() {
		for (var loop=0; loop<moduleManager.moduleLoadCalls.length; loop++) {
            moduleManager.moduleLoadCalls[loop]();
		}
		moduleManager.loadedCallBacks = true;
		moduleManager.loaded = moduleManager.loadedCallBacks && moduleManager.loadedOnLoadCallBacks;
	},
    
    loadModulesOnLoad: function() {
		for (var loop=0; loop<moduleManager.moduleLoadOnLoadCalls.length; loop++) {
			moduleManager.moduleLoadOnLoadCalls[loop]();
		}
		moduleManager.loadedOnLoadCallBacks = true;
		moduleManager.loaded = moduleManager.loadedCallBacks && moduleManager.loadedOnLoadCallBacks;
	}
});

var moduleManager = new ModuleManager();

Event.observe(document, 'dom:loaded', moduleManager.loadModules);
Event.observe(window, 'load', moduleManager.loadModulesOnLoad);
