/**
 *  PlugIn for JQuery to create tagCloud from json-request
 *  Author: XK @ gcs GmbH Salzburg
 *  contact: xk@gcs-salzburg.at
 *  version: 0.1 ALPHA
 */

( function( $ )
{
	$.fn.tagcloud=function( json_path )
	{
		var debug = true; //debug mode on
		var source = this; //store source control
		
		try
		{
			if ( json_path==null || json_path=="" )
				return this; //maintain jquery chainability
		
			//generate a unique id for the taglist element (<ul>)
			var tagsource = this.attr( "id" )+"_taglist_"+Math.floor( Math.random( )*1001 );
			
			//add the class tagcloud to container
			source.addClass( "tagcloud" );
			
			$.getJSON( json_path, function( data ) 
			{
				//create list for tag links  
				$( "<ul>" ).attr( "id", tagsource ).addClass( "tagcloud_group" ).appendTo( source );
				
				$.each( data, function( name, href ){
	
					//create item  
					var li = $( "<li>" );
					
					//add class
					li.addClass( "tagcloud_item");
					li.addClass( "tagcloud_item_" + (Math.floor( ( Math.random()*4 ) )+1));
					  
					//create link  
					$( "<a>" ).text( name ).attr( {title: href, href: href} ).appendTo( li );  
				
					//add to list
					li.appendTo( "#"+tagsource );
				} );
	        });
		}
		catch ( ex )
		{
			if ( debug )
				console.log( "jquery.tagcloud: "+ex );
		}
	}
} )( jQuery );
