// JS Shell
// Author: Jacob Torrey
// Date: Oct. 26, 2008

var termtxt = 'guest@jacobtorrey.com:/home/jacob/$ ';
var syncc = 0;
var blinkvar = false;
var tttspeed = 100;

function sync() {
    if(!blinkvar) {
	document.getElementById("tterm").style.visibility = 'visible';
	document.getElementById("sol").style.visibility = 'visible';
	blinkvar = true;    
    } 
    syncc++;
    switch(syncc) {
    case 1:
	getFocus();
	var str = "cd /home/jacob && ls";
	ttt("tterm", str);
	window.setTimeout("sync()", tttspeed * str.length);
	return;
	break;
    case 2:
	appendterm('<a href="#" onclick="javascript:cat(\'about\', 0);">about</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="javascript:cat(\'projects\', 0);">projects</a>');
	appendterm('<a href="#" onclick="javascript:cat(\'related\', 0);">related_sites</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="javascript:cat(\'contact\', 0);">contact</a>');
	appendterm(termtxt);
	document.getElementById('cominput').style.visibility = 'visible';
	document.getElementById('cominput').focus();
	break;
    default:
	return;
    }
    sync();
    //    getFocus();
}

function resize() {
    var dom = document.getElementById('cominput');
    var currentsize = parseInt(dom.style.width.replace('ex', ''));
    var len = dom.value.length + 1;
    if(len <= currentsize) {
	len -= 2;
    }
    dom.style.width = len + 'ex';
}

function cat(file, cb) {
    document.getElementById('term').scrollTop = document.getElementById('term').scrollHeight;
    switch(file) {
    case "about":
    	if(cb) {
	    appendterm("Login: jacob&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name: Jacob Torrey");
	    appendterm("Home Directory: Clarkson University&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Shell: /bin/bash");
	    appendterm("On since Sun Oct 26 15:57 (EDT) on tty7 from :0");
	    appendterm("&nbsp;&nbsp;&nbsp;&nbsp;1 hour 19 minutes idle");
	    appendterm("No mail.");
	    appendterm("Plan:");
	    appendterm("Current place of education: Clarkson University");
	    appendterm("Major area of study: MSc. candidate in Computer Science");
	    appendterm("Current place of employment: Assured Information Security");
	    appendterm("Interests in computer science: Systems biology, distributed systems, cellular automaton, computer security");
	    appendterm("Extracurricular interests: Hiking, canoing, amateur radio, veganism");
	    appendterm(termtxt);
	} else {
	    str = "finger jacob";
	    ttt("tterm", str);
	    window.setTimeout("cat('" + file + "', '" + 1 + "')", tttspeed * str.length);
	}
	break;
    case "related":
	if(cb) {
	    appendterm('<a href="#" onclick="openlink(\'http://www.r4n0k.com\', 0)">blog</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="openlink(\'https://launchpad.net/~ranok\', 0)">launchpad</a>');
	    appendterm('<a href="#" onclick="openlink(\'http://people.clarkson.edu/~torreyji/\', 0)">CU_webspace</a>&nbsp;&nbsp;&nbsp;&nbsp;');
	    appendterm(termtxt);
	} else {
	    str = 'ls related_sites';
	    ttt("tterm", str);
	    window.setTimeout("cat('" + file + "', '" + 1 + "')", tttspeed * str.length);
	}
	break;
    case "sudo":
	if(cb) {
	    termtxt = 'root@jacobtorrey.com:/home/jacob/# ';
	    appendterm(termtxt);
	} else {
	    str = 'sudo -s';
	    ttt("tterm", str);
	    window.setTimeout("cat('" + file + "', '" + 1 + "')", tttspeed * str.length);
	    
	}
	break;
    case "contact":
	if(cb) {
	    appendterm("The best way to contact me is using the <a href=\"mailto:torreyji@clarkson.edu\">mail</a> command");
	    appendterm("Feel free to contact me with any questions regarding any of my projects or if you would like a copy of my resume");
	    appendterm(termtxt);
	} else {
	    str = "cat contact";
	    ttt("tterm", str);
	    window.setTimeout("cat('" + file + "', '" + 1 + "')", tttspeed * str.length);
	}
	break;
    case "projects":
	if(cb) {
	    appendterm("* Paxos - A paper written for my cryptography class describing the Paxos algorithm and some improvements and innovative implementations.");
	    appendterm("&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"#\" onclick=\"openlink('http://www.jacobtorrey.com/PAXOS/paxos.pdf', 0);\">Paper Download (pdf)</a>");
	    appendterm("&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"#\" onclick=\"openlink('http://www.jacobtorrey.com/PAXOS/paxos.erl', 0);\">Basic Paxos simulator source code download (erl)</a>");
	    appendterm("* Web Application Server (WAPS) - A proof of concept look at easily converting desktop applications into dynamic AJAX run web applications.");
	    appendterm("&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"#\" onclick=\"openlink('http://www.jacobtorrey.com/WAPS/WAPS.ps', 0);\">Paper download (ps)</a>");
	    appendterm("&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"#\" onclick=\"openlink('http://www.jacobtorrey.com/WAPS/WAPS.tar.gz', 0);\">Source code download (tar.gz)</a>");
	    appendterm("* Object-Oriented Approach to Manipulating Acoustic and Seismic Spectra - Co-authored a research paper describing the development of and usage of a Java class to contain and manipulate acoustic spectral data");
	    appendterm("&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"#\" onclick=\"openlink('http://www.crrel.usace.army.mil/library/technicalreports/TR06-20.pdf', 0);\">Paper download (pdf)</a>");
	    appendterm("* Ethics of Key Escrow - A paper discussing the background and ethics of the practice of key escrow");
	    appendterm("&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"#\" onclick=\"openlink('http://www.jacobtorrey.com/KE/keyescrow.pdf', 0);\">Paper download (pdf)</a>");
	    appendterm("* Wiki Wide Web - A web-based system to allow any user to to edit other websites and colaborate with other users.");
	    appendterm("&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"#\" onclick=\"openlink('http://www.wikiwideweb.com', 0);\">Wiki Wide Web (external site)</a>");
	    appendterm("&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"#\" onclick=\"openlink('http://www.jacobtorrey.com/WWW/wikiwideweb.pdf', 0);\">Paper submitted to WikiSym 2008 but not accepted (pdf)</a>");
	    appendterm("* Open Server Project - An attempt to remove the boiler-plate code from creating robust, secure servers thus allowing developers to focus on creating the server logic.");
	    appendterm("&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"#\" onclick=\"openlink('http://www.openserverplatform.com', 0);\">Open Server Platform (external site)</a>");
	    appendterm(termtxt);
	} else {
	    str = "cat projects";
	    ttt("tterm", str);
	    window.setTimeout("cat('" + file + "', '" + 1 + "')", tttspeed * str.length);
	}
	break;
    default:
	return false;
    }
    getFocus();
    return false;
}

function getFocus() {
    document.getElementById('cominput').focus();
    //window.setTimeout('getFocus()', 500);
}

function readcommand() {
    var com = document.getElementById("cominput").value;
    com = com.replace(/</g, '&lt;');
    com = com.replace(/>/g, '&gt;');
    document.getElementById("cominput").value = '';
    document.getElementById("cominput").style.width = '0ex';
    appendterm2(com);
    switch(com) {
    case "cat about":
    case "finger jacob":
	cat('about', 1);
	break;
    case "sudo -s":
	termtxt = 'root@jacobtorrey.com:/home/jacob/# ';
	appendterm(termtxt);
	break;
    case "cat projects":
	cat('projects', 1);
	break;
    case "ls":
    case "ls /home/jacob":
	syncc = 1;
	sync();
	break;
    case "ls related_sites":
	cat('related', 1);
	break;
    case "cat contact":
	cat('contact', 1);
	break;
    case "whoami":
	appendterm("guest");
	appendterm(termtxt);
	break;
    case "date":
	var d = new Date();
	var daytoday = new Array("Sun","Mon","Tues","Wed","Thurs","Fri","Sat");
	var monthtomonth = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	appendterm(daytoday[d.getDay()] + ' ' + monthtomonth[d.getMonth()] + ' ' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds() + ' ' + d.getFullYear());
	appendterm(termtxt);
	break;
    case "help":
	appendterm("JIT-sh Version 0.1");
	appendterm("Commands:");	
	appendterm("help - Prints this help");
	appendterm("ls - Prints the orignal menu");
	appendterm("finger jacob - Printd out information about Jacob");
	appendterm("cat projects - Prints Jacob's current projects");
	appendterm("ls related_sites - Lists sites related to Jacob");
	appendterm("cat contact - Prints out how to contact Jacob");
	appendterm("date - Prints out the current time and date");
	appendterm("help - Prints this help");
	appendterm(termtxt);
	break;
    default:
	appendterm(com + ": command not found");
	appendterm(termtxt);
    }
    return false;
}

function openlink(href, cb) {
    if(cb) {
	//location.replace(href); // Stupid IE
	location.href = href;
	appendterm(termtxt);
    } else {
	str = "lynx";
	ttt("tterm", str);
	window.setTimeout("openlink('" + href + "', '" + 1 + "')", tttspeed * str.length);
    }
}

function appendterm(str) {
    document.getElementById('tterm').innerHTML += '<br />' + str;
    document.getElementById('term').scrollTop = document.getElementById('term').scrollHeight;
}


function appendterm2(str) {
    document.getElementById('tterm').innerHTML += str;
    document.getElementById('term').scrollTop = document.getElementById('term').scrollHeight;
}

function ttt(domname, str) {
    var dom = document.getElementById(domname);
    if(str.length > 0) {
	dom.innerHTML += str.charAt(0);
	str = str.substring(1);
	if(str.charAt(0) == ' ') {
	    dom.innerHTML += str.charAt(0);
	    str = str.substring(1);
	}
	window.setTimeout("ttt('" + domname + "', '" + str + "')", tttspeed - 10);
    }  
}

function blink(divname) {
    var dive, temp;
    dive = document.getElementById(divname);
    if(dive != null) {
	temp = dive.style.color;
	dive.style.color = dive.style.backgroundColor;
	dive.style.backgroundColor = temp;
	window.setTimeout("blink('" + divname + "')", 1000);
    }
}
