function buildTagCloud(tag_data) {
	var threshold = tag_data.threshold || 1;
	threshold -= 1;
	
	var data = tag_data.tags;
	var nt;
	for(var index in data) {
		if(typeof(data[index]) === 'function') { continue; }
		
		var current = data[index];
		var li = jQuery('<li><span></span></li>');
		var a  = jQuery('<a>');
		var taginfo = jQuery('<div class="taginfo ui-corner-all ui-widget-content"><p>Pagina\'s met "' + index + '"</p><ul></ul></div>');
		
		for(var i = 0; i < current.length; i += 1) {
			taginfo.find('ul').append('<li><a href="/' + current[i].alias + '.html">' + current[i].title + '</a></li>');
		}

		a.text(index).attr('href','#').click(function() {
			jQuery('.taginfo').hide();
			jQuery('.taginfo',jQuery(this).parent()).show();
			
			if(nt) { clearTimeout(nt); }
			
			this.blur();
			return false;
		});
		
		taginfo.click(function() {
			jQuery(this).hide();
		}).bind('mouseout',function() {
			
			if(nt) { clearTimeout(nt); }
			nt = setTimeout(function() { jQuery('.taginfo').hide(); }, 1000);
			
		}).appendTo(li.find('span'));
		a.appendTo(li);
		
		// calculate size
		var size = 1;
		if(current.length / 10 < 1) {
			size = (current.length * 1.5) / 10 + 1;
		} else if(current.length / 10 > 2) {
			size = 2;
		} else {
			size = (current.length * 1.5) / 10;
		}
		size *= 1 - (threshold / 10);
		
		size = (size+"").substr(0,3);
		
		li.children('a').css('font-size', size + "em");
		
		jQuery('.tagcloud > ul').append(li);
	}
	
	jQuery('.tagcloud').click(function() { jQuery('.taginfo').hide(); });
}
var TagCloud = {
	getTagsFromPage:function (pageid) {
		var data = jQuery.ajax({
						url:'/_modules/tags/json.php?pageid=' + pageid,
						data:{},
						async:false,
						global:false,
						type:"GET"
					}).responseText;
		data = eval('(' + data + ')').tags;
		var tags = [];
		for(var index in data) { tags.push(index); }
		return tags;
	},
	newsFromTags:function(pageid) {
		var tags = TagCloud.getTagsFromPage(pageid);
		tags = tags.join('+OR+').replace(/\s+/g, '+');
		var url = 'http://news.google.com/news?pz=1&ned=nl_nl&hl=nl&as_scoring=n&as_maxm=8&q=' + tags + '&as_qdr=m&as_drrb=q&as_mind=22&as_minm=7&as_maxd=21&output=rss';
		url = '/_modules/rssreader/proxy.php?' + jQuery.param({url:url});
		return url;
	}
};

jQuery(document).ready(function() {
	
	jQuery.getJSON('/_modules/tags/json.php?callback=?', buildTagCloud);
	
});