(function( $ ){
	$.fn.tagCloud = function(word_array) {
		// Reference to the container element
		var $this = this;
		var aleatorio = function (inferior,superior){
			var numPossibilidades = superior - inferior
			var aleat = Math.random() * numPossibilidades
			aleat = Math.floor(aleat)
			return parseInt(inferior) + aleat
		} 
		var corRand = function() {
			var hexadecimal = new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F")
			var cor_aleatoria = "#";
			var posarray = 0;
			for (i=0;i<6;i++){
				posarray = aleatorio(0,hexadecimal.length)
				cor_aleatoria += hexadecimal[posarray]
			}
			return cor_aleatoria
		}
		word_array.sort(function(a, b){
			if (a.frequencia < b.frequencia) {
				return 1;
			} else if (a.frequencia > b.frequencia) {
				return -1;
			} else {
				return 0;
			}
		});
		var arr = []
		var textArr = []
		$.each(word_array, function(index, word) {
			var inner_html = word.url !== undefined ? "<a href='" + word.url + "' class='tagCloud'>" + word.text + "</a></span>" : word.text;
			var weight = Math.round((word.frequencia - word_array[word_array.length - 1].frequencia)/(word_array[0].frequencia - word_array[word_array.length - 1].frequencia) * 4.0) + 1;
			textArr = {text:"<span id='word_" + index + "' class='w"+weight+"' style='margin-left:8px;' title='" + (word.title || "") + "'>" + inner_html + "</span>",ordem:word.text};
			arr.push(textArr);
		})
		arr.sort(function(a, b){
			if (a.ordem.toUpperCase() < b.ordem.toUpperCase()) {
				return -1;
			} else if (a.ordem.toUpperCase() > b.ordem.toUpperCase()) {
				return 1;
			} else {
				return 0;
			}
		});
		$this.append("<b><em>Marcadores:</em></b>")
		$.each(arr, function(k, v) {
			$this.append(v.text)
		})
	}
})(jQuery);
