var TagCloud = Class.create();
TagCloud.prototype = {
  initialize: function(element) {
    this.tags = [];
    this.element = $(element);
    this.parse_tree();
    this.randomize_tags();
    this.replace_tags();
  },

  parse_tree: function() {
    var lis = $$('#' + this.element.id + ' ul li');
    for (var i=0; i<lis.length; i++) {
      this.tags.push(new Tag($(lis[i]), i));
    }
  },

  randomize_tags: function() {
    this.tags.sort(function(){return (Math.round(Math.random())-0.5);})
  },

  replace_tags: function() {
    $$('#' + this.element.id + ' ul')[0].remove();
    this.ul = $(Builder.node('ul',this.tags.collect(function(tag){return tag.element}).slice(0,10)));
    this.element.appendChild(this.ul);
  }

};

var Tag = Class.create();
Tag.prototype = {
  initialize: function(ele, rank) {
    this.name = ele.down().innerHTML;
    this.href = ele.down().href;
    this.rank = rank;
    this.element = $(Builder.node('li', [Builder.node('a',{href:this.href}, this.name)]));
    this.element.setStyle({
		  fontSize: 160+this.rank*10 +"%"
		});
  }
};

var tagcloud;
Event.observe(window,'load',function() {
  tagcloud = new TagCloud('tag-cloud');
});
