manageAssignmentsGrid = function(){
  var assignSummary = new Ext.ux.grid.GridSummary();
   var colSelect = new Ext.grid.CheckboxSelectionModel();
    totalPercentage=0;
    var columns = [
        colSelect,
        {header: "Assignment Category", id: 'assignCatCol',sortable: true, dataIndex: 'category'},
        {header: "Drop Lowest Grade",sortable: true, dataIndex: 'droplowest', width: 110,    
        renderer: function(value, metaData, record, rowIndex, colIndex, store) {
          if (record.data.drop != true) {
            metaData.css = 'notEditable';
          }
          return value;
        }
        , editor: new Ext.form.TextField({})        
        },
        {header: "Percentage", sortable: true, dataIndex: 'percentage', width: 100, editor: new Ext.form.NumberField({}), renderer: function(v, meta){ meta.css = 'textAlignRight'; return ((v!=undefined)?v+'%':'') },summaryType: 'sum', summaryRenderer: function(v){return '<span class="assignSummaryPercent' + ((v !=100)?'':'100') + '">' + v + '%</span>'}}
    ];
      
    manageAssignmentsGrid.superclass.constructor.call(this, {
        hideMode: 'offsets',
        tbar:[
          {
            xtype: 'button',
            text: 'Remove Category',
            iconCls: 'delete',
            handler : function(){
              records = this.ownerCt.ownerCt.selModel.getSelections();
              for (i = 0; i < records.length; i++){
                this.ownerCt.ownerCt.ownerCt.sendRowBack(records[i]);
              }
                Ext.getCmp('manageAssignmentsCatGrid').store.sort('category', 'ASC');
            }
          },
          {
            xtype: 'tbseparator'
          },{
            xtype:'button',
            text: 'copy',
            iconCls: 'copy',
            menu: [
              {
                 text: 'Copy to all other grading periods',
                 iconCls: 'copy',
                 handler: function(){ 
                    Ext.Ajax.request({
                    url: 'teacher_tools/gradebook_data.json.php',
                    method: 'POST',
                    params: {command: 'copygradingperiods', courseid: Ext.getCmp('assigncourses').getValue(), quarter: Ext.getCmp('assignquarter').getValue() },
                    success: function() {
                    },
                    failure: function() {
                        Ext.Msg.alert('Status', 'Unable to copy weights to other quarters.');
                    }
                  });
                 }
              },
              {
                 text: 'Copy to selected grading period',
                 iconCls: 'copy',
                 handler: function(){ 
                    Ext.Ajax.request({
                      url: 'teacher_tools/gradebook_data.json.php',
                      method: 'POST',
                      params: {command: 'setlowhighgrades', lowgrade: Ext.getCmp('lowgrade').getValue(), highgrade: Ext.getCmp('highgrade').getValue(), lowsetting: Ext.getCmp('lowsetting').getValue(), highsetting: Ext.getCmp('highsetting').getValue(), lowmessage: Ext.getCmp('lowmessage').getValue(), highmessage: Ext.getCmp('highmessage').getValue()},
                      success: function() {
                      },
                      failure: function() {
                          Ext.Msg.alert('Status', 'Unable to set low grades or high grades.');
                      }
                    });         
                  }
              },
              {
                 text: 'Copy to ALL my courses and grading periods',
                 iconCls: 'copy',
                 handler: function(){ 
                    Ext.Ajax.request({
                      url: 'teacher_tools/gradebook_data.json.php',
                      method: 'POST',
                      params: {command: 'setlowhighgrades', lowgrade: Ext.getCmp('lowgrade').getValue(), highgrade: Ext.getCmp('highgrade').getValue(), lowsetting: Ext.getCmp('lowsetting').getValue(), highsetting: Ext.getCmp('highsetting').getValue(), lowmessage: Ext.getCmp('lowmessage').getValue(), highmessage: Ext.getCmp('highmessage').getValue()},
                      success: function() {
                      },
                      failure: function() {
                          Ext.Msg.alert('Status', 'Unable to set low grades or high grades.');
                      }
                    });         
                 }
              }
            ]
          }
        ],
        store: new Ext.data.Store({
            url : 'teacher_tools/gradebook_data.json.php',
            baseParams: {command: 'getCourseCategories'},
            reader: new Ext.data.JsonReader({root: 'assignedCategories'}, [            
              {name: 'select'},
              {name: 'category'},
              {name: 'numdrop'},
              {name:'drop'},
              {name: 'percentage'}
            ]),
              autoLoad : false,
              sortInfo: {
                field: 'category',
                direction: 'ASC'
              },
              writer: new Ext.data.JsonWriter({
                  writeAllFields: true // write all fields, not just those that changed
              })
        }),
        autoExpandColumn: 'assignCatCol',
        columns: columns,
        sm: colSelect,
        plugins:[assignSummary],
        autoScroll: true,  
        id: 'manageAssignmentsGrid',
        title: 'Assigned Categories',
        border: true,
        width: 500,
        listeners: {
          render: function(grid){
             var firstGridDropTargetEl =  this.getView().scroller.dom;
             var firstGridDropTarget = new Ext.dd.DropTarget(firstGridDropTargetEl, {
                  ddGroup    : 'maDDGroup',
                  notifyDrop : function(ddSource, e, data){
                    for (row in data.selections){
                      if (typeof(data.selections[row]) != 'function') {
                        grid.ownerCt.chooseRow(data.selections[row]);
                      }
                    }
                  return true
                  }
            });
          },
          beforeedit: function(object) {
            if (object.column != 2) {
              return true;
            }
            if (object.record.get('drop')!=true) {
              return false; 
            } else {
              return true;
            }
          }          
        }
    });
}

Ext.extend(manageAssignmentsGrid, Ext.grid.EditorGridPanel); 
