(function ($) {
    // Adapted from the typewriter() function in the "Text Effects" plugin by Jason Frame:
    // http://onehackoranother.com/projects/jquery/jquery-grab-bag/text-effects.html
    $.fn.transmission = function() {
        var textDelay = 5;
        var restartDelay = 5000;
      
        var scrollStart = 20;
        var scrollIncrement = 5;
        var scrollSpeed = "slow";
        
        this.each(function() {
            var $ele = $(this), str = $ele.text(), progress = 0, lines = 0;
            $ele.text('');
            $ele.animate({top: 0}, "fast");
            var timer = setInterval(function() {
                $ele.text(str.substring(0, progress++) + (progress & 1 ? '_' : ''));
                if (str.charAt(progress) == "\n") {
                    lines++;
                    if (lines > scrollStart && lines % scrollIncrement == 0) {
                        $ele.animate({top: "-" + (lines - scrollStart) + "em"}, "slow");
                    }
                }
                if (progress > str.length) {
                    clearInterval(timer);
                    setTimeout(function () {
                        $ele.transmission();
                    }, restartDelay);
                }
            }, textDelay);
        });
        return this;
    };
})(jQuery);
