var Engine = {
	
	loadedModules: new Array(),
	modules: new Array(),
	uri: "/",
	
	bootstrap: function(){
		/* For debugging only */
		dojo.io.XMLHTTPTransport.preventCache = true;
		/*---*/
		this.setStatus("Page Loading...");
		dojo.require("dojo.io.*");
		dojo.hostenv.writeIncludes();
		setTimeout("Engine.hideStatus()",5000);
		if((""+document.location).indexOf('#') < 0){
			Engine.loadPage("#Page/Welcome");
		}else{
			Engine.loadPage("" + document.location);
		}
	},
	
	setStatus: function(message){
		if($('status') != null){
			$('status').parentNode.removeChild($('status'));
		}		
		var body = document.getElementsByTagName("body")[0];
		var div = document.createElement("div");
		div.style.position = "absolute";
		div.style.top = "50%";
		div.style.left = "50%";
		div.style.width = "200px";
		div.style.margin = "-12px 0 0 -100px";
		div.style.border = "0px";
		div.style.padding = "20px";
		div.style.opacity = "0.85";
		div.style.backgroundColor = "#B5E2FE";
		div.style.border = "1px solid #000000";
		div.style.color = "#000000";
		div.style.fontSize = "25px";
		div.style.textAlign = "center";
		div.id = 'status';
		body.appendChild(div);
		div.innerHTML = message;
	},
	
	hideStatus: function(){
		Engine.opacityDown($('status'));
	},
	
	opacityDown: function(theElement){
		if(theElement == null)
			return;
		var opacity = parseFloat(theElement.style.opacity);
		if (opacity < 0.08){
			theElement.parentNode.removeChild(theElement);
		}else{
			opacity -= 0.07;
			theElement.style.opacity = opacity;
			setTimeout(function(){Engine.opacityDown(theElement);}, 50);
		}
		return true;
	},
	
	loadPage: function(url){
		Engine.setStatus("Page Loading...");
		if(!url)
			url = "" + document.location;
		var hashIndex = url.indexOf('#');
		if(hashIndex < 0 || hashIndex >= url.length-2)
			return Engine.hideStatus;
		uri = url.substring(hashIndex + 1);
		var moduleLength;
		if(uri.indexOf('/') > 0)
			moduleLength = uri.indexOf('/');
		else
			moduleLength = uri.length;
		var module = uri.substring(0,moduleLength);
		uri = uri.substring(uri.indexOf('/'));
		if(Engine.loadedModules[module] && ! dojo.lang.isUndefined(Engine.modules[module])){
			Engine.modules[module].execute(uri);
		}else{
			Engine.loadModule(module);
		}
	},
	
	loadModule: function(module){
		if(Engine.loadedModules[module])
			return;
		dojo.io.bind({
			url: "javascript/" + module + ".js",
			sync: true,
			error: function(type, evaldObj){Engine.showError("Error while loading module.");},
			load: function(type, data, evt){eval(data);Engine.modules[module].execute(uri);}
		});
	},
	
	loadScript: function(file) {
		if (typeof(window["" + file]) != "undefined") { // Already exists
			return;
		}
		var head = document.getElementsByTagName("head")[0];
		script = document.createElement('script');
		script.id = file;
		script.type = 'text/javascript';
		script.src = "javascript/" + file;
		head.appendChild(script)
	},
	
	setContent: function(content){
		$('content').innerHTML = content;
	},
	
	showError: function(message){
		Engine.setStatus(message);
		setTimeout("Engine.hideStatus()",10000);
	}
}


function $() {
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);
    if (arguments.length == 1)
      return element;
    elements.push(element);
  }
  return elements;
}
