var COMMENTS = COMMENTS != null && typeof COMMENTS != 'undefined' ? COMMENTS : {
	pageSize: 15,
	SendComment: function(nodeID)
	{
		if (!COMMENTS.form.getForm().isValid()) { return; }

		//COMMENTS.win.body.mask();
		COMMENTS.win.buttons[0].disable();

		Ext.Ajax.request({
			url: 'post-comment.php',
			method: 'POST',
			form: COMMENTS.form.getForm().getEl().dom,
			timeout: 250000,

			success: function (result, request)
			{
				if (result.responseText.indexOf('succ:') == 0)
				{
					//var wnd = Ext.MessageBox.alert('Success', result.responseText.substring(5));
					var wnd = Ext.MessageBox.show({title: 'Success',msg: result.responseText.substring(5),buttons: Ext.MessageBox.OK, icon: Ext.MessageBox.INFO, modal: false});
					var delayedHide = new Ext.util.DelayedTask(wnd.hide, wnd).delay(5000);
				}
				else Ext.MessageBox.alert('Failed', result.responseText);

				//COMMENTS.win.body.unmask();
				COMMENTS.win.buttons[0].enable();
				COMMENTS.win.close();
			},

			failure: function (result, request)
			{
				Ext.MessageBox.alert('Failed', result.responseText);

				//COMMENTS.win.body.unmask();
				COMMENTS.win.buttons[0].enable();
				COMMENTS.win.close();
			}
		});
	},

	ShowCommentWindow: function(element,nodeID, category)
	{
		COMMENTS.store = new Ext.data.JsonStore({
			url: 'getcomments.php?node=' + nodeID,
			root: 'comments',
			totalProperty: 'totalCount',
			idProperty: 'id',

			fields: ['comment', 'empty']
		});

		Ext.Ajax.request({
			url: 'getcomments.php?node=' + escape(nodeID) + '&get_count=1',
			method: 'GET',
			timeout: 250000,

			success: function (result, request)
			{
				var count = parseInt(result.responseText);

				COMMENTS.pagingBar = new Ext.PagingToolbar({
					pageSize: COMMENTS.pageSize,
					store: COMMENTS.store,
					displayInfo: count != 0,
					displayMsg: 'Displaying comments {0} - {1} of {2}',
					emptyMsg: "<b>No comments to display</b>",

					items:['-']
				});

				COMMENTS.grid = new Ext.grid.GridPanel({
					height: 372-40,

					title:'Comments on “' + category + '”',
					store: COMMENTS.store,
					trackMouseOver:false,
					disableSelection:true,
					loadMask: true,
					stripeRows: true,

					// grid columns
					columns:[
					{
						width: 560,
						header: "Comment",
						dataIndex: 'comment'
					}],

					// paging bar on the bottom
					bbar: COMMENTS.pagingBar
				});

				COMMENTS.hiddenNodeID = new Ext.form.Hidden({id: 'node', value: nodeID});

				COMMENTS.textArea = new Ext.form.TextArea({
									fieldLabel: 'Too high?  Too low?  Share your comments here',
									id: 'comment',
									allowBlank: false,
									anchor: '100%',
									height: '75',
									validateOnBlur: false
								});

				COMMENTS.nameField = new Ext.form.TextField({
									fieldLabel: 'Enter your name (optional)',
									id: 'user',
									allowBlank: true,
									anchor: '100%'
								});

				COMMENTS.form = new Ext.FormPanel({
						width: 610,
						height: 560-40,
						frame: true,
						labelWidth: 75,
						labelAlign: 'top',
						bodyStyle: 'padding:5px;',
						monitorValid: true,

						items: [COMMENTS.grid, COMMENTS.hiddenNodeID, { html: '<br />' }, COMMENTS.textArea, COMMENTS.nameField]
					});

				COMMENTS.win = new Ext.Window({
						layout: 'fit',
						//layout: 'form',
						width: 620,
						height: 610-40,
						resizable: false,
						closeAction: 'close',
						title: 'Tell us what you think!',
						items: [COMMENTS.form],
						shadow: false,
						modal: false,
						buttonAlign: 'center',
						border: false,

						buttons: [{
							text: 'Submit',
							handler: function() { COMMENTS.SendComment(nodeID); }
						}]
					});

				COMMENTS.win.show();

				COMMENTS.store.load({params:{start:0, limit: COMMENTS.pageSize}});

				//when store is loaded, grid is focused
				COMMENTS.store.on('load', function(store, records, options)	{
					COMMENTS.textArea.focus();
				});
				//if data loaded from cache
				setTimeout(function() {COMMENTS.textArea.focus();}, 1000);
			},

			failure: function (result, request)
			{
				Ext.MessageBox.alert('Failed', result.responseText);
			}
		});
	}
};
