/* ****************************************************************** */
function Twitter () {
	var id = Math.round(Math.random() * 1000000);
	
	var obj = new _Twitter(id);
		obj.name = '_Twitter_object_' + id;

	eval(obj.name + ' = obj');
	
	return(obj);
}
/* ****************************************************************** */





/* ****************************************************************** */
function _Twitter (id) {
	this.id			= id;
	this.results	= 15;
	this.radius		= 1000;
	
	this.base_url	= 'http://search.twitter.com/search.json?lang=en';
	
	this.queries;
	this.mapObject;
	this.locations;
}
/* ****************************************************************** */



/* ****************************************************************** */
_Twitter.prototype.setQueries = function (queries_array) {
	this.queries = queries_array;
}

_Twitter.prototype.setMap = function (mapObject) {
	this.mapObject = mapObject;
}

_Twitter.prototype.setLocations = function (locations) {
	this.locations = locations;
}
/* ****************************************************************** */



/* ****************************************************************** */
_Twitter.prototype.generate_urls = function () {
	
//	var keywords	= '&q="' + queries.join('"+OR+"') + '"';
	var callback	= '&callback=_Twitter_object_' + this.id + '.processResponse';
	var results		= (this.results ? '&rpp='+this.results : '');
	var since_id	= (this.lastID() ? '&since_id=' + this.lastID() : '');
	
	var urls = new Array();
	
	for (var j in this.queries) {
		var keywords = '&q=' + this.queries[j][0];
		for (var i in this.locations) {
			var geocode = '&geocode=' + this.locations[i] + ',' + this.radius + 'mi';
			var url = this.base_url + geocode + callback + keywords + since_id + results;
			
			urls.push(url);
		}
	}

	return(urls);
}
/* ****************************************************************** */



/* ****************************************************************** */
_Twitter.prototype.processResponse = function (response) {
	this.lastID(response.max_id);

	var map = this.mapObject;
	
	map.processResponse(response);
}
/* ****************************************************************** */



/* ****************************************************************** */
_Twitter.prototype.update = function () {

	function rndSort(){return (Math.round(Math.random())-0.5);} 

	var urls = this.generate_urls();
		urls.sort(rndSort);

	for (var i in urls) {
		var url = urls[i];
		
		var script = document.createElement('script');
			script.src = url;
			
		document.getElementsByTagName('head')[0].appendChild(script);
	}

	return(urls.length);
}
/* ****************************************************************** */



/* ****************************************************************** */
_Twitter.prototype.lastID = function (id) {
	if (id && ((!this.last_id) || (id > this.last_id))) this.last_id = id;
	return(this.last_id);
}
/* ****************************************************************** */
