var wterm = {}
wterm.release = "0.0";
wterm.releaseDate = "dev-now";
wterm.name = "WTerm";
wterm.version = wterm.name + wterm.release + wterm.releaseDate;
wterm.banner = wterm.name + ' ' + wterm.release + ' ' +  document.location.host ;
wterm.homepage= "http://wterm.saverpigeeks.com/";

wterm.autoload = Array(
	'term_core.js',		// core wterm framework
	'term_shell.js',	// default wterm shell
	'term_builtin.js',	// built-in commands
	'term_utils.js',	// additional utility commands
	'term_info.js',		// informational commands
	'term_test.js'
	);

wterm.components = Array(); // loaded components

var getAjax = function() { return new XMLHttpRequest(); }
var geval = function(src) 
	{ 
		if(window.execScript) { window.execScript(src); return; }
		window.eval.call(window, src);
	}
var isdefined = function (o) { return (typeof(window[o]) != 'undefined'); }
var isint = function (n) 
	{ 
		var nx = parseInt(n); 
		return ( (nx==n) && (n.toString()==nx.toString()) ); 
	}
var toggleDebug = function() 
	{ 
		var d = $('debug');
		if(!d) return;
		if(d.style.display=='none') d.style.display='block';
		else d.style.display='none';
	};
wterm.debug = function(txt)
	{
		var d = $('debug');
		try { if(d) d.innerHTML = txt; } catch(e) {}
		if(isdefined('console') && console) console.log(txt);
	}

wterm.fetch = function(scriptpath)
	{
		var ajax = getAjax();
		ajax.open('GET', scriptpath, false);
		ajax.send('');
		return ajax.responseText;
	}
wterm.loadScript = function(scriptpath)
	{
		wterm.debug('Loading: ' + scriptpath);
		var body = wterm.fetch(scriptpath);
		geval(body);
		wterm.components.push(scriptpath);
		wterm.debug('Finished: ' + scriptpath);
	}

var $ = function(id) { return document.getElementById(id); };

var initTerminal = function()
	{
	wterm.autoload.forEach(function(f)
		{
			wterm.loadScript(f);
		});
	wterm.initTerminal();
	wterm.onload.execute();
	}

wterm.onload = new function () 
	{ 
		var methods = Array();
		var ran = false;
		this.add = function(method) { 
			// If we've already loaded, just run
			if(ran) method();
			else methods.push(method); 
		};
		this.execute = function () { 
			methods.forEach(function(f) 
				{ f(); });
			ran = true;
			methods = Array();
			};
	}

if(document.addEventListener)
	document.addEventListener("DOMContentLoaded", initTerminal, false);
else document.onload = initTerminal;
