function wCore(parameters) {
	console.log ('wCore: Phase 0 - Construction');
	this.setup = parameters;
}

/**
 * Loads a distant JS file.
 * 
 * @param file
 *            URL of file to load
 */
wCore.prototype.include = function (file, onloaded) {
	
	var that = this;
	
	this.includedFiles = (this.includedFiles || []);
	
	var alreadyLoaded = !!this.includedFiles[file];
	

		
	if (file.substr (file.length-3, 3)  == '.js') {
		if (alreadyLoaded) {
			return;	// No retrigger
		}
		var script = document.createElement('script');
		var scripts = document.getElementsByTagName('script')[0].parentNode;
		
		script.type = "text/javascript";
		script.async = true; 
		script.src = file;		
	} else {
		if (alreadyLoaded) {
			if (onloaded != undefined) {
				onloaded();
				return;
			}
		}
		var script = document.createElement ('link');
		var scripts = document.getElementsByTagName('head')[0];
		
		script.async = true; 
		script.type="text/css";
		script.rel="stylesheet";
		script.href = file;
		script.media = "screen";
	}
	
	this.includedFiles[file] = false;

	script.onload = function() {
		if (!that.includedFiles[file]) {
			that.includedFiles[file] = true;
			onloaded();
		}
	}
	script.onreadystatechange = function (event) {
		if (this.readyState == 'loaded' || this.readyState == 'complete') {
			if (!that.includedFiles[file]) {
				that.includedFiles[file] = true;
				onloaded ();				
			}
		} else {
			console.log ('State change: ', this.readyState);			
		}
	};
		
	
//	scripts.appendChild (script);
	scripts.insertBefore(script, scripts.firstChild);
}

wCore.prototype.boot = function () {
	
	var that = this;
	console.log ('wCore: Phase 1 - Booting');
	
	if (typeof ($) == 'undefined') {
		
		// jQuery not loaded, get it
		this.include ('/Support/jQuery/jquery-min.js', function(event) {
						
			// Register initialisation function once jquery is available
			$(document).ready(function() {
				
				if (typeof ($) !== 'undefined') {
					console.log ('wCore: Phase 2 - Got jQuery');
					that.loadComponents ();			
				} else {
					throw new String ('Failed to load jQuery. Cannot continue.');
				}
				
			});
		});		
	} else {
		console.log ('wCore: Phase 2 - Already got jQuery');
		this.loadComponents ();	
		
	}

	
}

wCore.prototype.initDocument = function() {
	console.log ('wCore: Phase 4 - Initializing webpage');
	
	
	this.UI.initAjaxLinks ("a.Ajax");
	if (this.Edit)
		this.Edit.initEditors();
};

wCore.prototype.checkRequiredComponents = function () {
	var hasAllDependencies = true;
	
	for (var component in this.setup.components) {
		if (this.setup.components[component]) {
			// Is a required component, check it exists in our properties
			if (this[component] === undefined) {
				hasAllDependencies = false;
				break;
			}
		}
	}
	
	if (hasAllDependencies) {
		console.log ('wCore: Phase 3 - Got all required modules');

		var tmpCore = this;
		$(document).ready (
			function () {
			tmpCore.initDocument ();
			});
				
	} else {
	}	
}

wCore.prototype.allocateComponent = function (cname) {
	
	try {
		var x = 'this.' + cname + '=new w' + cname.substr (0,1).toUpperCase() + cname.substr (1, cname.length) + '(this)';
		eval (x);
	} catch (e) {
		
		if (this.setup.components[cname] !== undefined && this.setup.components[cname] === true) {
			throw new String ('Failed to get required component: ' + cname + ' (' + e + ')');
		} else {
// console.error ('Failed to get component: ' + cname + ' (' + e + ')');
		}	
	};

}

wCore.prototype.loadComponent = function(name) {
	var that = this;
	
	var resource = '/Support/iCore/JS/Core.' + name + '.js';
	var cname = 'w' + name.substr (0,1).toUpperCase() + name.substr (1, name.length);
		
	if (eval ('typeof(' + cname + ')') == "undefined") {
// console.log ('Component ' + name + ' not yet loaded.');
		this.include (resource, function(event) {
			that.allocateComponent (name);	
			that.checkRequiredComponents ();			
		});
	} else {
// console.log ('Component ' + name + ' already loaded. Reusing');
		this.allocateComponent (name);
		this.checkRequiredComponents ();
		
	}
	
}

wCore.prototype.loadComponents = function () {
	for (var component in this.setup.components) {
		if (this.setup.components[component])
			this.loadComponent (component);
	}
}


Core = new wCore ( {
	'components' : {
		'tools':			true,
		'network':			true,
		'UI':				true,
		'InlineGallery': 	true,
	}
});

Core.boot ();

