SD.teacherTools.addPage('gradebook', 
//Page content
{
	title: 'Gradebook',
	layout: 'fit',
	id: 'gradebook-panel',
	border: true,
	hideMode: 'offsets',
	tools: [{
		id: 'help',
		handler: function(btn) {
			helppopup.loadHelpFile("Gradebook", 500, 455);
		}
	}],
	items: [{
		xtype: 'tabpanel',
		//activeTab: 0,
		border: false,
		items: [
		new gradebookGrid(), new gradebookMissingGrid()]
	}],
	clearAll: function() {
		Ext.getCmp('gradebookcourseid').clearValue();
		var gbGrid = Ext.getCmp('gradebookGrid');
		gbGrid.store.removeAll(true);
		gbGrid.store.fireEvent('datachanged', gbGrid);
		//clear any pre-loaded tasks
		for (var i = gbGrid.colModel.columns.length - 1; i > 0; i--) {
			var col = gbGrid.colModel.columns[i];
			if (parseInt(col.dataIndex)) { //all taskid's are integers
				gbGrid.removeColumn(col.dataIndex);
			}
		}
		Ext.getCmp('gradingMethodCombo').clearValue();
		var missingGradeCourseCombo = Ext.getCmp('missinggradecourseid');
		if (missingGradeCourseCombo.getValue != '') {
			missingGradeCourseCombo.clearValue();
			var missingGrid = Ext.getCmp('gradebook_missing_grid');
			missingGrid.store.removeAll(true);
			gbGrid.store.fireEvent('datachanged', missingGrid);
		}
	},
	listeners: {
		show: function(panel) {
			var grid = Ext.getCmp('gradebookGrid');
			if (grid.gradebookData === undefined) {
				grid.gradebookData = new Object;
				Ext.Ajax.request({
					url: 'schooldynamics_data.json.php',
					params: {
						command: 'getRoundingChoice'
					},
					success: function(r) {
						grid.gradebookData.rounding = (Ext.decode(r.responseText)).rounding;
					}
				});
			}
			panel.items.items[0].setActiveTab(0);
			var courseCombo = Ext.getCmp('gradebookcourseid');
			if (courseCombo.store.find('courseid', courseCombo.getValue()) == -1 && courseCombo.getValue() != '') {
				this.clearAll();
			} else if (courseCombo.getValue() != SchoolDynamics.currentCourseID) {
				courseCombo.setValue(SchoolDynamics.currentCourseID);
			}
		},
		hide: function(panel) {
			var grid = panel.items.items[0].getActiveTab();
			if (grid != undefined) {
				grid.store.save();
			}
		}
	}
},
//Tool Content
{
	regionTitle: 'Gradebook Tools',
	defaults: {
		autoHeight: true,
		cls: 'toolbox',
		frame: true
	},
	items: [{
		title: 'Gradebook Utilities',
		itemId: 'gradebook',
		tools: [{
			id: 'help',
			handler: function(btn) {
				///////////////////////////////////////Finish///////
			}
		}],
		items: [{
			xtype: 'toolboxtext',
			iconCls: 'calculator',
			text: 'Assign a calculation method:',
			listeners: {
				click: function(e, t) {
					console.log(this);
				}
			}
		},
		{
			xtype: 'combo',
			width: 150,
			id: 'gradingMethodCombo',
			editable: false,
			triggerAction: 'all',
			typeAhead: true,
			store: [
				['percent', 'Percent Method'],
				['autocalc', 'Points to Percent'],
				['truepoints', 'True Points']
			],
			listeners: {
				setValue: function(combo, newValue, oldValue) {
					Ext.Ajax.request({
						url: 'teacher_tools/gradebook_data.json.php',
						mode: 'POST',
						params: {
							command: 'setGradingMethod',
							gradingMethod: newValue,
							courseid: Ext.getCmp('gradebookcourseid').getValue()
						},
						success: function(a, b, c) {
							var response = Ext.decode(a.responseText);
							if (response.success) {
								Ext.MessageBox.alert('Success', 'Grading method successfully changed');
							} else {
								Ext.MessageBox.alert('Error', response.reason);
							}
						},
						failure: function() {
							Ext.MessageBox.alert('ERROR', 'Update failed');
						}
					});
				}
			}
		},
		{
			xtype: 'link',
			iconCls: 'clipboard_task_gear',
			itemId: 'assignments',
			text: 'Manage categories',
			handler: function(btn) {
				jsLoader.load('teacher_tools/' + modulizerPath + '/gradebook_mng_assignments_module.js');
				jsLoader.loadedCall(
				function() {
					SD.teacherTools.gradebook.manageAssignments.display();
				});
			}
		},
		{
			xtype: 'link',
			iconCls: 'envelope_manage',
			text: 'Manage automated e-mails',
			handler: function(btn) {
				jsLoader.load('teacher_tools/' + modulizerPath + '/gradebook_email_popup.js');
				jsLoader.loadedCall(
				function() {
					SD.teacherTools.gradebook.manageEmails.display();
				});
			}
		}
/*,
        {
          xtype: 'link',
          iconCls: 'medal_red',
          text: 'Ability evaluations',          
          handler: function(){
            gradebookAbility.show();
          }          
        }*/
		]
	},
	{
		title: 'Student Utilities',
		tools: [{
			id: 'help',
			handler: function(btn) {
				///////////////////////////////////////Finish///////
			}
		}],
		items: [{
			xtype: 'link',
			iconCls: 'display_order',
			text: 'Student display order',
			handler: function() {
				jsLoader.load('teacher_tools/' + modulizerPath + '/gradebook_student_order_popup.js');
				jsLoader.loadedCall(
				function() {
					SD.teacherTools.gradebook.studentOrder.display();
				});
			}
		},
		{
			xtype: 'link',
			iconCls: 'chart_bar',
			text: 'Student summary',
			handler: function() {
				jsLoader.load('teacher_tools/' + modulizerPath + '/gradebook_summary.js');
				jsLoader.loadedCall(
				function() {
					var popup = new SD.teacherTools.gradebook.classes.gradebookSummary.window({
						title: 'Student Summary',
						fieldHeader: 'Tasks',
						limitColumns: [0, 5],
						baseParams: {
							command: 'studentSummary',
							courseid: SchoolDynamics.currentCourseID
						},
						requiredBaseParams: new Array('courseid', 'quarter', 'studentid')
					});
					popup.show();
				});
			}
		}]
	},
	{
		title: 'Extended Access',
		tools: [{
			id: 'help',
			handler: function(btn) {
				helppopup.loadHelpFile("supervise", 500, 455);
			}
		}],
		items: [{
			xtype: 'toolboxtext',
			iconCls: 'magnifier',
			text: 'View the following teacher:'
		},
		new SchoolDynamics.extendedAccessCombo({
			id: 'gradebookteacherid',
			reshowPage: 'gradebook-panel'
		})]
	}],
	listeners: {
		show: function(toolbox) {
			allow = SchoolDynamics.isAccessible("gradebook_weights");
			parentitem = toolbox.items.item('gradebook');
			parentitem.items.item('assignments')[allow ? 'show' : 'hide']();
		}
	}
}
);

