var categories = new Array();
var dedupedCategories = [];

$(document).ready(function() {

    $.jGFeed('http://gogeo.edina.ac.uk/gogeo-java/rss.xml',
    		function(feeds){
    		  // Check for errors
    		  if(!feeds){
    		    // there was an error
    		    return false;
    		  }
    		  // do whatever you want with feeds here
    		  for(var i=0; i<feeds.entries.length; i++){
    		    var entry = feeds.entries[i];
    		    // Entry title
                categories[categories.length] = entry;
    		  }
    		  sort(categories);
    		}, 10);
});

function caseInsensitiveCompare(a, b) {
    var anew = a.title.toLowerCase();
    var bnew = b.title.toLowerCase();
    if (anew < bnew) return -1;
    if (anew > bnew) return 1;
    return 0;
}

function sort(categories) {
    /*$(data).find('item').each(function() {
    	alert($(this).text());
        categories[categories.length] = $(this).text();
    });*/
    categories.sort(caseInsensitiveCompare);
    //Dedup tag list and create a multi-dimensional array to store 'tag' and 'tag count'
    var oldCategory = '';
    var x = 0;
    $(categories).each(function() {
        if (this.title.toString() != oldCategory) {
            //Create a new array to put inside the array row
            dedupedCategories[x] = [];
            //Store the tag name first
            dedupedCategories[x][0] = this;
            //Start the tag count
            dedupedCategories[x][1] = 1;
            x++;
        } else {
            //Increment tag count
            dedupedCategories[x - 1][1] = dedupedCategories[x - 1][1] + 1;
        }
        oldCategory = this.title.toString();
    });
    // Loop through all unique tags and write the cloud
    var cloudHtml = "";
    $(dedupedCategories).each(function(i) {
        cloudHtml += "<a href=";
        cloudHtml += dedupedCategories[i][0].link + "><span style=\"font-size:" + ((dedupedCategories[i][1] * 3) + 12) + "px; !important\">";
        cloudHtml += dedupedCategories[i][0].title + "</span></a> \n";
    });
    $('#bloggerCloud').append(cloudHtml);
}
