/* ****************************************************************** */
function Manager () {
	var id = Math.round(Math.random() * 1000000);

	var obj = new _Manager(id);
	    obj.name = '_Manager_object_' + id;

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





/* ****************************************************************** */
function _Manager (id) {
	
	this.id			= id;
	this.queries	= new Array();
	this.locations	= new Array();
	this.interval	= 25;
	this.onPause	= false;
	
	this.map;
	this.service;
	this.timer;
	this.counterTag;
}
/* ****************************************************************** */



/* Queries ********************************************************** */
_Manager.prototype.addQuery = function (query) {
	this.queries.push(query);
}

_Manager.prototype.setQueries = function (queries) {
	this.queries = queries;
}

_Manager.prototype.resetQueries = function () {
	this.queries = new Array();
}

_Manager.prototype.checkQueries = function () {
	return (this.queries.length > 0);
}
/* ****************************************************************** */



/* Locations ******************************************************** */
_Manager.prototype.addLocation = function (location) {
	this.locations.push(location);
}

_Manager.prototype.setLocations = function (locations) {
	this.locations = locations;
}

_Manager.prototype.resetLocations = function () {
	this.locations = new Array();
}

_Manager.prototype.checkLocations = function () {
	return (this.locations.length > 0);
}
/* ****************************************************************** */



/* ****************************************************************** */
_Manager.prototype.setInterval = function (interval) {
	this.interval = interval;
}
/* ****************************************************************** */




/* ****************************************************************** */
_Manager.prototype.init = function () {
	var ok = this.checkLocations() && this.checkQueries();
	if (!ok) return( alert("Queries or Locations not specified!") );
	
	this.map = new Map();
	this.map.setColors(this.queries);

	this.service = new Twitter();
	this.service.setQueries(this.queries);
	this.service.setMap(this.map);
	this.service.setLocations(this.locations);
	
	if (!this.map || !this.service) return( alert("Map or Service not initialized!") );
	
	return(1);
}
/* ****************************************************************** */



/* ****************************************************************** */
_Manager.prototype.start = function () {
	return (this.init() && this.update());
}

_Manager.prototype.update = function () {
	this.stopTimer();

	if (this.map.isLoaded() && !this.map.isDrawing() && !this.isPaused()) {
		var added = this.service.update();
	}

	this.startTimer();
}
/* ****************************************************************** */



/* ****************************************************************** */
/* ****************************************************************** */



/* ****************************************************************** */
_Manager.prototype.pause = function () {
	this.onPause = true;
}

_Manager.prototype.isPaused = function () {
	return (this.onPause);
}

_Manager.prototype.toggle = function () {
	this.onPause = (this.isPaused() ? 0 : 1)
	
	this.update();
	
	return(!this.isPaused());
}
/* ****************************************************************** */



/* ****************************************************************** */
_Manager.prototype.startTimer = function () {
	if (!this.timer) {
		eval('var x = function () {' + this.name + '.update();}');
		this.timer = setTimeout(x, this.interval*1000);
	}
}

_Manager.prototype.stopTimer = function () {
	if (this.timer) {
		clearTimeout(this.timer);
	}

	this.timer = null;
}
/* ****************************************************************** */
