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

	com.qwidget.Section = Class.extend({
		model: undefined,
		view: undefined,
		parentQwidget: undefined,
		cqq: com.qwidget.qwids,
		init: function (parentQwidget)
		{
			this.parentQwidget = parentQwidget;
			// if (this.parentQwidget === undefined || this.model === undefined || this.view === undefined)
			// 	com.qwidget.warn('Section.init - parentQwidget: ' + this.parentQwidget + ', model:' + this.model + ', view:' + this.view);
		},
		refresh: function ()
		{
			if (this.model)
				this.model.refresh();
		},
		hide: function ()
		{
			this.view.hide();
		},
		show: function (refresh)
		{
			this.view.show();
			if (refresh)
				this.refresh();
		},
		qwidMap: function (qwidMap)
		{
			this.view.qwidMap(qwidMap);
		},
		isHidden: function ()
		{
			return this.view.isHidden();
		},
		postRefresh: function ()
		{
			
		}
	});
	
	com.qwidget.SurveySection = com.qwidget.Section.extend({
		answerNeverClicked: true,
		postInitRefresh: undefined,
		init: function (node, parentQwidget, question)
		{
			//var openClose = jQuery(_qwid(this.cqq.open_close), node).len(1);
			//openClose.hide();

			var that = this;
			var onClickAnswer = function (eventObject)
			{
				// 'this' is the DOM element...
				if (that.answerNeverClicked)
				{
					that.answerNeverClicked = false;
					that.parentQwidget.submitAnswer();
					//openClose.show();
				}
				com.qwidget.debug('SurveySection.onClickAnswer');
			}
			var onRefreshComplete = function ()
			{
				com.qwidget.debug('SurveySection.onRefreshComplete - questionAnswered: ' + that.questionAnswered());
				if (that.questionAnswered())
				{
					that.view.clickAnswer(that.model.memberAnswerId);
				}
			}
			this.view = new com.qwidget.SurveyView(node, onClickAnswer, onRefreshComplete);
			this.model = new com.qwidget.SurveyModel(question, this.view);
			
			this.postInitRefresh = function () {
				// wire up behaviors...
				jQuery(_qwid(this.cqq.open_close), node).len(1).click(this.onClickOpenCloseFunction());
				jQuery(_qwid(this.cqq.open_close_inbox), node).len(1).click(this.onClickOpenCloseInboxFunction());
			}
			
			this._super(parentQwidget);
		},
		refresh: function ()
		{
			this._super();
			if (this.postInitRefresh)
			{
				this.postInitRefresh();
				this.postInitRefresh = undefined;
			}
		},
		onClickOpenCloseInboxFunction: function ()
		{
			var that = this;
			var f = function (eventObject)
			{
				that.parentQwidget.expand(true);
				that.parentQwidget.openInbox();
				return false;
			}
			return f;
		},
		onClickOpenCloseFunction: function ()
		{
			var that = this;
			var f = function (eventObject)
			{
				if (com.qwidget.activateDebuggerPoint)
				{
					//alert('debugger...');
					debugger;
				} 
				that.parentQwidget.toggle(true);
				return false;
			}
			return f;
		},
		question: function ()
		{
			return this.view.question();
		},
		questionAnswered: function ()
		{
			return this.model.questionAnswered();
		},
		displayMode: function (mode, unreadMessages)
		{
			this.view.displayMode(mode, unreadMessages);
		}
	});
	
	com.qwidget.MenuSection = com.qwidget.Section.extend({
		init: function (node, parentQwidget, isLoggedIn)
		{
			this.view = new com.qwidget.MenuView(node);
			this.model = null;

			// wire up behaviors...
			jQuery(_qwid(this.cqq.menu_loggedIn_logOut), node).len(1, 'MenuSection.init').click(this.onClickLogOut);
			jQuery(_qwid(this.cqq.menu_loggedIn_account), node).len(1, 'MenuSection.init').click(this.onClickAccount);
			var q = jQuery(_qwid(this.cqq.menu_loggedIn_messages), node).len(1, 'MenuSection.init')
			q.click(this.onClickMessages);
			// q.text('Messages ('+com.qwidget.AccountManager.unreadMessageCount()+')');
			jQuery(_qwid(this.cqq.menu_loggedOut_signUp), node).len(1, 'MenuSection.init').click(this.onClickRegister);
			jQuery(_qwid(this.cqq.menu_loggedOut_logIn), node).len(1, 'MenuSection.init').click(this.onClickLogIn);
			
			this._super(parentQwidget);
			
			this.view.loggedIn(isLoggedIn);
		},
		loggedIn: function (loggedIn)
		{
			this.view.loggedIn(loggedIn);
		},
		onClickLogIn: function (eventObject)
		{
			com.qwidget.AccountManager.showLogIn();
			return false;
		},
		onClickLogOut: function (eventObject)
		{
			com.qwidget.AccountManager.logOut();
			return false;
		},
		onClickRegister: function (eventObject)
		{
			com.qwidget.AccountManager.showRegister();
			return false;
		},
		onClickAccount: function (eventObject)
		{
			com.qwidget.AccountManager.showMaintain();
			return false;
		},
		onClickMessages: function (eventObject)
		{
			com.qwidget.MessagesManager.showInbox();
			return false;
		},
		refresh: function ()
		{
			this.unreadMessageCount(com.qwidget.AccountManager.unreadMessageCount());
		},
		unreadMessageCount: function (unreadMessageCount)
		{
			this.view.unreadMessageCount(unreadMessageCount);
		}
	});

	com.qwidget.ResultsSection = com.qwidget.Section.extend({
		init: function (node, parentQwidget, question)
		{
			this.view = new com.qwidget.ResultsView(node);
			this.model = new com.qwidget.ResultsModel(question, this.view);

			this._super(parentQwidget);
		}
	});

	com.qwidget.CommentSection = com.qwidget.Section.extend({
		commentPrompt: 'Explain your answer to others',
		init: function (node, parentQwidget)
		{
			//this.question = question;
			this.view = new com.qwidget.CommentView(node);
			this.model = null;
			
			// wire up behaviors...
			var f = this.onKeydownKeyupCommentFunction();
			jQuery(_qwid(this.cqq.comment_text), node).len(1).keydown(f);
			jQuery(_qwid(this.cqq.comment_text), node).len(1).keyup(f);
			jQuery(_qwid(this.cqq.comment_text), node).len(1).focus(this.onFocusCommentFunction());
			jQuery(_qwid(this.cqq.comment_submit), node).len(1).click(this.onClickCommentSubmitFunction());

			this._super(parentQwidget);
		},
		onKeydownKeyupCommentFunction: function ()
		{
			var that = this;
			return function (eventObject)
			{
				if (this.value.length > 200 && eventObject.keyCode != 8 && eventObject.keyCode != 46)
					return false;
				that.view.charactersRemaining(200 - this.value.length);
				return true;
			};
		},
		onFocusCommentFunction: function ()
		{
			var that = this;
			return function (eventObject)
			{
				if (this.value == that.commentPrompt)
				{
					this.value = '';
					that.view.charactersRemaining(200);
				}
				return true;
			};
		},
		onClickCommentSubmitFunction: function (eventObject)
		{
			var that = this;
			return function (eventObject)
			{
				var qwidMap = {};
				that.view.qwidMap(qwidMap);
				var text = qwidMap[that.cqq.comment_text];
				if (text == null || text.length == 0)
				{
					alert('Please enter a comment.');
					return false;
				}
				if (com.qwidget.AccountManager.isLoggedIn() == false)
				{
					com.qwidget.AccountManager.showLogIn();
					return false;
				}
				that.parentQwidget.submitAnswer();
				return false;
			};
		},
		submitSuccessful: function ()
		{
			this.view.submitSuccessful();
		}
	});

	com.qwidget.ResponsesSection = com.qwidget.Section.extend({
		init: function (node, parentQwidget, question)
		{
			this.view = new com.qwidget.ResponsesView(node, this);
			this.model = new com.qwidget.ResponsesModel(question, this.view);

			this._super(parentQwidget);
			
			jQuery(_qwid(this.cqq.responders_menu_recent), node).len(1).click(this.filterRespondersFunction('recent'));
			jQuery(_qwid(this.cqq.responders_menu_mostAgree), node).len(1).click(this.filterRespondersFunction('agree'));
			jQuery(_qwid(this.cqq.responders_menu_mostDisagree), node).len(1).click(this.filterRespondersFunction('disagree'));
			jQuery(_qwid(this.cqq.responders_submenu_previous), node).len(1).click(this.pageRespondersFunction('previous'));
			jQuery(_qwid(this.cqq.responders_submenu_more), node).len(1).click(this.pageRespondersFunction('next'));
		},
		filterRespondersFunction: function (mode)
		{
			var that = this;
			var filterResponders = function ()
			{
				that.view.selectTab(mode);
				that.model.filter(mode);
				return false;
			};
			return filterResponders;
		},
		pageRespondersFunction: function (mode)
		{
			var that = this;
			var pageResponders = function ()
			{
				that.model.changePage(mode);
				return false;
			}
			return pageResponders;
			
		},
		sendReply: function (replyData, replySent)
		{
			this.parentQwidget.sendReply(replyData, replySent);
		},
		agreeDisagreeFlag: function (responseId, responderId, mode, forceSwap)
		{
			this.model.agreeDisagreeFlag(responseId, responderId, mode, forceSwap);
		}
	});

	com.qwidget.FooterSection = com.qwidget.Section.extend({
		init: function (node, parentQwidget)
		{
			//this.question = question;
			this.view = new com.qwidget.FooterView(node);
			this.model = null;
			this._super(parentQwidget);
			
			var aboutFunction = function ()
			{
				var about = function ()
				{
					var dialog = new com.qwidget.AboutDialog();
					dialog.show();
					return false;
				}
				return about;
			}
			jQuery(_qwid(this.cqq.qwidget_about), node).len(1).click(aboutFunction());
		}
	});










