if(document.all){document.write("<STYLE TYPE='text/css'>\n#MENU, #Prompt {position: absolute; }\n</STYLE>");}

var bodyFarRight;
var bodyFarBottom;

function initialize(){
	getBodyDim();
	if(document.all){
		bodyFarBottom += 16;
	}

	var elDim = getDimentions('Prompt');
	xPosition('Prompt', 0, (bodyFarBottom - elDim[3]));
	document.getElementById('Prompt').style.visibility = 'visible';

	xPosition('MENU', 0, 0);
	
	keepFixed();
}

function keepFixed(){
	emuFixedPos('bdy', 'MENU', 0, 0);

	var elDim = getDimentions('Prompt');
	emuFixedPos('bdy', 'Prompt', 0, (bodyFarBottom - elDim[3]));
}

function emuFixedPos(bodyid, id, xpos, ypos){
	if(document.all){
		var fixedElDim = getDimentions(id);
		scrollOffsetY = document.getElementById(bodyid).scrollTop;
		xPosition(id, xpos, (ypos + scrollOffsetY));
//		alert(bodyFarBottom - fixedElDim[3]);
//		xPosition(id, 0, 0);
	}
}

function getBodyDim(bId){
	inw = document.body.clientWidth;
	inh = document.body.clientHeight;
	outw = document.body.offsetWidth;
	outh = document.body.offsetHeight;
	if(window.innerWidth){
		inw = window.innerWidth;
		inh = window.innerHeight;
	}
	if(window.outerWidth){
		outw = window.outerWidth;
		outh = window.outerHeight;
	}

//	var bodyDimentions = getDimentions(bId);
//	if(document.all){
//		if(document.body.scroll != "no"){bodyDimentions[2] -= 21;}
//	}
//	bodyFarRight = bodyDimentions[2];
	bodyFarRight = inw;
	bodyFarBottom = inh;
	if(typeof(document.getElementById('bdy').style.MozOpacity) != "undefined"){
		bodyFarRight = bodyFarRight - 	15;
//		bodyFarBottom = bodyFarBottom - 	15;
	}
	var currentpos = bodyFarRight;
//	alert('Body Width: '+currentpos);
//	alert('Window Inner Width: '+inw+'\nWindow Outer Width: '+outw);
}

function getDimentions(mId){
	obj = document.getElementById(mId);
//	alert(mId);
	var dimentions = new Array(obj.offsetLeft, obj.offsetTop, obj.offsetWidth, obj.offsetHeight);
	return dimentions;
}

function xPosition(id, x, y){
	var obj = document.getElementById(id);
	obj.style.left = x;
	obj.style.top = y;
}

function xWidth(id, x){
//	alert(x);
	var obj = document.getElementById(id);
	obj.style.width = x;
}

//Pseudo OS

var blinkStat = 1;
function blinker(obj){
	var isVisible = '';
	if(blinkStat == 1){
		isVisible = 'visible';
		blinkStat = 0;
	}else{
		isVisible = 'hidden';
		blinkStat = 1;
	}
	document.getElementById(obj).style.visibility = isVisible;
	setTimeout("blinker('"+obj+"')", 500);
}

var url = window.location;
if(document.layers){alert('your browser is too old\nplease upgrade it');}

function getKey(keyCode){
	keyValidate(event.keyCode);
}
function getKeyM(e){
	keyValidate(e.which);
}

function keyValidate(code){
//	alert(code);
	var keyString = String.fromCharCode(code).toLowerCase();
	var query = document.getElementById("QUERY").innerHTML;
	if(code == 27){
		document.getElementById("QUERY").innerHTML = '';
	}else	if((code >= 112 && code <= 123) || (code >= 33 && code <= 40) || code == 46){
		return true;
	}else	if(code == 190){
		document.getElementById("QUERY").innerHTML += '.';
	}else	if(code == 191){
		document.getElementById("QUERY").innerHTML += '/';
	}else	if(code == 188){
		document.getElementById("QUERY").innerHTML += ',';
	}else	if(code == 59){
		document.getElementById("QUERY").innerHTML += ';';
	}else	if(code == 222){
		document.getElementById("QUERY").innerHTML += "'";
	}else	if(code == 219){
		document.getElementById("QUERY").innerHTML += '[';
	}else	if(code == 221){
		document.getElementById("QUERY").innerHTML += "]";
	}else	if(code == 220){
		document.getElementById("QUERY").innerHTML += '\\';
	}else	if(code == 192){
		document.getElementById("QUERY").innerHTML += "`";
	}else	if(code == 109){
		document.getElementById("QUERY").innerHTML += "-";
	}else	if(code == 8){
		document.getElementById("QUERY").innerHTML = query.substring(0, (query.length - 1));
//		window.location = url;
		document.onkeypress = cancel;
	}else	if((code >= 16 && code <= 18) || code == 91){
		return true;
	}else	if(code == 13){
		executeCmd(query);
		document.getElementById("QUERY").innerHTML = '';
		return false;
	}else{
		document.getElementById("QUERY").innerHTML += keyString;
	}
}
document.onkeydown = (document.all)?(getKey):(getKeyM);
function cancel(){
	return false;
}


function executeCmd(cmd){
	cmd = cmd.toLowerCase();
	var spos = cmd.indexOf(' ');
	if(spos == -1)
		spos = cmd.length;
	var opt = cmd.substring((spos + 1), cmd.length);
	cmd = cmd.substring(0, spos);
	cmd = cmd.replace(/^\s+/, "");
	cmd = cmd.replace(/\s+$/, "");
	opt = opt.replace(/^\s+/, "");
	opt = opt.replace(/\s+$/, "");
//	alert("("+cmd+")");
//	alert("("+opt+")");

	document.getElementById("CONTENT").innerHTML += "<BR>\n<B STYLE='line-height: 3em; color: silver; ' CLASS=CMD>shell# ./&nbsp;</B> <SPAN CLASS=CMD>"+cmd+"</SPAN><BR>\n";
	var result = '';
	

	if(cmd == 'help'){
		result = "<EM>reload, refresh, cls, clr, clear, reboot, restart</EM> = Refresh the page<BR>\n";
		result += "<EM>c, connect, open, load, man</EM> = load [page name], if extention is omitted, .html is appended by default<BR>\n";
		result += "<EM>help</EM> = displays this help<BR>\n";
		result += "<EM>load help</EM> = if you want to open help page<BR>\n";
	}else if(cmd == 'reload' || cmd == 'refresh' || cmd == 'reboot' || cmd == 'restart' || cmd == 'cls' || cmd == 'clr' || cmd == 'clear'){
		window.location = window.location;
	}else if(cmd == 'c' || cmd == 'connect' || cmd == 'open' || cmd == 'load'){
		var url = opt;
		url = (opt.indexOf('.') == -1)?(opt+".html"):(opt);
		
		replaceCmd(url);
		window.location = url;
	}else{
		result = "<SPAN CLASS=CMD>'"+cmd+"' is an unknown command, please try 'help' for a list of existing commands</SPAN>";
	}

	document.getElementById("CONTENT").innerHTML += result;
	var bodyHt = document.body.scrollHeight;
	scrollTo(bodyHt, bodyHt);
}

function replaceCmd(msg){
	document.getElementById("QUERY").innerHTML = msg;
	document.getElementById("MESSAGE").style.display = 'block';
	document.getElementById("BlinkMessage").innerHTML = "OPENING<BR>\n"+msg;

	var elDim = getDimentions('MESSAGE');
	xPosition('MESSAGE', (bodyFarRight - 450) / 2, elDim[1]);
	xWidth('MESSAGE', 450);
	xWidth('SUBMESSAGE', 420);
}

function cancelCmd(msg, proceed){
//	alert(msg);
	(proceed != 1)?(setTimeout("cancelCmd('"+msg+"', 1)", 3000)):(document.getElementById("MESSAGE").style.display = 'none');
}