/*
Blogger Classic Tag Cloud plugin for $
Copyright (c) 2007-2009 Nik Makris (nikmakris.com)
Licensed under the MIT license (http://www.nikmakris.com/projects/BloggerClassicTagCloud/#license) 
Version: 1.0.0 (29/06/2009 21:35:00)
*/
(function($) { // Compliant with jquery.noConflict()
    	
		$.fn.extend({

		tagCloud : function(feedfilename) {


        return this.each(function() {
         
		    var $container = $('<ul></ul>')
			var div = $(this);
			$(this).empty();
            var feedurl = feedfilename;
            
			var tags = new Array();
			var dedupedTags = [];
			
            $.get(feedurl, function(data) {
            //Find each tag and add to an array
            $(data).find('tag').each(function() {
					
				tags[tags.length] = $(this).text();
                });

                tags.sort(caseInsensitiveCompare);
                //Redup tag list and create a multi-dimensional array to store 'tag' and 'tag count'
                var oldTag = '';
                var x = 0;
                $(tags).each(function() {
                    if (this.toString() != oldTag) {
                        //Add new tag
                        dedupedTags[x] = [];
                        dedupedTags[x][0] = this.toString();
                        dedupedTags[x][1] = 1;
                        x++;
                    } else {
                        //Increment tag count
                        dedupedTags[x - 1][1] = dedupedTags[x - 1][1] + 1;
                    }
                    oldTag = this.toString();
                });
                // Loop through all unique tags and write the cloud
                $(dedupedTags).each(function(i) {
                    var $link = $('<a></a>').attr({
						href: '#',
						id:'tag_filter',
						
					}).addClass('filter_off');
					
					var $item = $('<li></li>').attr('id','tag_item');
                    var $span = $('<span></span>').attr('class','tag_count');
                    $link.append(dedupedTags[i][0]);
					$span.append(' (' + dedupedTags[i][1] + ')');
                    $item.append($link);
					$item.append($span);
					$container.append($item);

                });
				div.append($container);
			
            });

        });

    //Used to make JavaScript sort case-insensitive
    function caseInsensitiveCompare(a, b) {
        var anew = a.toLowerCase();
        var bnew = b.toLowerCase();
        if (anew < bnew) return -1;
        if (anew > bnew) return 1;
        return 0;
    }
 }

});

})(jQuery);

