	if(!com) var com = {};
	if(!com.qwidget) com.qwidget = {};

  	com.qwidget.MessagesManager = {
		inboxMessage: undefined,
		inboxDetailMessage: undefined,
		outboxMessage: undefined,
		outboxDetailMessage: undefined,
		cqq: com.qwidget.qwids,
		init: function ()
		{
		},
		showInbox: function ()
		{
			if (!this.inboxMessage) this.inboxMessage = new com.qwidget.InboxMessages();
			
			this.inboxMessage.show();
		},
		showInboxFunction: function ()
		{
			var that = this;
			var f = function () {that.showInbox(); return false;}
			return f;
		},
		showOutbox: function ()
		{
			if (!this.outboxMessage) this.outboxMessage = new com.qwidget.OutboxMessages();
			
			this.outboxMessage.show();
		},
		showOutboxFunction: function ()
		{
			var that = this;
			var f = function () {that.showOutbox(); return false;}
			return f;
		},
		showInboxDetail: function (messageId, messageRead)
		{
			if (!this.inboxDetailMessage) this.inboxDetailMessage = new com.qwidget.InboxDetailMessages();

			this.inboxDetailMessage.show(messageId, this.inboxMessage.model.qwidMap);
			if (!messageRead)
				this.markMessageRead(messageId);
		},
		showOutboxDetail: function (messageId)
		{
			if (!this.outboxDetailMessage) this.outboxDetailMessage = new com.qwidget.OutboxDetailMessages();

			this.outboxDetailMessage.show(messageId, this.outboxMessage.model.qwidMap);
		},
		markMessageRead: function (messageId)
		{
			var credentials = com.qwidget.AccountManager.credentials();
			var dataOut =
			{
				memberId: 	credentials.memberId,
				email: 		credentials.memberEmail,
				password: 	credentials.memberPassword,
				messageId: 	messageId
			};
			var dataPass = {};
			var url = com.qwidget.servicesUrl + '/discussion/message/mark_read.jsp';
			com.qwidget.ajax(url, dataOut, dataPass,
				function (dataIn, dataPass)
				{
					com.qwidget.log('MessagesManager.markMessageRead - messageId: '+ messageId);
					var unreadMessageCount = com.qwidget.AccountManager.unreadMessageCount() - 1;
					if (unreadMessageCount < 0)
						unreadMessageCount = 0;
					com.qwidget.AccountManager.unreadMessageCount(unreadMessageCount);
					com.qwidget.QwidgetManager.unreadMessageCount(unreadMessageCount);
				}
			);
		},
		archiveMessage: function (messageId, mode, callback)
		{
			var credentials = com.qwidget.AccountManager.credentials();
			var dataOut =
			{
				memberId: 	credentials.memberId,
				email: 		credentials.memberEmail,
				password: 	credentials.memberPassword,
				messageId: 	messageId
			};
			var dataPass = 
			{
				box: (mode == this.cqq.messages_inbox ? 'inbox' : 'outbox')
			};
			var url = com.qwidget.servicesUrl + (mode == this.cqq.messages_inbox || mode == this.cqq.messages_inboxDetail ? '/discussion/message/archive_inbox.jsp' : '/discussion/message/archive_sent.jsp');
			com.qwidget.ajax(url, dataOut, dataPass,
				function (dataIn, dataPass)
				{
					com.qwidget.log('MessagesManager.archiveMessage - messageId: '+ messageId + ' ('+dataPass.box+')');
					if (callback)
						callback();
				}
			);
		},
		replyToMessage: function (responseId, threadId, toMemberId, subject, body, inReplyToId, callback)
		{
			// DRY - QwidgetManager.sendReply should use this function...
			if (!body || body.length == 0 || body == null)
			{
				alert("Please enter a reply.")
				return;
			}
			if (com.qwidget.AccountManager.isLoggedIn())
			{
				var credentials = com.qwidget.AccountManager.credentials();
				var dataOut =
				{
					fromMemberId: 	credentials.memberId,
					email: 			credentials.memberEmail,
					password: 		credentials.memberPassword,
					responseId: 	responseId,
					threadId: 		threadId,
					toMemberId: 	toMemberId,
					body: 			body,
					subject: 		subject,
					inReplyToId: 	inReplyToId
				};
				var dataPass = {};
				
				if (callback == undefined)
				{
					callback = function ()
					{
						com.qwidget.log('MessagesManager.replyToMessage...');
					}
				}
				
				var that = this;
				com.qwidget.ajax(com.qwidget.servicesUrl + '/discussion/message/create.jsp', dataOut, dataPass, callback);
			}
		}
	};

	com.qwidget.MessagesBase = Class.extend({
		view: undefined,
		model: undefined,
		cqq: com.qwidget.qwids,
		init: function ()
		{
		},
		toggleShowArchived: function ()
		{
			return this.model.toggleShowArchived();
		},
		pageMessages: function (mode)
		{
			this.model.changePage(mode);
			return false;
		},
		refresh: function ()
		{
			this.model.refresh();
		}
	});
	com.qwidget.InboxMessages = com.qwidget.MessagesBase.extend({
		init: function ()
		{
			this.view = new com.qwidget.InboxMessagesDialog(this);
			this.model = new com.qwidget.InboxMessagesModel(this.view);
		},
		show: function ()
		{
			this.view.show();
			this.model.refresh();
		}
	});
	com.qwidget.InboxDetailMessages = com.qwidget.MessagesBase.extend({
		init: function ()
		{
			this.view = new com.qwidget.InboxDetailMessagesDialog(this);
			// this.model = new com.qwidget.InboxMessagesModel(this.view);
		},
		show: function (messageId, qwidMap)
		{
			this.view.show(messageId);
			this.view.refresh(qwidMap);
			// this.model.refresh();
		}
	});
	com.qwidget.OutboxMessages = com.qwidget.MessagesBase.extend({
		init: function ()
		{
			this.view = new com.qwidget.OutboxMessagesDialog(this);
			this.model = new com.qwidget.OutboxMessagesModel(this.view);
		},
		show: function ()
		{
			this.view.show();
			this.model.refresh();
		}
	});
	com.qwidget.OutboxDetailMessages = com.qwidget.MessagesBase.extend({
		init: function ()
		{
			this.view = new com.qwidget.OutboxDetailMessagesDialog(this);
			// this.model = new com.qwidget.OutboxMessagesModel(this.view);
		},
		show: function (messageId, qwidMap)
		{
			this.view.show(messageId);
			this.view.refresh(qwidMap);
			// this.model.refresh();
		}
	});
	
