/*
* jQuery TagCloud 0.1.0
* Copyright (c) 2009 Dave Evartt
* Dual licensed under the MIT and GPL licenses:
*   
*   davee@wehali.com
*
* Creates a tag cloude from a JSON object retrieved from the server.
*
* This plugin was designed to get JSON from Coldfusion, which returns JSON  in 
*	this format:	ROWCOUNT,COLUMNS,DATA
*	
*	ROWCOUNT: number of rows returned
*	COLUMNS: columns within DATA
*	DATA: contains two arrays (tags and freq)
*
*  This plugin loops through the two arrays and builds an ordered list on the 
*  fly and stuffs teh list into the selector
*
*  the cloud is achieved through CSS
<style type="text/css">
.tagCloud {  background-color: #fff; text-align:center; padding:5px; overflow:auto; font-size: 15px; font-family:arial; border:#666666 thin solid }

.tagCloud ol { margin:0; padding:0; }
.tagCloud ol li { display:inline; list-style-type:none; float:left; margin:0 5px;  }
.tagCloud ol li a { text-decoration:none; }
.tagCloud ol li a:hover { text-decoration:underline; cursor:pointer;}		
</style>

* tagCloud is the id of the div to populate the cloud with
* tagCloud ol is the ordered list that is created by the plugin

* NOTE: this is a concatenation of the tagcloud id (in theis case 'tagCloud') and
* the word 'List', hence 'tagCloudList.
*
* thus, the example CSS aboves assumes that you created a div called 'tagCloud'
* and somewhere called this plugin;

$("#tagCloud").tagCloud('url.cfm',{				
	minPercent:80,  //default:100
	maxPercent:200, //default:300
	clickTag:tagCloudClick,
	colors:["blue","darkblue","darkslateblue", "dodgerblue",  "mediumblue","navy","royalblue","slateblue"],
	busy:'<img src="/gfx/loading_big.gif" width="300" height="300">'
});

*
* You must supply a function in  your main application to handle the click event for the tag. You must supply the name of this function trhough the clickTag option (see above)

function tagCloudClick(topic){
	alert(topic);
}

*/



(function($){
	$.fn.tagCloud = function(url,options) {
		var defaults = {
			clickTag: function(id) { alert(id); },
			url:'',
			busy:'<h2>LOADING...<h2>',
			title:"See all pages tagged with ",
			colors: ["blue","darkblue","darkslateblue", "dodgerblue",  "mediumblue","navy","royalblue","slateblue"],
			maxPercent:300,
			minPercent:100
		};//defaults

		var options = $.extend(defaults, options);

		

function showCloud(div){
	var	minPercent=options.minPercent;
	var maxPercent=options.maxPercent;	
	var colors = options.colors;
	var tagList = div.id+"List";


//var colors = new Array("blue","darkblue","darkslateblue", "dodgerblue",  "mediumblue","navy","royalblue","slateblue");

	function getFontSize(min,max,count) {
		return parseInt(minPercent + ((max-(max-(count-min)))* multiplier));
	}

<!--- Different shades of blue --->
	function setRandomColor(){
		return colors[Math.floor(Math.random()*colors.length)];
	}

 
//display the busy indicator
$(div).html(options.busy);



$.getJSON(url, function(data) {
	min =10000;
	max=0;		
/*
//This plugin was designed to get JSON from Coldfusion, which returns JSON  in 
	this format:	ROWCOUNT,COLUMNS,DATA
	
	ROWCOUNT: number of rows returned
	COLUMNS: columns within DATA
	DATA: contains two arrays (tags and freq)
	
	For simplicity, the variable json contains only the two arrays, so if you're using another JSON source, this is where you would change this function to handle it)
*/
	json=data.DATA; 
			
	
	$.each(json.freq, function(i,freq){
	if (freq > max) max=freq;
	if (freq < min) min=freq;
	});
	$(div).html('');

	multiplier = (maxPercent-minPercent)/(max-min);	

	//create list for tag links
	$("<ol>").attr("id", tagList).appendTo(div);

	$.each(json.tag, function(i,tag){
		freq = json.freq[i]
		fontSize=getFontSize(min,max,freq );
		
			//create item
		var li = $("<li>");

		//create link
		clr = setRandomColor();
		$("<a>").text(tag).attr({title: options.title + tag, id: tag }).appendTo(li);

		//set tag size
		li.children().css("fontSize", fontSize+"%");
		li.children().css("color", clr);
		//add to list
		li.appendTo("#"+tagList);
		
	});


//handle the click event
	$('#'+tagList + ' li a').click(function() {
		options.clickTag($(this).attr('id'));
	});

	});



}//showCloud


  
		return this.each(function() {
			showCloud(this);
		}); //this
	}; //tagCloud
})(jQuery);
