
/*
 * PseudoTerminal
 * 
 * Copyright 2011, Greg Sabia
 * Licenced under the BSD License.
 * See license.txt
 *
 * Extends HAL
 *
 */

$.extend(HAL,{
    terminal : {
        line : [],
        history : [],
        terminalArea : false,
        terminalInput : false,
        sudo : false,
        bashString : '[guest@gregsabia.com]$',
        currentLine : null,
        init : function(target){
            var self = this;
            window.terminal = self;
            
            $(document).keypress(function(event){
                self.keypress(event);
            });
            
            $(function(){
                self.createInput();
                self.addTextLine('<br />You broke something!<br />Welcome to <a href="http://gregsabia.com">http://gregsabia.com</a><br />Type "home" and hit "enter" to be returned to the home page<br /><br />');
                self.addNewLine();
                
                setTimeout(function(){
                    $('#terminal_input').focus();
                },100);
                
                $('body').click(function(){
                    $('#terminal_input').focus();
                });
            });
        },
        keypress : function(event){
            var keys = KeyboardJS.activeKeys();
            
            if(keys[keys.length - 1] == 'enter'){
                var cmd = this.cmd();
                
                switch(cmd.toLowerCase()){
                    default : 
                        if(cmd) this.addTextLine('&nbsp;&nbsp;-bash: '+cmd+' not found');
                    break;
                    case 'make' :
                    case 'make me a sandwich' :
                        var line = (this.sudo) ? 'okay.' : 'what? make one yourself.';
                        this.addTextLine('&nbsp;&nbsp;' + line);
                    break;
                    case 'home' :
                        this.addTextLine('&nbsp;&nbsp;Redirecting you...');
                        setTimeout(function(){
                            window.location.href = 'http://gregsabia.com';
                        },1000);
                        
                        return false;
                    break;
                    case 'rm' :
                        this.addTextLine('removing /');
                        return setTimeout(function(){
                            terminal.addTextLine('Just kidding...');
                            terminal.clearLine();
                            terminal.addNewLine();
                        },3000);
                    break;
                    case 'ls' :
                        this.addTextLine('&nbsp;&nbsp;no.');
                    break;
                    case 'cd' :
                        this.addTextLine('&nbsp;&nbsp;MP3');
                    break;
                    case 'fuck' :
                        this.addTextLine('&nbsp;&nbsp;-bash: not a single one was given.');
                    break;
                    case 'cat' :
                        this.addTextLine('&nbsp;&nbsp;Meow.');
                    break;
                    case 'vi' :
                    case 'vim' :
                        this.addTextLine('&nbsp;&nbsp;Use nano.');
                    break;
                    case 'nano' :
                        this.addTextLine('&nbsp;&nbsp;Use vim.');
                    break;
                    case 'touch' :
                        this.addTextLine('&nbsp;&nbsp;Stop, hammer time!');
                    break;
                }
                
                this.clearLine();
                this.addNewLine();
            }
        },
        createInput : function(){
            this.terminalInput = document.createElement('input');
            $.extend(this.terminalInput,{ type : 'text', id : 'terminal_input', onkeyup : function(){ terminal.typeAhead(this.value); } });
            $(this.terminalInput).css('position','absolute').css('top','-300px');
            $(this.terminalInput).prependTo('body');
        },
        addNewLine : function(string){
            var string = string || this.bashString+' <span class="area"></span>';
            
            // Remove current class from current terminal area
            $(this.terminalArea).removeClass('current');
            
            // Create a new terminal area
            this.terminalArea = document.createElement('div');
            
            $(this.terminalArea).html(string);
            $(this.terminalArea).addClass('line').addClass('current');
            $(this.terminalArea).appendTo('div#terminal_area');
        },
        addTextLine : function(string){
            var tmp = document.createElement('div');
            $(tmp).html(string);
            $(tmp).addClass('line');
            $(tmp).appendTo('div#terminal_area');
        },
        clearLine : function(){
            this.sudo = false;
            return $('#terminal_input').val('');
        },
        getLine : function(){
            return $('#terminal_input').val();
        },
        cmd : function(){
            var fragments = this.getLine().split(' ');
            if(fragments[0] == 'sudo'){
                var cmd = fragments[1];
                this.sudo = true;
            }else{
                var cmd = fragments[0];
            }
            
            return cmd;
        },
        typeAhead : function(content){
            $('#terminal_area div.current span.area').html(content);
        }
    }
});

HAL.terminal.init();
