function lobster(lt) {
    this.lt = lt;
    this.movingright = true;
    this.x = 0;
    this.stopped = true;

    this.blink = function(closed) {
        var l = document.getElementById(this.lt);
        var lw = l.firstChild.nodeValue;
        if (closed) {
            // We cheat and always put the eyes in the o_O configuration after
            // blinking. This is what real lobsters do as well!
            l.replaceChild(
                document.createTextNode(lw.replace(/-_-/,"o_O")),
                l.firstChild
            );
            window.setTimeout(function(target){target.blink(false);}, 15000 * Math.random() + 50, this);
            //window.setTimeout("this.blink(false)", 15000 * Math.random() + 50);
        }
        else {
            l.replaceChild(
                document.createTextNode(lw.replace(/o_O/i, "-_-")),
                l.firstChild
            );
            window.setTimeout(function(target){target.blink(true);}, 100 * Math.random() + 150, this);
            //window.setTimeout("this.blink(true)", 100 * Math.random() + 150);
        }
    }

    this.chop = function(repeat) {
        if (repeat) {
            window.setTimeout(function(target){target.chop(true);}, 1500 * Math.random() + 2500, this);
            //window.setTimeout("this.chop(true)", 1500 * Math.random() + 2500);
        }
        if (repeat && !this.stopped) {
            return 0;
        }
        var l = document.getElementById(this.lt);
        var lw = l.firstChild.nodeValue;
        // Yeah, this is awful, but I'm lazy, so that's allowed.
        lw = lw.replace(/\|/,"closed");
        lw = lw.replace(/\\\//,"open");
        lw = lw.replace(/closed/, "\\/");
        lw = lw.replace(/open/, "|");
        lw = lw.replace(/O_o/, "o#O");
        lw = lw.replace(/o_O/, "O#o");
        lw = lw.replace(/#/, "_");
        l.replaceChild(document.createTextNode(lw), l.firstChild);
    }

    this.move = function(firstload) {
        if (firstload) {
            window.setTimeout(function(target){target.move(false);}, Math.random() * 30000 + 10000, this);
            //window.setTimeout("this.move(false)", Math.random() * 30000 + 10000);
            return 0;
        }
        var l = document.getElementById(this.lt);
        var lw = l.firstChild.nodeValue;
        if (this.x == 0) {
            this.movingright = true;
        }
        else if (this.x > 25) {
            this.movingright = false;
        }
        if (this.movingright) {
            lw = " " + lw;
            this.x++;
        }
        else {
            lw = lw.replace(/ /, "");
            this.x--;
        }
        // Stop?
        if (Math.random() > 0.97) {
            if (Math.random() > 0.8) {
                // Lobsters usually switch directions after resting
                this.movingright = !this.movingright;
            }
            this.stopped = true;
            window.setTimeout(function(target){target.move(false);}, Math.random() * 30000 + 15000, this);
            //window.setTimeout("this.move(false)", Math.random() * 30000 + 15000);
        }
        else {
            this.stopped = false;
            l.replaceChild(document.createTextNode(lw), l.firstChild);        
            this.chop(false);
            window.setTimeout(function(target){target.move(false);}, 150, this);
            //window.setTimeout("this.move(false)", 150);
        }
    }

    this.init = function() {
        this.blink(false);
        this.chop(true);
        this.move(true);
    }

}



