﻿$.fn.verticalLiveFeed = function() {
    return this.each(function() {
        var $wrapper = $(this)

        function newTweet(cont) {
            tweetContainer = cont;

            $collection = $wrapper.find("> li");

            if ($collection.length >= 3) {
                $collection.filter("li:last").find("span").fadeOut("slow", function() {
                    $collection.filter("li:last").css("display", "none");
                    DoAnimation(tweetContainer);
                    $collection.filter("li:last").remove();
                });
            } else {
                DoAnimation(tweetContainer);
            }
        }

        function DoAnimation(tweetContainer) {
            $collection.filter("li:first").before($(tweetContainer).hide());
            $(tweetContainer).slideDown("slow");
            $(tweetContainer).find("span").hide();
            $(tweetContainer).find("span").fadeIn("slow");
        }

        $(this).bind('newTweet', function(event, cont) {
            newTweet(cont);
        });
    });
};