jQuery.fn.typewriter = function(config) {
    return this.each(function(){

        var speed = config.speed;
        var listText = config.listText;
        var numElements = config.listText.length;
        var delay = config.delay;

        var curNum = 0;
        var elem = jQuery(this);
        var text = listText[curNum];
        var pos = 1;
        var interval;

        if (text.length) {
            elem.html("");
            start();
        }

        function write() {
            elem.html(text.substr(0, pos++)+' _ ');

            if (pos > text.length) {
                /*++curNum;
                curNum = curNum < listText.length ? curNum : 0;*/

                curNum = Math.floor(Math.random() * numElements);

                text = listText[curNum];
                pos = 1;
                clearInterval(interval);
                setTimeout(start, delay);
            }
        }

        function start() {
                interval = setInterval(write, speed);
        }
    });
}; 

