define(['emitter/render'], function(RenderEmitter){
	var _points = [];

	var _addPoint = function(drifterModel){
		_points.push(drifterModel);
		RenderEmitter.trigger('drifter-list', _points);
		RenderEmitter.trigger('add-map-marker', drifterModel);
	}

	var _removePoint = function(drifterModel){
		var id = drifterModel.getId(), removeIndex;
		for(var i=0, p; removeIndex = i, p = _points[i++];) {
			if (p.getId() == id) {
				_points.splice(removeIndex, 1);
				break;
			};
		}
		RenderEmitter.trigger('drifter-list', _points);
		RenderEmitter.trigger('remove-map-marker', drifterModel);
	}

	var _updateStatus = function(data){
		var id = data.id, status = data.status;
		for(var i=0, p; p = _points[i++];) {
			if (p.getId() == id) {
				p.setStatus(status);
				if(status == 'fail') p.setErrorMessage(data.message);
				break;
			};
		}
		RenderEmitter.trigger('drifter-list', _points);
	}

	var _updateAllStatus = function(data){
		var status = data.status;
		for(var i=0, p; p = _points[i++];) {
			p.setStatus(status);
		}
		RenderEmitter.trigger('drifter-list', _points);
	}

	var _clear = function(data){
		_points = [];
		RenderEmitter.trigger('drifter-list', _points);
		RenderEmitter.trigger('clear-drifters');
	}

	return {
		addPoint: _addPoint,
		removePoint: _removePoint,
		updateStatus: _updateStatus,
		updateAllStatus: _updateAllStatus,
		clear: _clear
	}
});