var done = 0;
var timeoutId = 0;

function shouldFixData() {
   if (done == 4) {/*change this number if the number of feeds changes*/
       fixData();
       $("#filters").show();
       $("#loading").hide();
   }
   else {
       $("#filters").hide();
       $("#loading").show();
   }
}
function fixData() {
    clearTimeout(timeoutId);

    $(".date").each(function() {
        // fix flickr dates
        var data = this.innerHTML.replace("Z", "");

        // format dates in a consistent way
        var dt = Date.parse(data);
        if (dt != null) {
            this.innerHTML = dt.toString("MMM dd, yyyy  h:mm tt");

            // we use this to sort the content
            var hidden_content = dt.toString("yyyy-MM-dd HH:mm:ssZ");
            $(this).parent().find('.sort-date')[0].innerHTML = hidden_content;
        }
    });

    // Make url's links
    $(".feed-item").linkify();

    // Sort by date
    $(".sort-date").hide();
    $(".feed-item").tsort(".sort-date", {order:"desc"})

    limitData('div.feed-item');

}
function limitData(selector) {
    // Only show 24 items
    var from = 0;
    var step = 24;
    $(selector).hide()
        .end()
        .find(selector + ':lt(' + (from + step) + '):not(' + selector + ':lt(' + from + '))')
                                                     .show()
}

function cleanUpTweets() {
    $('.twitter p').each(function() {
        var text = this.innerHTML;

        text = text
        .replace(/@([a-zA-Z0-9_]+)/g, '@<a href="http://twitter.com/$1" title="Click to view $1 on Twitter">$1</a>')
        .replace(/(?:^|\s)#([^\s\.\+:!]+)/g, function (a, u) {
            return ' <a href="http://twitter.com/search?q=' + encodeURIComponent(u) + '" title="Click to view this on Twitter">#' + u + '</a>';
        });
    this.innerHTML = text;
    });

}

$(document).ready(function() {
    timeoutId = setInterval ("shouldFixData()", 1000 );

    $("#filters a").click(function() {
		$("#filters a").removeClass("active");
		$(this).addClass("active");
        if (this.innerHTML == "all") {
            $("div.feed-item").fadeIn('slow');
            limitData("div.feed-item");
        }
        else {
            var selector = "div.feed-item." + this.innerHTML;
            $("div.feed-item").fadeOut('slow');
            $(selector).fadeIn('slow');
            limitData(selector);
        }
    });	

    $.getJSON('http://joshuantaylor.tumblr.com/api/read/json?callback=?',
        function(tumblr) {
            $( "#tumblr-template" ).tmpl(tumblr.posts).appendTo( "#feed-list" );
            done += 1;
        });

    $.getJSON('http://api.dribbble.com/players/joshuantaylor/shots?callback=?',
            function(dribbble) {
                $( "#dribbble-template" ).tmpl(dribbble.shots).appendTo( "#feed-list" );
                done += 1;
            });
    $.getJSON('http://search.twitter.com/search.json?q=from:joshuantaylor&amp;rpp=10?callback=?',
            function(twitter) {
                $( "#twitter-template" ).tmpl(twitter.results).appendTo( "#feed-list" );
                done += 1;
                cleanUpTweets();
            });
    $.getJSON('http://api.flickr.com/services/feeds/photos_public.gne?id=82816412@N00&lang=en-us&format=json&jsoncallback=?',
            function(flickr) {
                $( "#flickr-template" ).tmpl(flickr.items).appendTo( "#feed-list" );
                done += 1;
            });
	/*
    $.getJSON('http://www.joshuataylordesign.com/blog/api/get_recent_posts/?callback=?',
            function(blog) {
                $( "#blog-template" ).tmpl(blog.posts).appendTo( "#feed-list" );
                done += 1
            });*/

});

