$(document).ready(function(){
    var total = $(".middle .twitterblok li").length;
    $(".middle .twitterblok li").css("display","none");
    $("#tweets-animation").css("display","block").append("<div>"+$($(".middle .twitterblok li")[total-1]).html()+"</div>");
    
    tweetFadeIn(total-2);
});

function tweetFadeIn(index){
    var runned, $Tweet, height, exist = null;
    var index = index < 0 || index == undefined ? ($(".twitterblok li").length - 1) : index;
    var $Tweets = $(".twitterblok li");
    
    if(1==2 && $("#tweet"+index).html()){ 
        $Tweet = $("#tweet"+index);
        height = $Tweet.innerHeight();
        exist  = true;
    }
    else{
        $Tweet = $("<div></div>").html($($Tweets[index]).html()).attr("id","tweet"+index);
        height = $($Tweets[index]).innerHeight();
    }
        
    $("#tweets-animation div").delay(1000).animate({top:"+="+height+"px"},"fast",function(){
        if(!runned){
            runned = true;
            if(!exist){
                insertTweet($Tweet);
            }
            else{
                $Tweet.fadeOut().animate({top:"0"},"fast",function(){
                    $Tweet.fadeIn("fast");
                    tweetFadeIn(index-1);
                });
            }
        }
    });
}
function insertTweet($Tweet){
    $Tweet.css("display","none");
    $("#tweets-animation").append($Tweet)
    $Tweet.fadeIn("fast",function(){
        tweetFadeIn($Tweet.attr("id").replace(/tweet/i,"")-1);
    });
}
function moveTweet($Tweet){
    $($Tweet).fadeOut().animate({top:"0px"}).fadeIn();
}
