
var test = 0;

(function() {
	var TUIO = function() {
		// Listener class

		this.Listener = function(impl) {
			if (impl != undefined) {
				// override original method implementation
				for (var key in impl) {
					this[key] = impl[key];
				}
			}
		}
		this.Listener.prototype = {
			object_add:    function(data) { },
			object_update: function(data) { },
			object_remove: function(data) { },
			cursor_add:    function(data) { },
			cursor_update: function(data) { },
			cursor_remove: function(data) { },
			string_add:    function(data) { },
			string_update: function(data) { },
			string_remove: function(data) { }
		}

		// Instance variables

		this.objects = [];
		this.cursors = [];
		this.strings = [];

		this._data = {};

		this._default_listener = new this.Listener();
		this._listeners = [this._default_listener];

		this._connector = undefined;

	};
	TUIO.prototype = {
		start: function(name) {
			var c = this._connector;
			if (c != undefined) {
				if (c.start != undefined) {
					c.start();
				}
			}
		},

		stop: function() {
			var c = this._connector;
			if (c != undefined) {
				if (c.stop != undefined) {
					c.stop();
				}
			}
		},

		setConnector: function(connector) {
			this._connector = connector;
		},
		
		addListener: function(listener) {
			this._listeners.push(listener);
		},
		removeListener: function(listener) {
			this._listeners.splice(this._listeners.indexOf(listener), 1);
		},

		_invoke: function(method, data) {
			var i, len = this._listeners.length;
			for (i=0; i<len; i++) {
				var listener = this._listeners[i];
				listener[method](data);
			}
		},

		cursorCallback: function(type, sid, fid, x, y, z, angle) {
			if(type >= 6)
				return;
			
			var data;
			
			if ((type != 0) && (type != 3)) {
				data = this._data[sid];
			}
			else {
				data = {
					sid: sid,
					fid: fid,
					path: []
				}
				this._data[sid] = data;
			}

			data.path.push([x, y, z]);
	
			data.x = x;
			data.y = y;
			data.z = z;
			
			if (type < 3) {
				data.angle = angle;
			}
	
			switch (type) {
				case 0: 
					this.objects.push(data);
					this._invoke('object_add', data);
					break;
	
				case 1: 
					this._invoke('object_update', data);
					break;
	
				case 2: 
					this.objects.splice(this.objects.indexOf(data), 1);
					this._invoke('object_remove', data);
					break;
	
				case 3: 
					this.cursors.push(data);
					this._invoke('cursor_add', data);
					break;
	
				case 4: 
					this._invoke('cursor_update', data);
					break;
	
				case 5: 
					this.cursors.splice(this.cursors.indexOf(data), 1);
					this._invoke('cursor_remove', data);
					break;
	
				default:
					break;
			}
	
			if ((type == 2) || (type == 5)) {
				delete this._data[sid];
			}
		},
		
		stringCallback: function(type, sid, code) {
			if(type < 6)
				return;
			
			var data;
			
			if ((type != 6)) {
				data = this._data[sid];
			}
			else {
				data = {
					sid: sid,
					code: code
				}
				this._data[sid] = data;
			}
			
			//data.code = code;
			
			switch (type) {
				case 6: 
					if(this.strings != null && this.strings.length <= 0)
					{
						this.strings.push(data);
						this._invoke('string_add', data);
						test++;
						//alert(test);
					}
					break;
	
				case 7: 
					this._invoke('string_update', data);
					break;
	
				case 8: 
					//var str = "";
					//for(var j = 0 ; j < this.strings.length ; j++)
						//str += "(" + this.strings[i].sid + ")" + this.strings[i].code + " ";
					//alert(str);
					//this.strings.splice(this.strings.indexOf(data), 1);
					this.strings.length = 0;
					this._invoke('string_remove', data);
					test--;
					alert(test);
					break;

				default:
					break;
			}
	
			if ((type == 8)) {
				delete this._data[sid];
			}
		},

		// Convenient callbacks set

		object_add:    function(f) { this._default_listener.object_add = f;    },
		object_update: function(f) { this._default_listener.object_update = f; },
		object_remove: function(f) { this._default_listener.object_remove = f; },
		cursor_add:    function(f) { this._default_listener.cursor_add = f;    },
		cursor_update: function(f) { this._default_listener.cursor_update = f; },
		cursor_remove: function(f) { this._default_listener.cursor_remove = f; },
		string_add:    function(f) { this._default_listener.string_add = f;    },
		string_update: function(f) { this._default_listener.string_update = f; },
		string_remove: function(f) { this._default_listener.string_remove = f; }

	};
	this.tuio = new TUIO(); 
})();

