$(document).ready(function() {
    var display = $("#display");
    display.empty();
    
    var messages = [
        function() {
            return '<strong><a href="../">atashi.org</a></strong>\nescape as the spiral closes';
        },
        
        function() {
            return 'design concept 7\n<strong>data feed\u00a0/ feed data</strong>';
        },
        
        function() {
            return 'created using <strong><a href="http://jquery.com/">jQuery</a></strong>\nand the <strong><a href="http://www.omkrets.se/typografi/">Miso</a></strong> typeface';
        },
        
        function() {
            return 'source code\n<strong><a href="keyboard.js">JavaScript</a></strong>, <strong><a href="screen.css">CSS</a></strong>';
        },
        
        function() {
            var date = new Date();
            return 'currently <strong>' + date.toLocaleTimeString() + '</strong>\n' + date.toLocaleDateString();
        },
        
        function() {
            // estimation algorithm shamelessly stolen from <http://math.berkeley.edu/~galen/popclk.html>
            var fromEpoch = (new Date()).getTime() - Date.UTC(2000, 6, 1);
            var population = "" + Math.floor(Math.exp(22.528293835777973 + 4.0275867940663167699e-13 * fromEpoch - 1.1020778551110890530e-25 * fromEpoch * fromEpoch));
            var formattedPopulation = [];
            
            while (population.length > 0) {
                formattedPopulation.unshift(population.substring(population.length - 3));
                population = population.substring(0, population.length - 3);
            }
            
            return 'about <strong>' + formattedPopulation.join(',') + '</strong> people\ncall Earth home right now';
        },
        
        function() {
            return '<strong>end of transmission</strong>\nrepeating in 7000 milliseconds';
        }
    ];
    
    var currentMessage = 0;
    
    // add a cursor since "text-decoration: blink" support is iffy
    var cursor = $(document.createElement("span"));
    cursor.append(document.createTextNode("\u200b_"));
    display.append(cursor);
    
    (function() {
        cursor.toggle();
        window.setTimeout(arguments.callee, 500);
    })();
    
    var keyboard = function() {
        display.empty();
        
        var textQueue = [];
        
        // build the "empty" tree
        (function(node, target) {
            var skeletonNode, i;
            
            switch (node[0].nodeType) {
                case 1 /* element */:
                    skeletonNode = document.createElement(node[0].tagName);
                    
                    for (i = 0; i < node[0].attributes.length; i++) {
                        skeletonNode.setAttribute(node[0].attributes[i].name, node[0].attributes[i].value);
                    }
                    
                    var contents = node.contents().get();
            
                    for (i = 0; i < contents.length; i++) {
                        arguments.callee($(contents[i]), skeletonNode);
                    }
                    
                    break;
    
                case 3 /* text node */:
                    skeletonNode = document.createTextNode("");
                    textQueue.push({
                        content: node[0].textContent || node[0].nodeValue /* IE */,
                        target: skeletonNode
                    });
                    break;
            }
            
            $(target).append(skeletonNode);
        })($("<div>" + (messages[currentMessage])() + "</div>"), display);
        
        currentMessage = (currentMessage + 1) % messages.length;
        
        // force the cursor to the end of the display again
        display.append(cursor);
        
        // start typing the text
        (function() {
            if (textQueue.length > 0) {
                if (textQueue[0].target.textContent) {
                    textQueue[0].target.textContent += textQueue[0].content.charAt(0);
                } else { /* IE */
                    textQueue[0].target.nodeValue += textQueue[0].content.charAt(0);
                }
                
                textQueue[0].content = textQueue[0].content.substring(1);
                
                if (textQueue[0].content.length == 0) {
                    textQueue.shift();
                }
                
                window.setTimeout(arguments.callee, 25);
            } else {
                window.setTimeout(keyboard, 7000);
            }
        })();
    }
    
    window.setTimeout(keyboard, 1000);
});