(function() {
    var cursor = 0;
    var limit = 20;

    function update() {
        var url = 'http://creative360.com/sxswcables/ajax.php'
                        + '?cursor=' + cursor + '&callback=?';
        $.ajax({
            url:      url,
            cache:    false,
            dataType: 'json',
            success:  function(data) {
                $('#indicator').hide();
                $.each(data, function(idx, item) {
                    var tweet = $('#clonesrc').clone();

                    cursor = item.id;
                    tweet.attr('id', 'twt' + item.id);
                    $('#tweets').prepend(tweet);

                    var twid = '#twt' + item.id;
                    $(twid + ' .tweet-image img')
                            .attr('data-user-id', item.user.id)
                            .attr('href', 'http://twitter.com/' + item.user.screen_name)
                            .attr('alt', item.user.name)
                            .attr('src', item.user.profile_image_url);

                    $(twid + ' a.tweet-screen-name')
                            .attr('data-user-id', item.user.id)
                            .attr('href', 'http://twitter.com/' + item.user.screen_name)
                            .attr('title', item.user.name)
                            .html(item.user.screen_name);

                    $(twid + ' span.tweet-full-name').html(item.user.name);

                    var tweet_text = item.text;
                    tweet_text = tweet_text.replace(/@([A-Za-z0-9_-]*)/g, "@<a href='http://twitter.com/$1'>$1</a>");
                    tweet_text = tweet_text.replace(/#([A-Za-z0-9_-]*)/g, "#<a href='http://twitter.com/?searchq=%23$1'>$1</a>");
                    $(twid + ' .tweet-text').html(tweet_text);

                    $(twid + ' a.tweet-timestamp')
                            .attr('href', 'http://twitter.com/' + item.user.screen_name
                                + '/status/' + item.id)
                            .attr('title', item.created_at_str);

                    $(twid + ' a.tweet-timestamp span')
                            .attr('data-time', item.created_at);
                    $(twid).slideDown(250);
                });
                adjustTimes();
                while ($('#tweets .tweet:nth-child(' + limit + ')').length > 0) {
                    $('#tweets .tweet:nth-child(' + limit + ')').remove();
                }
                setTimeout(function() {
                    update();
                }, 5000);
            },
            error:    function(data) {
                setTimeout(function() {
                    update();
                }, 5000);
            }
        });
    }

    function adjustTimes() {
        var now = parseInt(new Date() / 1000);
        $('#tweets .tweet a.tweet-timestamp span').each(function(index) {
            var display = '';
            var suffix = 's';
            var term = '';
            var ts = $(this).attr("data-time");
            if (now - ts >= 86400) {
                display = $(this).closest('a').attr('title');
            } else {
                var newval;
                if (now - ts < 60) {
                    newval = now - ts;
                    term = 'second';
                } else if (now - ts < 3600) {
                    newval = parseInt((now - ts) / 60);
                    term = 'minute';
                } else {
                    newval = parseInt((now - ts) / 3600);
                    term = 'hour';
                }
                if (newval == 1) {
                    suffix = '';
                }
                display = newval + ' ' + term + suffix + ' ago';
            }
            $(this).html(display);
        });
    }

    function flickr() {
        var delay = Math.floor(Math.random()*100);
        $('#tweets .tweet').each(function(index) {
            $(this).delay(delay).animate({opacity:0.6}, 200, function() {
                $(this).animate({opacity:1.0}, 200);
            });
            delay = delay + Math.floor(Math.random()*100);
        });
        setTimeout(function() {
            flickr();
        }, 30000 + delay);
    }

    setTimeout(function() {
        flickr();
    }, 30000);

    $(document).ready(function(e) {
        update();
        setInterval(function() {
            adjustTimes();
        }, 1000);
    });
})($);

