var MooTerminal = new Class({
	
	//implements
	Implements: [Options],

	//options
	options: {
		backgroundColor: '#000',
		foreColor: '#6F6',
		font: 'courier',
		containerElement: 'body',
		fontSize:"12"
	},
	
	//initialization
	initialize: function(options) {
		// set options
		this.setOptions(options);
		
		// Set the container
		var containerElem = this._getContainer();
		
		// Apply Colors
		containerElem.setStyle('background-color', this.options.backgroundColor);
		containerElem.setStyle('color', this.options.foreColor);
		containerElem.setStyle('font-family', this.options.font);
		containerElem.setStyle('font-size', this.options.fontSize + "px");
		
		
		this._initializeOutputBuffer();
		
		this._initializeCommandLine();
		this._commandLineFocus();
		
		var self = this;
		document.getElement('html').addEvent("click", function(){
			self._commandLineFocus();
		});
		
	},
	
	//a method that does whatever you want
	_puts: function(message) {
		new Element("p", {
			html: message,
			styles: {
				margin: "2px"
			}
		}).inject(this._getOutputBuffer());
	},
	
	_putsLoginNotice: function () {
		
		this._getOutputBuffer().set('html', '');
		this._puts("Guest logged in on " + new Date());
		this._puts("type <code>help</code> for a list of the available commands");
	},
	
	_initializeOutputBuffer:function(){
		new Element ("div", {
			id: 'outputBuffer',
			html: 'Loading..'
		}).inject(this._getContainer());
		
		this._putsLoginNotice();
	},
	
	_initializeCommandLine:function (){
		
		var self = this;
		
		var commandLinePrompt = new Element('p', {
			html: "<span>$>&nbsp;</span>",
			styles: {
				margin: "2px"
			}
		});
		
		new Element('input', {
			type: 'text',
			id: 'termCommandLine',
			styles: {
				width: 'auto',
				'font-size': this.options.fontSize + "px",
				background: "none",
				color: this.options.foreColor,
				border: "none",
				'font-family': this.options.font
			},
			events: {
				'keypress': function(e) {
					
					if (e.key == 'enter') {
						self._puts("<span>$>&nbsp;</span>" + $('termCommandLine').value);
						self._handleCommand($('termCommandLine').value);
						$('termCommandLine').set('value',''); 
					}
				}
			}
		}).inject(commandLinePrompt);
		
		commandLinePrompt.inject(this._getContainer());
	},
	
	_handleCommand: function(command) {
		if (command == 'ls') {
			var output = "<pre>drwx------+ 52 eladmeidar  eladmeidar     1768 Jan 15 22:21 Desktop <br/>\
drwx------+ 29 eladmeidar  eladmeidar      986 Jan 15 10:01 documents <br/>\
drwx------+ 14 eladmeidar  eladmeidar      476 Jul  3  2009 projects <br/>\
drwxr-xr-x+  4 eladmeidar  eladmeidar      136 Sep  4  2008 public <br/>\
drwxr-xr-x+  6 eladmeidar  eladmeidar      204 Feb  5  2009 sites <br/>\
</pre>"
			
			this._puts(output);
		} else {
			this._puts("OS: '" + command + "': command was not found.");
		}
	},
	
	_commandLineFocus: function() {
		$('termCommandLine').focus();
	},
	_getContainer: function() {
		return document.getElement(this.options.containerElement);
	},
	
	_getOutputBuffer: function() {
		return $('outputBuffer');
	}
	
});