/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var MWEBC2S = function() {
	var channelPacketCallbacks = [];
	var privatePacketCallbacks = [];
	var disconnectCallbacks = [];
	var cometURL = "/MWEB/mweb"; // default MwebServelt location, may be
									// changed with setConnectionPoint before
									// connect() is called
	var request = null;
	var connected = false;
	var _this = null;
	var connectedCB = null;
	return {
		setConnectionPoint : function(connectionPoint) {
			cometURL = connectionPoint;
		},
		reconnect : function() {
			console.log("reconnect>>>");
			request = $.ajax( {
				url : cometURL,
				type : 'GET',
				dataType : 'json',
				data : {
					'connectType' : 'reconnect'
				},
				error : this.errorFunction,
				cache : false,
				ifModified : false,
				success : this.dataArrived
			});
			console.log("reconnect<<<");
		},
		registerChannelPacketCallback : function(callbackFunction) {
			channelPacketCallbacks.push(callbackFunction);
		},
		registerPrivatePacketCallback : function(callbackFunction) {
			channelPacketCallbacks.push(callbackFunction);
		},
		registerDisconnectCallback : function(callbackFunction) {
			disconnectCallbacks.push(callbackFunction)
		},
		dataArrived : function(jsonData) {
			console.log("DataArrived>>> "+jsonData);
			if (jsonData.hasOwnProperty("packetType")) {
				console.log(jsonData);
				if (jsonData.packetType == "channel") {
					for ( var i = 0; i < channelPacketCallbacks.length; i += 1) {
						channelPacketCallbacks[i](jsonData.from,
								jsonData.channelName, jsonData.data);
					}
				} else if (jsonData.packetType == "private") {
					for ( var i = 0; i < privatePacketCallbacks.length; i += 1) {
						privatePacketCallbacks[i](jsonData.from, jsonData.data);
					}
				} else if (jsonData.packetType == "control") {
					if (jsonData.controlType == "connected") {
						if (connectedCB != null) {
							console.log("Connected as " + jsonData.userName);
							connectedCB(jsonData.userName);
						}
					}
				} else {
					console.log("Unknown packet type received: "
							+ jsonData.packetType);
				}
				console.log("DataArrived<<<");
			}
			_this.reconnect();
		},

		errorFunction : function() {
			console.log("errorFunction>>>");
			_this.reconnect();
		console.log("errorFunction<<<");
	},
	connect : function(connectedCallback) {
		_this = this;
		connectedCB = connectedCallback;
		request = $.ajax( {
			url : cometURL,
			type : 'GET',
			dataType : 'json',
			data : {
				'connectType' : 'connect'
			},
			error : this.errorFunction,
			cache : false,
			ifModified : false,
			success : this.dataArrived
		});
		console.log("connect<<<");
	},
	disconnect : function() {
		connected = false;
		request.abort();
	},
	sendPacketToUser : function(usrName, packetObject) {
		console.log("sendPacketToUser>>>");
		var packet = {
			packetType : "private",
			userName : usrName,
			data : packetObject
		};
		$.ajax( {
			url : cometURL,
			type : "POST",
			dataType : "json",
			data : "packet=" + JSON.stringify(packet)
		});
		console.log("sendPacketToUser<<<");
	},
	sendPacketToChannel : function(chanName, packetObject) {
		var packet = {
			packetType : "channel",
			channelName : chanName,
			data : packetObject
		};
		$.ajax( {
			url : cometURL,
			type : "POST",
			dataType : "json",
			data : "packet=" + JSON.stringify(packet)
		});
	}

	}
}

var mwebc2s = MWEBC2S();
