// A helper var to count the span we are in
var spanIndex = 0;

// Wait until dom is ready
$(document).ready(function ()
{
    // Check wheter we have to show the animation
    if (location.href.indexOf('#instant') == -1)
    {
        $('#page')
        // Add the cursor
            .append($('<span></span>').attr('id', 'cursor').addClass('ignore').text("|").everyTime(550, 'toggle', function ()
            {
                // Toggle the class
                $(this).toggleClass('visible');
            }))
        // Hide all spans
            .find('span').css('display', 'none')
        // Iterate through all spans
            .each(function (index, element)
            {
                // Get the span
                var span = $(this);

                // This span has to get progressed later
                span.addClass('progress');

                // Check whether we got a text content span
                if (!span.hasClass('html') && !span.hasClass('ignore'))
                {
                    // This span is a word
                    span.addClass('word');

                    // Set up a buffer
                    var htmlBuffer = '';

                    // Get the span text
                    var spanText = span.text();

                    // Count the characters
                    var spanTextLength = spanText.length;

                    // Iterate all characters
                    for (var i = 0; i < spanTextLength; ++i)
                    {
                        // Wrap all characters
                        htmlBuffer += '<span class="char">' + spanText.substr(i, 1) + '</span>';
                    }
                    // Try to find a link
                    var link = span.find('a');

                    // Append the new content
                    (link.length > 0 ? link : span).html(htmlBuffer);
                }
            })

        // Start writing in 2000 ms
        $('#page').oneTime(2000, 'startWriting', writeText);

        // Hide the image and show it again later
        $('#tk').hide().oneTime(5000, 'fadeInAgain', function ()
        {
            // Fade in the image
            $(this).fadeIn(1000);
        });

        // Hide the socialbit link and show it again later
        $('#socialbit,#social').hide();
    }
});

var writeText = function ()
{
    // Get the current span
    var span = $('span.progress:eq(' + spanIndex + ')');

    // Set up time buffer
    var timeBuffer = 0;

    // Check whether we dont have to ignore this element
    if (!span.hasClass('ignore'))
    {
        // Show the word
        span.css('display', 'inline');

        // Check whether the span contains no html
        if (!span.hasClass('html'))
        {
            // Iterate all characters
            span.find('.char').each(function (index, element)
            {
                // Increase the time buffer randomly
                timeBuffer += (20 + Math.floor(Math.random() * 101));

                // Set up the timer
                $(element).oneTime(timeBuffer, 'show', function ()
                {
                    // Show the character
                    $(this).css('display', 'inline');
                });
            });
        }
    }

    // Increase the time buffer randomly
    timeBuffer += (!span.hasClass('fast') ? (200 + Math.floor(Math.random() * 1201)) : (20 + Math.floor(Math.random() * 101)));

    // Increase the counter
    ++spanIndex;

    // Check whether we got more words to write
    if ($('span.progress:eq(' + spanIndex + ')').length > 0)
    {
        // Write the next word
        span.oneTime(timeBuffer, 'nextWord', writeText);
    }
    // We got no more words to write
    else
    {
        // Hide the socialbit link and show it again later
        $('#socialbit,#social').fadeIn(1000);
 
        // Hide adn remove the cursor
        $('#cursor').fadeOut(500, function () { $(this).remove(); });

        // Some debug output
        // alert('Done!');
    }
}
