var Engine = {
	init: function(){
		Engine.log("Initing Engine.");
		if((""+document.location).indexOf('#') < 0){
			document.location += "#Page/Welcome";
			Engine.loadPage("#Page/Welcome");
		}else{
			Engine.loadPage("" + document.location);
		}
		
	},
	debug: false,
	
	console: null,
	
	log: function(message){
		if(!Engine.debug)
			return;
		if(!Engine.console){
			// Log window does not exist, try to open it.
			consoleWnd = window.open('','Debug Information','width=600,height=150,menubar=0,toolbar=0,status=0,scrollbars=1,location=0,resizable=1');
			consoleWnd.document.writeln('<html><head><title>Console</title></head><body bgcolor=white></body></html>');
			Engine.console = consoleWnd.document.body;
		}
		Engine.console.innerHTML += (message + "<br/>\n");
	},
	
	/************************************************************************
	 * Module handling
	 ************************************************************************/
	modules: new Array(),
	
	loadModule: function(module){
		if(Engine.modules[module])
			return;
		Engine.log("Module " + module + " not yet loaded. Loading now...");
		var myAjax = new Ajax.Request("javascript/" + module + ".module.js",{
			method: 'get', 
			parameters: "?nocache=" + new Date(), 
			onFailure: function(){
				Engine.log("Could not load module " + module);
				Engine.showError("Could not load module " + module);
			},
			onSuccess: function(req){
				eval(req.responseText);
//				alert(req.responseText);
				Engine.modules[module].execute(uri);
			}
		});
	},
    	uri: "/",

    	loadPage: function(url){
    		Engine.setStatus("Loading...");
    		if(!url)
    			url = "" + document.location;
    		Engine.log("Loading " + url);
    		var hashIndex = url.indexOf('#');
    		if(hashIndex < 0 || hashIndex >= url.length-2)
    			return Engine.hideStatus;
    		uri = url.substring(hashIndex + 1);
    		document.location.hash = uri;
    		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.modules[module]){
    			Engine.modules[module].execute(uri);
    		}else{
    			Engine.loadModule(module);
    		}
    	},
	
	/************************************************************************
	 * Eye Candy
	 ************************************************************************/
	setStatus: function(message){
		if($('status') == null){
			var body = document.getElementsByTagName("body")[0];
			var div = document.createElement("div");
			div.id = 'status';
			body.appendChild(div);
		}
		var node = $('status');
		node.innerHTML = message;
		new Effect.Appear(node);
	},

	hideStatus: function(){
		new Effect.Fade($('status'));
	},
	
	showError: function(message){
		Engine.setStatus(message);
		Engine.setTimeout("Engine.hideStatus();",15000);
	},
	
	setContent: function(content){
		$('content').innerHTML = content;
	},
	setError: function(element){
		element.style.background = '#ffeeee';
	},
	setOK: function(element){
		element.style.background = "#eeffee";
	}
};
