/*========================================================================

blinkers.js | An easy to use script that adds a typewriter effect to text
Author: JigaR M. aka B-Reft | Monstamouth Entertainment
Porque somos los mejores... Somos los Monstamouth

Changelog:
) Improper word-breaks resolved;
) Initial letter-omissions while repeating the text resolved;
) Added pause at the end of each line;

==========================================================================*/

$('document').ready(function() {
                $('div.blinker')
                .each(function() {
                                thisBlinkerDiv = this;
                                thisBlinker = new blinker(thisBlinkerDiv);
                });
});

function blinker(el) {
                // Pointer to the div that contains the content of this blinker
                this.container = el;
                // Tracks the total number of spans in this blinker
                this.active = false;
                // Number of characters to display at once
                this.maxChars = 78;
                // The number of seconds for which the animation should stop between lines
                this.pauseLength = 1;
                // A function to initialize things - It hides all the spans inside
                // The container div so that it looks empty onload
                this.init = function() {
                                $(this.container)
                                .find('span')
                                .css('display', 'none');
                                // Collecting the text to be displayed inside the blinker and
                                // Splitting it for pauses and stuff
                                this.text = "";
                                while($(this.container).find('span').length > 0) {
                                                var thisSpan = $(this.container).find('span')[0];
                                                var thisSpanText = $(thisSpan).text();
                                                // Removing unwanted characters
                                                thisSpanText = thisSpanText.replace('&nbsp;', ' ');
                                                thisSpanText = thisSpanText.replace('<br>', '');
                                                thisSpanText = thisSpanText.replace('<br />', '');
                                                thisSpanText = thisSpanText.replace('\n', '');
                                                thisSpanText = thisSpanText.replace('\r', '');
                                                // Setting pause markers using the pipe character
                                                while(thisSpanText.length > 0) {
                                                                var currTextBlock = thisSpanText.substr(0, this.maxChars);
                                                                // Detecting last space in the current text block
                                                                var lastSpacePos = currTextBlock.length;
                                                                if(currTextBlock.length>=this.maxChars) {
                                                                                while(lastSpacePos!=0 && currTextBlock.substr(lastSpacePos, 1)!=' ') lastSpacePos--;
                                                                                if(lastSpacePos==0 && currTextBlock.length<this.maxChars) lastSpacePos=currTextBlock.length;
                                                                }
                                                                this.text += thisSpanText.substr(0, lastSpacePos+1) + '|';
                                                                thisSpanText = thisSpanText.substr(lastSpacePos+1, thisSpanText.length);
                                                }
                                                thisSpan.parentNode.removeChild(thisSpan);
                                }
                                // Removing back to back pauses and initial/terminal spaces
                                this.text = this.text.replace('||', '|');
                                this.text = this.text.replace('| ', '|');
                                this.text = this.text.replace(' |', '|');
                                // Creating the area where the text shall be displayed
                                this.nowShowing = document.createElement('span');
                                $(this.nowShowing)
                                .css('display', 'none')
                                .css('text-transform', 'uppercase')
                                .appendTo(this.container);
                                // Creating and adding the blinking cursor inside the container
                                this.cursor = document.createElement('span');
                                $(this.cursor)
                                .html('_')
                                .css('font-weight', 'bolder')
                                .css('padding-left', 0)
                                .css('margin-left', 0)
                                .css('margin-right', 1)
                                .appendTo(this.container);
                                var blinkerCursor = this.cursor;
                                var blinkInterval = setInterval(function() {
                                                $(blinkerCursor)
                                                .toggleClass('hidden');
                                }, 130);
                                this.animate();
                }
                // Character by character, it adds visibility to the letters of
                // The span element currently being displayed
                this.animate = function() {
                                var thisBlinker = this;
                                thisBlinker.active = true;
                                $(thisBlinker.nowShowing)
                                .css('display', 'inline');
                                // Updating nowShowing counter
                                thisBlinker.currCharIndex = 0;
                                var charInterval = setInterval(function() {
                                                // If the current sentence has ended
                                                if(thisBlinker.currCharIndex >= thisBlinker.text.length) {
                                                                thisBlinker.currCharIndex = 0;
                                                }
                                                // If animation for a sentence is going on
                                                if(thisBlinker.active == true) {
                                                                if(thisBlinker.text.substr(thisBlinker.currCharIndex, 1) == '|') {
                                                                                thisBlinker.clear();
                                                                                thisBlinker.currCharIndex++;
                                                                } else {
                                                                                $(thisBlinker.nowShowing)
                                                                                .html($(thisBlinker.nowShowing).html() + thisBlinker.text.substr(thisBlinker.currCharIndex, 1));
                                                                                thisBlinker.currCharIndex++;
                                                                }
                                                }
                                }, 130);
                }
                // Stops the blinker animation from proceeding for a while
                this.pause = function() {
                                var thisBlinker = this;
                                thisBlinker.active = false;
                                setTimeout(function() {
                                                thisBlinker.active = true;
                                }, (thisBlinker.pauseLength * 1000));
                }
                // Stops the blinker animation from proceeding for a while
                // Then clears visible content and resumes animation
                this.clear = function() {
                                var thisBlinker = this;
                                thisBlinker.active = false;
                                setTimeout(function() {
                                                $(thisBlinker.nowShowing).html('');
                                                thisBlinker.active = true;
                                }, (thisBlinker.pauseLength * 1000));
                }
                // Constructor stuff to get things started
                this.init();
}
