
function Twitter(blockId) {
	this.blockId = blockId;
	this.url = 'twitter.php?a=a';
	this.items = new Array();
	this.lastItem = null;
	this.lastValue = null;
	this.index = 0;
	this.count = 2;


	this.buildURL = function(value) {
		return (
			this.url +
			'&query=' + value
		);
	}

	this.refresh = function() {
		this.load(this.buildURL(this.lastValue), true);
	}

	this.reload = function(value) {
		this.lastValue = value;
		this.load(this.buildURL(value), true);
		this.render();
		setTimeout('twitter.onTimer();', 1000); 
	}

	this.load = function(name, sync) {
		ajax_load(
			name,
			'',
			this,
			'handle',
			'text/plain',
			sync,
			'GET',
			'text/plain'
		);
	}

	this.handle = function(content) {
		eval('this.lastItem = ' + content + ';');
		this.render();
	}

	this.onTimer = function() {
		setTimeout('twitter.onTimer();', 10000); 
		this.render();
	}

	this.render = function() {
		var block = document.getElementById(this.blockId);
		if (block == null)
			return;
		this.removeAllChildren(block);
		var html = '';
		var refresh = false;
		for (var i = 0; i < this.count; i++) {
			html += '<p>' + this.lastItem.results[this.index].text + '</p>';
			this.index++;
			if (this.index >= this.lastItem.results.length) {
				this.index = 0;
				refresh = true;
			}
		}
		if (refresh)
			setTimeout('twitter.refresh()', 100);
		block.innerHTML = html;
	}

	this.removeAllChildren = function(node) {
		if (node == null)
			return;
		while (node.childNodes.length > 0)
			node.removeChild(node.firstChild);
	}

}



