﻿/*
Blogger Classic Tag Cloud plugin for jQuery
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()
    jQuery.fn.BloggerClassicTagCloud = function(ftppath, feedfilename) {



        return this.each(function() {
            var div = jQuery(this);
            var feedurl = ftppath + feedfilename;
            var categories = new Array();
            var dedupedCategories = [];
            jQuery.get(feedurl, function(data) {
                //Find each tag and add to an array
                jQuery(data).find('category').each(function() {
                    categories[categories.length] = jQuery(this).attr('term');
                });
                categories.sort(caseInsensitiveCompare);
                //Dedup tag list and create a multi-dimensional array to store 'tag' and 'tag count'
                var oldCategory = '';
                var x = 0;
                jQuery(categories).each(function() {
                    if (this.toString() != oldCategory) {
                        //Add new tag
                        dedupedCategories[x] = [];
                        dedupedCategories[x][0] = this.toString();
                        dedupedCategories[x][1] = 1;
                        x++;
                    } else {
                        //Increment tag count
                        dedupedCategories[x - 1][1] = dedupedCategories[x - 1][1] + 1;
                    }
                    oldCategory = this.toString();
                });
                // Loop through all unique tags and write the cloud
                jQuery(dedupedCategories).each(function(i) {
                    var $link = jQuery('<a></a>').attr('href', ftppath + 'labels/' + dedupedCategories[i][0] + '.html');
                    var $span = jQuery('<span></span>').attr('style', 'font-size:' + ((dedupedCategories[i][1] * 3) + 12) + 'px;');
                    $span.append(dedupedCategories[i][0]);
                    $link.append($span);
                    div.append($link);
                    div.append(' \n');
                });
            });

        });

    };
    //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);
