﻿/*
    Cumulus Tags Pluging for jQuery v1.0
    By Mahdi Yousefi [mahdi@tini.ir]
    
    Usage: Just repeat your list with title attribute enabled like:
    <a title="20" href="sample-page.htm">One Item</a>
    
    You can use any tag, but you need to set title to weight.
    Settings:
    --------------------------------------------------------
    minFont: (integer, default: 8)
    minimum font of item
    
    fontLength: (integer, default: 14)
    number of font items. (maximum font = minimum font + font length)
    
    regex: (regular expression, default: /(\d+)/)
    regular expression that get weight from title
    
    tag: (string, default: a)
    tag name, it can be any valid html tag like: span,div,a,li
    tag must have title attribute with weight as value
*/

(function($) {
    $.fn.cumulusTags = function(options) {
        var defaults = {
            fontLength: 14,
            minFont: 8,
            regex: /(\d+)/,
            tag: "a"
        };
        var settings = $.extend(defaults, options);
        var min = 0, max = 0, scaleLength = 0;
        var alist = $(this).find(settings.tag);
        alist.each(function() {
            var mct = settings.regex.exec(this.title);
            if (mct != null && mct.length > 1) {
                $(this).data("count", mct[1]);
                mct = parseInt(mct[1]);
                if (mct < min) {
                    min = mct;
                };
                if (mct > max) {
                    max = mct;
                };
            } else {
                $(this).data("count", -1);
            };
        });
        scaleLength = parseFloat((max - min + 1)) / parseFloat(settings.fontLength);
        alist.each(function() {
            var count = parseInt($(this).data("count"));
            if (count > -1) {
                count = Math.round((parseFloat(count) - min) / scaleLength);
                count += settings.minFont;
                $(this).css("fontSize", count + "pt");
            };
        });
    };
})(jQuery);
