/*
MIT license.
Schaffer Krisztián
http://woophoo.com
Originally based on DynaCloud v3 by
Johann Burkard

Extended by Adam Theriault
--Added custom link options and  made a rather 
hacky job of having size dependent on external styles.

Usage:
------
Call it on a jQuery object created from one element and pass an array of
objects with "tag", "count" and "link" attributes. E.g.:
var tags = [{tag: "computers", count: 56, link: "http://example.com"}, {tag: "mobile" , count :12, link:"http://www.foo.bar" }, ... ];
$("#tagcloud"}.tagCloud(tags);

Configuration:
--------------
Optionally you can pass a configuration object to tagCloud as the second
parameter. Allowed settings are:
sort: Comparator function used for sort the tags before displaying, or false if
   no sorting needed.
   default: sort by tag text using
          function (a, b) {return a.tag < b.tag ? -1 : (a.tag == b.tag ? 0 : 1)
click: Event handler which receives the tag name as first parameter
   and the original click event as second. The preventDefault() is called
   on event before passing so don't bother.
   default: does nothing:
          function(tag, event) {}
maxFontSizeEm: Size of the largest tag in the cloud in css 'em'. The smallest
   one's size is 1em so this value is the ratio of the smallest and largest
   sizes.
   default: 3

Styling:
--------
 The plugin adds the "tagcloudlink" class to the generated tag links. Note that
an "&nbsp;" is generated between the links too.
*/
jQuery.fn.tagCloud = function(cl, givenOptions) { //return this.each( function() { //like a real jQuery plugin: run on each element
   if (!cl || !cl.length) {
      return this;
   };
   // Default settings:
   var defaults = {
      sort: function (a, b) {return a.tag < b.tag ? -1 : (a.tag == b.tag ? 0 : 1)},//default sorting: abc
      click: function(tag) {},
      maxFontSizeEm: 4
   };
   var options = {};
   jQuery.extend(options, defaults, givenOptions);

   // calculating the max and min count values
   var max = -1;
   var min = cl[0].count;
   $.each(cl, function(i, n) {
      max = Math.max(n.count, max);
      min = Math.min(n.count, min);
   });
   if (options.sort) {
      cl.sort(options.sort);
   };
   //Normalization helper
   var diff = ( max == min ? 1    // if all values are equal, do not divide by zero
                           : (max - min) / (options.maxFontSizeEm - 1) ); //optimization: Originally we want to divide by diff
                           // and multiple by maxFontSizeEm - 1 in getNormalizedSize.
   function getNormalizedSize(count) {
      return 1 + (count - min) / diff;
   };
   // Generating the output
   this.empty();
   for (var i = 0; i < cl.length; ++i) {
      var tag = cl[i].tag;
      var link = cl[i].link;
      var size = getNormalizedSize(cl[i].count);
      var currStyle;
      
      if(size == 1) {
        currStyle = 'cloud1';
      } else if(size < 1.5) { 
        currStyle = 'cloud2';
      } else if(size < 2) {
        currStyle = 'cloud3';
      } else if(size < 2.5) {
        currStyle = 'cloud4';
      } else if(size < 3) { 
        currStyle = 'cloud5';
      } else if(size < 3.5) {
        currStyle = 'cloud6';
      } else if(size < 4) {
        currStyle = 'cloud7';
      } else {
        currStyle = 'cloud8';
      };

      var tagEl = jQuery('<a href="' + link + '" class="' + currStyle + '">' + tag + '<\/a>')
                  .data('tag', tag);
      if (options.click) {
         tagEl.click(function(event) {
        //    event.preventDefault();
            options.click(jQuery(event.target).data('tag'), event);
         });
      };
      this.append(tagEl).append(" ");
   };
   return this;
//})
}
