cli.lib.namespace("cli.gui");

cli.gui.inputel = false;
cli.gui.outputel = false;
cli.gui.promptel = false;

cli.gui.inputfield = false;
cli.gui.bodyel = false;

cli.gui.el = function (id) {
	return document.getElementById(id);
}

cli.gui.init = function () {

	cli.gui.inputel = document.getElementById('input');
	cli.gui.outputel = document.getElementById('output');
	cli.gui.promptel = document.getElementById('prompt');
	cli.gui.inputfield = document.getElementById('inputfield');
	cli.gui.bodyel = document.getElementById('body');

	if (cli.gui.inputfield.createTextRange) {
		cli.gui.inputfield.onkeyup = new Function("return cli.keyboard.mcursor(event);");
		cli.gui.bodyel.onfocus = new Function("return cli.gui.focusinput(event);");
		cli.gui.bodyel.onclick = new Function("return cli.gui.focusinput(event);");
		cli.gui.bodyel.onkeydown = new Function("return cli.keyboard.keyDownHandler(event);");
	} else {
		cli.gui.inputfield.onkeyup = cli.keyboard.mcursor;
		cli.gui.bodyel.onfocus = cli.gui.focusinput;
		cli.gui.bodyel.onclick = cli.gui.focusinput;
		cli.gui.bodyel.onkeydown = cli.keyboard.keyDownHandler;
	}

}

// input / output functions
cli.gui.error = function (text) {
	cli.ajax.stopall();

	cli.gui.out("Error: " + text + "<br/> <br/>");
	cli.gui.showinput();
	cli.gui.focusinput();
	cli.gui.scroll();
}

cli.gui.outln = function (text) {
	cli.gui.out(text + "<br/>");
}

cli.gui.out = function (text) {
	//cli.gui.outputel.innerHTML += text;  
	var div = document.createElement("div");
	div.innerHTML = text;
	cli.gui.outputel.appendChild(div);

}

cli.gui.less = function (text) {
	return "<span class='less'>" + text + "</span>";
}

cli.gui.info = function (text) {
	return "<span class='info'>" + text + "</span>";
}

cli.gui.clear = function () {
	cli.gui.outputel.innerHTML = "";
}

cli.gui.showinput = function () {
	cli.gui.inputel.style['display'] = 'block';
}

cli.gui.hideinput = function () {
	cli.gui.inputel.style['display'] = 'none';
}

cli.gui.focusinput = function () {
	var txt = "";
	//if (document.getSelection) txt = document.getSelection();
	if (document.selection) txt = document.selection.createRange().text;
	else if (window.getSelection) txt = window.getSelection().toString();

	if (txt.length == 0) {

		document.f.q.value = document.f.q.value; // for safari
		if (cli.gui.inputel.style['display'] != 'none') document.f.q.focus();
	}
}

cli.gui.updateprompt = function () {
	cli.gui.prompt = cli.config.user + "@" + cli.config.host + ":/" + cli.config.mode + cli.config.pend;
	cli.gui.promptel.innerHTML = cli.gui.prompt;
}

cli.gui.scroll = function () {
	window.scrollBy(0, 122500);
}

cli.gui.setstyle = function (ele, prop, val) {
	try {
		var el = cli.gui.el(ele);
		el.style[prop] = val;
		return true;
	}
	catch(e) {
		return false;
	}

}

cli.gui.setstyleclass = function (classname, style) {
	//cli.gui.outputel.innerHTML += text;  
	var div = document.createElement("div");
	var out = "<br style='line-height:0px;'/><style>" + classname + " {" + style + "}</style>";
	div.innerHTML = out;
	cli.gui.bodyel.appendChild(div);
}
