xmlHttp= new Object();
linklist = new Object();
jobs = 0;
pending = new Array();
gotLinks = false;
function getStats() {
	if (gotLinks) return;
	var list = document.getElementsByTagName("a");
	for (var i=0; i< list.length; i++) {
		if (!list[i].href.match(/ed2k:\/\/\|(\w+)\|([^\|]+)\|([0-9]{9,})\|([0-9A-F]+)\|(?:\S*\|)*/i)) continue;
		var current = RegExp.$4;
		if (linklist[current]) continue;
		linklist[current] = list[i];
	}
	getLinkStats(linklist);
	gotLinks = true;
}
function getLinkStats(links) {
	jobs = 0;
	for (current in links) {
		// Mozilla, Opera, Safari sowie Internet Explorer 7
		if (typeof XMLHttpRequest != 'undefined') {
		    xmlHttp[current] = new XMLHttpRequest();
		}
		if (!xmlHttp[current]) {
		    // Internet Explorer 6 und älter
		    try {
		        xmlHttp[current]  = new ActiveXObject("Msxml2.XMLHTTP");
		    } catch(e) {
		        try {
		            xmlHttp[current]  = new ActiveXObject("Microsoft.XMLHTTP");
		        } catch(e) {
		            xmlHttp[current]  = null;
		            return;
		        }
		    }
		}
		if (xmlHttp[current]) {
			jobs++; 
			pending[pending.length] = current;
		}
	}
	if (jobs > 0) {
		pending.reverse();
		jobQueue();
		
	}
}
function jobQueue() {
	showWaitScreen();
	if (pending.length > 0) {
		var current = pending[pending.length-1];
		pending.length = pending.length-1;
		xmlHttp[current].open('GET', 'getstats.php?hash='+current+"&ref="+document.location.href, true);
		xmlHttp[current].onreadystatechange = statsResult;
		xmlHttp[current].send(null);
	}
	else {
		jobs = 0;
	}
}
function showWaitScreen() {
	if (pending.length > 0) {
		pleaseWait(true,pending.length,jobs);
	}
	else {
		pleaseWait(false);
	}
}
	
function pleaseWait(toggle,todo,total) {
	var id = document.getElementById("pleasewait");
	id.style.position = "absolute";
	if (toggle) {
		id.style.display='block';
		var x,y;
		if (self.innerHeight) // all except Explorer
		{
			x = self.innerWidth;
			y = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
			// Explorer 6 Strict Mode
		{
			x = document.documentElement.clientWidth;
			y = document.documentElement.clientHeight;
		}
		else if (document.body) // other Explorers
		{
			x = document.body.clientWidth;
			y = document.body.clientHeight;
		}
		var screenx = x;
		var screeny = y;
		var x=0,y=0;
		if (self.pageYOffset) // all except Explorer
		{
			x = self.pageXOffset;
			y = self.pageYOffset;
		}
		else if (document.documentElement && document.documentElement.scrollTop)
			// Explorer 6 Strict
		{
			x = document.documentElement.scrollLeft;
			y = document.documentElement.scrollTop;
		}
		else if (document.body) // all other Explorers
		{
			x = document.body.scrollLeft;
			y = document.body.scrollTop;
		}
		var xpos = (screenx)/2+x -250;
		var ypos = (screeny)/2+y -40;
		id.style.left = xpos+'px';
		id.style.top = ypos+'px';
		id.innerHTML = '<center><br><br><b>Please Wait</b><br><br>Processing: '+todo+' of '+total+'<br><br><table border=0 style="border-collapse:collapse; width:100%; padding:0px; margin:0px;"><tr style="border:0px; background:#FFFFFF; padding:0px; margin:0px;"><td style="border:0px; padding:0px; margin:0px;background-image:url(wait.gif);background-repeat:repeat-x; height:7px; width:'+Math.round((total-todo)/total*100+1)+'%;"></td><td></td></tr></table></center>';
		id.style.border = '1px solid black';
		id.style.backgroundColor = 'skyblue';
		id.style.height = '140px';
		id.style.width = '240px';
		id.style.align = 'center';
		id.focus();
	}
	else
		id.style.display="none";
}
function statsResult() {
	for (current in xmlHttp) {
		if (!xmlHttp[current]) continue;
		if (xmlHttp[current].readyState==4 && xmlHttp[current].status==200 && xmlHttp[current].responseText != "") {
			var result = xmlHttp[current].responseText.split("\n");
			var stats = document.createTextNode(" Full: "+result[0]+" Peers: "+result[1]);
			linklist[current].parentNode.insertBefore(stats,linklist[current].nextSibling);
			xmlHttp[current] = null;
			jobQueue();
		}
	}
}
function applyStyles() {
	var list = document.getElementById("bodyContent").childNodes;
	var lastheading;
	for (var i=0; i<list.length; i++) {
		if (list[i].nodeName == "a") {
			lastheading = list[i];
			lastheading.className = lastheading.getAttribute("name");
			continue;
		}
		if (lastheading) {
			lastheading.appendChild(list[i]);
		}
	}
}
