n3rd = {
	commandHistory : [],
	commandFuture: [],

	url: 'post.php',

	// ugly but quite handy
	rootUrl: 'http://www.n3rddispuut.nl',
	siteUrl: 'http://www.n3rddispuut.nl',

	init: function(){
		if(!$('command')==null) return;

		Event.observe($('command'), 'keyup', n3rd.readCommand);
		new Ajax.Request(
			n3rd.url,
			{
                                method: 'post', 
                                parameters: 'keycode=13&cmd=' + encodeURIComponent('cat /public/home'),
                                onComplete: n3rd.parseResponse
                        } 
                    );
		$('command').focus();
	},

	readCommand: function(e){
		if(e.keyCode==13){
			cmd = $('command').value;
			n3rd.commandHistory.push(cmd);
			if(cmd=='clear' || cmd=='cls'){
				$('consoletext').innerHTML="";
				$('command').value = "";
				return;
			}
			if(cmd!=""){
				$('command').value = "";
				new Ajax.Request(
					n3rd.url,
					{
						method: 'post', 
						parameters: 'keycode=' + e.keyCode + '&cmd=' + encodeURIComponent(cmd),
						onComplete: n3rd.parseResponse
					}
				);
			}
		} else if(e.keyCode==38){
			if(c = n3rd.commandHistory.pop()){
				$('command').value = c;
				n3rd.commandFuture.push(c);
			}
		} else if(e.keyCode==38){
		} else if(e.keyCode==27){
			$('command').value = '';
		}
	},

	parseResponse: function(r){
	n3rd.writeOutput(r.responseText);
	},

	writeOutput: function(str){
		new Insertion.Bottom('consoletext', '<div>' + str + '</div>');	
		var links = $('consoletext').getElementsByTagName('a');
		for(var i=0; i < links.length; i++){
			link = links[i];
			if(
			    link.className=="file" || 
			    link.className=="directory"
			){
				// make sure we don't observe things twice or more
				Event.stopObserving(link, 'click', n3rd.linkClick);
				Event.observe(link, 'click', n3rd.linkClick);
			}
		}
        window.setTimeout(n3rd.scrolltoBottom, 0);
	},

	scrolltoBottom: function(){
		$('footer').focus();
		$('command').focus();
	        if($('consoletext').scrollTop){
                    $('consoletext').scrollTop = '13371337';
                } else if($('consoletext').scrollTo){
                    $('consoletext').scrollTo(0, 13371337);
                }
	},

	linkClick: function(e){
		var el = Event.element(e);
		var fsname = el.href.replace(n3rd.siteUrl, '');
		fsname = fsname.replace(n3rd.rootUrl, '');

		if(el.className=="directory"){
			$('command').value = "cd " + fsname + ";ls";
		} else if(el.className=="file"){
			$('command').value = "cat " + fsname;
		}

		cmd = $('command').value;
		n3rd.commandHistory.push(cmd);
		if(cmd=='clear' || cmd=='cls'){
			$('consoletext').innerHTML="";
			$('command').value = "";
			return;
		}
		if(cmd!=""){
			$('command').value = "";
			new Ajax.Request(
				n3rd.url,
				{
					method: 'post', 
					parameters: 'keycode=13&cmd=' + encodeURIComponent(cmd),
					onComplete: n3rd.parseResponse
				}
			);
		}

		Event.stop(e);
	},

    save: function(filename, content){
			new Ajax.Request(
				n3rd.url,
				{
					method: 'post', 
					parameters: '',
                    postBody: 'keycode=13&cmd=save'
                            + '&filename=' 
                            +  escape(filename)
                            + '&content=' 
                            + escape(content),
					onComplete: n3rd.parseResponse
				}
			);
    }
}

Event.observe(window, 'load', n3rd.init);

