Ext.namespace('NGMP','NGMP.data');

NGMP.data.Connection = Ext.extend(Ext.data.Connection, {
	request:function(A) {
		if (A.callback) {
			A.originalCallback = A.callback;
		}
		A.loginCallback = function() {
			var B = A;
			B.callback = A.originalCallback;
			this.request(B);
		};
		A.loginCallbackScope = this;
		A.callback = this.authHandler;
		NGMP.data.Connection.superclass.request.call(this,A);
	},
	authHandler:function(B,D,A) {
		if (!D) {
			Ext.Msg.alert(LANG_error, String.format(LANG_could_not_send_request, A.status));
			Ext.callback(B.originalCallback, B.scope, [B,D,A]);
		} else {
			var C;
			if (A.responseText.substr(0,1) == "{") {
				C = Ext.decode(A.responseText);
			} else {
				C = {};
			}
			if  (C.authError) {
				switch (C.authError) {
					case 'UNAUTHORIZED':
						Ext.Msg.alert(LANG_unauthorized, LANG_unauthorized_tip);
						break;
					case 'NOTLOGGEDIN':
						NGMP.loginDialog.addCallback(B.loginCallback, B.loginCallbackScope);
						NGMP.loginDialog.show();
						break;
					default:
						break;
				}
				return false;
			} else {
				Ext.callback(B.originalCallback, B.scope, [B,D,A]);
			}
		}
		return true;
	}
}); 

NGMP.data.JsonStore = function(A) {
	NGMP.data.JsonStore.superclass.constructor.call(this, A);
	this.on('loadexception', function() {
		if (!this.reader.jsonData || NGMP.jsonAuthHandler(this.reader.jsonData, this.load, this)) {
			var B = LANG_json_data_load_error;
			if (this.reader.jsonData && this.reader.jsonData.feedback) {
				B += '<br><br>'+this.reader.jsonData.feedback;
			}
			Ext.Msg.show({
				title:LANG_error,
				msg:B,
				buttons: Ext.Msg.OK,
				icon:Ext.MessageBox.ERROR
			});
		}
	},this);
};
Ext.extend(NGMP.data.JsonStore,Ext.data.JsonStore);
