var postcount = 0;
var postmax = 400;
var posttotal = 0;
var tmap = new Object();
var tvals = new Object();
var vstr = "";
var tdiv = document.getElementById("tagcloud");
tdiv.innerHTML = "Loading cloud...";

if (window.XMLHttpRequest)
{
	document.xhttp=new XMLHttpRequest();
}
else // IE 5/6
{
	document.xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

var loc = document.location.href.split("/")[2];
document.xhttp.open("GET","http://" + loc + "/api/read?num=50&start=" + postcount,true);
document.xhttp.onreadystatechange = proc;
document.xhttp.send();

function proc(aEvt) {  

if (document.xhttp.readyState == 4)
{
	if(document.xhttp.status == 200)  
	{
		postcount += 50;
		xmlDoc=document.xhttp.responseXML;



		var tags = xmlDoc.getElementsByTagName("tag");
		var posts = xmlDoc.getElementsByTagName("posts");
		posttotal = posts[0].getAttribute("total") - 0;


		for(var t = 0; t < tags.length; t++)
		{
			var mval = tags[t].textContent.replace(" ", "");
			if(tmap[mval])
			{
				tmap[mval]++;
			}
			else
			{
				tmap[mval] = 1;
				tvals[mval] = tags[t].textContent;
			}
		}

		if(postcount < Math.min(posttotal,postmax))
		{
			document.xhttp.open("GET","http://" + loc + "/api/read?num=50&start=" + postcount,true);
			document.xhttp.send();
		}
		else
		{
			for(var m in tmap)
			{
				if(tmap[m] > 1)
				{
					vstr += "<a href='/tagged/"+tvals[m]+"' style='font-size:"+(tmap[m] + 8)+"px;'>"+tvals[m]+"</a> ";
				}
			}
			tdiv.innerHTML = vstr;
		}
	}
	else
	{  
		tdiv.innerHTML = "tag cloud failure";
	}
   }  
}
