	var myTagcloud = {};
	var myOptions = ['keyword','work','artist'];
	var myOptionsName = [];
	myOptionsName['keyword'] = 'quick search tags';
	myOptionsName['work'] = 'work titles';
	myOptionsName['artist'] = 'person names';
	var myType = "";
		
	// init
	$(document).ready(function () {
		myType = myOptions[Math.floor(Math.random() * myOptions.length)];
		
		// temporary debug overwrite for myType:
		// myType ='keyword';
		
		$.ajaxSetup({
			timeout: 10000,
			scriptCharset: "utf-8" ,
			contentType: "application/json; charset=utf-8"
		});
		
		getCloud(myType);
	})
			
	function getCloud(t)
	{
		myType = t;
		$.getJSON("/fileadmin/proxy.php?url=" + escape(repositoryurl + "soa/?service=tagcloud/Get_Most_Popular_Tags&type=" + t + "&limit=10"),"param",
			function(data)
			{
				myTagcloud = data;
				showCloud();
			}
		);
	}
	
	function sortTagCloud(a,b) {
		if (a.subject<b.subject) {return -1;}
		if (a.subject>b.subject) {return 1;}
		return 0;
	}
	
			
	function showCloud()
	{
		var myHtml = "<h2>Popular " + myOptionsName[myType] + "</h2>";
		myHtml += "<br /><p>";
		
		var maxTagcount = 0;
		var minTagcount = 0;
		
		// get the max count
		for (var item in myTagcloud)
		{
			// myTagcloud[item].count = Math.floor(Math.random() * 50);
			
		    if (myTagcloud[item].count > maxTagcount)
		    {
		    	maxTagcount = 1 * myTagcloud[item].count;
		    }
		    if ((myTagcloud[item].count < minTagcount) || (minTagcount == 0))
		    {
		    	minTagcount = 1 * myTagcloud[item].count;
		    }
		}
		
		// alphabetically sort
		myTagcloud.sort(sortTagCloud);
		
		//display the cloud
		for (var item in myTagcloud)
		{
		    tagsize = Math.round(5 * (myTagcloud[item].count/maxTagcount));
		    if (minTagcount == maxTagcount) { tagsize = 2; }
			
			if(myType == 'keyword') {
				searchparam = "&frm_searchtab=all&frm_searchword=" + myTagcloud[item].subject
			} else if(myType == 'work') {
				searchparam = "&frm_searchtab=worktitle&&frm_searchword=" + myTagcloud[item].subject
			} else {  
				// artist
				searchparam = "&&frm_searchtab=person&frm_searchword=" + myTagcloud[item].subject
			}
			
		    myHtml += "<a href='" + colorSize("/index.php?id=search" + searchparam  + "' class='tagsize" + tagsize) + "'>" + myTagcloud[item].subject + "</a><sub>" + myTagcloud[item].count + "</sub> ";
		}
		
		myHtml += "</p><br /><p>Other tagclouds are: ";
		
		// the pulldown
		for (var item in myOptions)
		{
			if (myOptions[item] == myType)
			{
				myHtml += '<a href="Javascript:getCloud(\''+myOptions[item]+'\');">' + myOptionsName[myOptions[item]] + '</a>';
			}
			else
			{
				myHtml += '<a href="Javascript:getCloud(\''+myOptions[item]+'\');">' + myOptionsName[myOptions[item]] + '</a>';
			}
			if (item < myOptions.length - 2)
			{
				myHtml += ', ';
			}
			else if (item < myOptions.length - 1)
			{
				myHtml += ' and ';
			}
			else
			{
				myHtml += '.</p>';
			}
		}
		
		$("#popular_tags").html(myHtml);
	}