if (typeof(calhoun) == "undefined") calhoun={};
if (typeof(calhoun.batchQuery) == "undefined") calhoun.batchQuery = {};

calhoun.batchQuery.updateCountText = function(textNodeId, value) {
	var span = YAHOO.util.Dom.get(textNodeId);
	span.innerHTML = ""+value;
}

calhoun.batchQuery.poll = function(callback, queryId) {
	var log = calhoun.util.getLogger();
	
	var handleResponse = function(response) {
		log("in handle response");
		log(response);

		if( response == null ) {
			// most likely user clicked on a link and so the ajax request 
			// is being canceled
			// silently drop this and stop querying
			return;
		}
		
		if (response.queriesRemaining > 0) {
			// reissue if there are still more queries outstanding
			log("response.queriesRemaining = "+response.queriesRemaining);
			calhoun.batchQuery.poll(callback, queryId);
		}
		
		for(var i=0;i<response.results.length;i++) {
			log("updating");
			var r = response.results[i];
			if (typeof(r.count) == "undefined") {
				if (typeof(r.errorMessage) != "undefined") {
					log("Error message: " + r.errorMessage);
				}
				calhoun.batchQuery.updateCountText(r.pageElementId, "error");
			} else {
				calhoun.batchQuery.updateCountText(r.pageElementId, r.count);
			}
		}
	};
	
	callback(queryId, handleResponse);
}

