self.interval = self.interval || 1000;
self.tick = self.tick || function() {};
self.config = self.config || {};

(new function() {
	var
		me = this,
		timer,
		cmds = [],
		execRun = false;
	
	function update() {
		if (!!timer) self.clearTimeout(timer);
		self.tick(function(result) {
			self.postMessage(result);
		});
		timer = self.setTimeout(update, self.interval);
	}
	
	function exec() {
		if (!!cmds.length) {
			execRun = true;
			var cmd = cmds.shift();
			me[cmd.cmd].apply(this,cmd.params);
		} else {
			execRun = false;
		}
	}
	
	this.start = function() {
		update();
		exec();
	}
	
	this.stop = function() {
		if (!!timer) self.clearTimeout(timer);
		exec();
	}
	
	this.loadConfig = function(name) {
		AJAX.request("../../panels/departure/config/"+name+".json").then(function(data) {
			config = data.response;
			if (isObject(config) && isArrayNotEmpty(config['stops'])) {
				config['stops_converted'] = [];
				for (var i = 0; i < config['stops'].length; ++i) {
					config['stops_converted'].push({url:'http://vrrf.finalrewind.org/'+config['stops'][i].split(':').join('/')+'.json',method:'GET',params:{frontend:'json'}});
				}	
			}
			exec();
		},function() {
			exec();
		});
	}
	
	self.onmessage = function(e) {
		cmds.push(e.data);
		if (!execRun) exec();
	}.bind(this);
	
}());