//Shell Interface coded by Forrest Hatfield

$(document).ready(function() {
	var commandHistory		= new Array();
	var commandHistoryPos	= 0;

	$('html').keydown(function(e){
		var code = (e.keyCode ? e.keyCode : e.which);
		
		//enter
		if(code == 13){
			var currentCommand = $('#shell .command:last').text();

			commandHistory.push(currentCommand);
			commandHistoryPos = commandHistory.length;
			
			switch(currentCommand){
				case "clear":
					$('#shell').empty();
					break;
				case "contact":
					$('#shell').append(dataContact);
					break;
				case "date":
					var d = new Date();
					$('#shell').append('<div>' + d + '</div>');
					break;
				case "exit":
					$('#shell').empty();
					$('#shell').append('<div>Connected to <a href="http://forresthatfield.com">forresthatfield.com</a>.</div>');
					$('#shell').append('<div>Type \'help\' to see a list of commands.</div>');
					$('#shell').append('<div>&nbsp;</div>');
					break;
				case "help":
					$('#shell').append(dataHelp);
					break;
				case "ls":
					var postData = {};
					postData["command"]	= 'ls';
					$.ajax({
						type:		"POST",
						url:		"ajax.php",
						data:		(postData),
						async:		false,
						success:	function(xml){
										$(xml).find("element").each(function(){
											var dataFile = $(this).find("file").text();
											var dataDate = $(this).find("date").text();
											var dataSize = $(this).find("size").text();
											var dataType = $(this).find("type").text();
											if(dataType == "file"){
												$('#shell').append('<div><span class="fileSize">' + dataSize + '</span><span class="fileDate">' + dataDate + '</span><span class="fileName">' + dataFile + '</span></div>');
											}else if(dataType == "dir"){
												$('#shell').append('<div><span class="fileSize">' + dataSize + '</span><span class="fileDate">' + dataDate + '</span><span class="dirName">' + dataFile + '</span></div>');
											}
										});
									}
					});
					break;
				case "projects":
					$('#shell').append(dataProjects);
					break;
				case "resume":
					$('#shell').append(dataResume);
					break;
				case "skills":
					$('#shell').append(dataSkills);
					break;
				case "whois":
					$('#shell').append('<div>&nbsp;</div>');
					$('#shell').append(dataPic);
					$('#shell').append('<div>&nbsp;</div>');
					$('#shell').append(dataBio);
					$('#shell').append('<div>&nbsp;</div>');
					break;
				case "":
					break;
				default:
					$('#shell').append('<div><span class="response">-bash: ' + currentCommand + ': command not found</span></div>');
			}
			
			$('#shell .cursor').remove();
			$('#shell').append('<div><span class="prompt">[forresthatfield.com]#</span><span class="command"></span><span class="cursor"></span></div>');
			$('html, body').scrollTop($('#shell').height());
			
			e.preventDefault();
			return false;
		}
		
		//up arrow
		if(code == 38){
			if(commandHistoryPos){
				commandHistoryPos--;
				$('#shell .command:last').text(commandHistory[commandHistoryPos]);
			}
			e.preventDefault();
			return false;
		}
		
		//down arrow
		if(code == 40){
			if(commandHistoryPos < (commandHistory.length - 1)){
				commandHistoryPos++;
				$('#shell .command:last').text(commandHistory[commandHistoryPos]);
			}
			e.preventDefault();
			return false;
		}
		
		//backspace
		if(code == 8){
			$('#shell .command:last').text($('#shell .command:last').text().substring(0,$('#shell .command:last').text().length - 1));
			e.preventDefault();
			return false;
		}
	});
	
	$('html').keypress(function(e){
		var code = (e.keyCode ? e.keyCode : e.which);

		//space
		if(code == 32){
			$('#shell .command:last').append('&nbsp;');
			e.preventDefault();
			return false;
		}
		
		//letters/numbers
		if((code >= 48 && code <= 57) || (code >= 65 && code <= 90) || (code >= 97 && code <= 122)){
			$('#shell .command:last').append(String.fromCharCode(code));
			e.preventDefault();
			return false;
		}
		
		return;
	});

	$('#shell').empty();
	$('#shell').append('<div>Connected to <a href="http://forresthatfield.com">forresthatfield.com</a>.</div>');
	$('#shell').append('<div>Type \'help\' to see a list of commands.</div>');
	$('#shell').append('<div>&nbsp;</div>');
	$('#shell').append('<div><span class="prompt">[forresthatfield.com]#</span><span class="command"></span><span class="cursor"></span></div>');
});
