// Shrink longer URLs by taking out the middle. 
// Called by tweetStyling
function shrinkUrl(longUrl){
    if(longUrl.length<=25) {
        if (longUrl.substring(0,4)=='http') {
            return longUrl.substring(7);
        } else {
            return longUrl;
        }
    }
    var l=longUrl.length;
    var firstUrlPart=longUrl.substring(0,24);
    //alert(firstUrlPart);
    if (firstUrlPart != 'http://') {
        firstUrlPart = firstUrlPart.substring(7,24);
        //alert(firstUrlPart);
    }
    var lastUrlPart=longUrl.substring(l-15,l);
    var shortUrl=firstUrlPart+' ...'+lastUrlPart;
    return shortUrl;
}

// Adjust the markup of the tweets on the homepage 
function tweetStyling() {
    $(".aktt_more_updates").remove();
    var items = $('#sidebar .aktt_tweets li');
    $(items).each( function(i) {
    
    	// ID this item and the links in it.
        var item = items[i];
        links = $("a", item);
        linkNum = links.length;
        // remove the last link, always the twitter address.
        $(":last", links).remove;
        
        // If there's one link, make the whole thing link to it.
        if (linkNum == 2) { 
            $(links).empty();
            $(this).children('a').remove();
            $(this).wrapInner(links[0]);
        }
        
        // If there's more than one link, create a 'more' link for the last one.
        if (linkNum >= 3) { 
            $(links).each(function(j) {
                if ((j + 1) < (linkNum -1)) {
                    $(this).css("display", "inline");
                    var shorterUrl = shrinkUrl($(this).text());
                    $(this).text(shorterUrl);
                }
                if ((j + 1) == (linkNum -1)) {
                    $(this).css("display", "inline");
                    var shorterUrl = shrinkUrl($(this).text());
                    $(this).text(shorterUrl);
                }
                if ((j + 1) == linkNum) {
                    $(this).remove();
                }
            });
        }
    });  
}

$(document).ready(function(){
    tweetStyling();
});
