// Compatability with jQuery:
jQuery.noConflict();

window.$ = function (element) {
	return document.getElementById(element);
};

/* Header and footer JavaScript */
jQuery(document).ready(function() {
	var $ = jQuery;

    function scroll_tweet(li){
        
        var post = li.children("div.post"),
        pWidth = post.width();
        if (pWidth > parseInt(balloon.css('width')))
        {
            var leftEnd = li.find('div.end');
            if(leftEnd.length === 0)
			{
                leftEnd = $('<div class="end left" />').appendTo(li);
            } 
            var offsX = parseInt(leftEnd.width());
            post.animate({left: offsX - pWidth - 55}, 14000, 'linear', function(){post.css('left', offsX);});
        }
    }

    function hide_tweet(current_tweet)
    {
        var cli = $("li#tweet" + current_tweet);
		cli.fadeOut();
    }


    function swap_tweets(current_tweet)
    {
        var cli = $("li#tweet" + current_tweet);
		cli.fadeOut();
        var next_tweet = (current_tweet + 1) % 5; /* max five tweets */
        var li = $("li#tweet" + next_tweet);
        li.fadeIn(400);
        setTimeout(function(){scroll_tweet(li);}, 4000);                
        setTimeout(function(){hide_tweet(next_tweet);}, 13500);
        setTimeout(function(){swap_tweets(next_tweet);}, 14000);
    }
        
    var balloon = $("#twitterpost div.balloon");
    if($("div.balloon ul li#tweet1").html() !== null)
    {
        var firstli = $("li#tweet0");
        setTimeout(function(){scroll_tweet(firstli);}, 4000);
        setTimeout(function(){hide_tweet(0);}, 13500);
        setTimeout(function(){swap_tweets(0);}, 14000);
    }

});
