SD.calendar.addPopup('eventApproval', new Ext.Window({
	layout: 'fit',
	title: 'Event Approval',
	width: 700,
	height: 500,
	closeAction: 'hide',
	modal: true,
	tools: [{
		id: 'help'
	}],
	items: [{
		xtype: 'tabpanel',
		activeTab: 0,
		border: false,
		items: [{
			title: 'Pending Events',
			layout: 'fit',
			tbar: [{
				xtype: 'splitbutton',
				text: 'Approve event(s)',
				iconCls: 'date_check',
				style: 'margin-left: 5px;',
				handler: function() {
					grid = this.ownerCt.ownerCt.items.items[0];
					selections = grid.getSelectionModel().getSelections();
					events = [];
					for (i = 0; i < selections.length; i++) {
						if (selections[i].data.approval == 'pending') {
							events.push(selections[i].data.itemno);
						}
					}
					Ext.Ajax.request({
						url: 'main_menu/calendar_data.json.php',
						success: function(r) {
							grid.store.load();
						},
						params: {
							command: 'approveEvents',
							eventIDs: Ext.encode(events)
						}
					});
				},
				menu: new Ext.menu.Menu({
					items: [{
						text: 'Approve event and <br />send note to other <br />approvers',
						iconCls: 'date_mail',
						width: 50
					}]
				})
			},
			{
				text: 'Reject event(s)',
				iconCls: 'date_delete',
				style: 'margin-left: 5px;',
				handler: function() {
					jsLoader.load('main_menu/' + modulizerPath + '/calendar_event_rejection_popup.js');
					jsLoader.loadedCall(
					function() {
						calendarEventRejectionPopup.show();
					});
				}
			}],
			items: new calendarApprovalGrid([0, 1, 2, 3, 4, 6], true),
			//pending
			listeners: {
				activate: function(tab) {
					tab.items.items[0].store.load();
				}
			}
		},
		{
			title: 'Approved Events',
			layout: 'fit',
			tbar: [{
				text: 'Delete event(s)',
				iconCls: 'date_delete',
				style: 'margin-left: 5px;',
				handler: function() {
					grid = this.ownerCt.ownerCt.items.items[0];
					selections = grid.getSelectionModel().getSelections();
					events = [];
					for (i = 0; i < selections.length; i++) {
						if (selections[i].data.approval == 'pending') {
							events.push(selections[i].data.itemno);
						}
					}
					Ext.Ajax.request({
						url: 'main_menu/calendar_data.json.php',
						success: function(r) {
							grid.store.load();
						},
						params: {
							command: 'deleteEvents',
							eventIDs: Ext.encode(events)
						}
					});
				}
			}],
			items: new calendarApprovalGrid([0, 1, 2, 3, 4, 5], false),
			//approved
			listeners: {
				activate: function(tab) {
					tab.items.items[0].store.load();
				}
			}
		},
		{
			xtype: 'panel',
			title: 'Approval Staff',
			layout: 'hbox',
			frame: false,
			padding: 10,
			layoutConfig: {
				align: 'stretch'
			},
			items: [{
				xtype: 'panel',
				title: 'Staff',
				autoScroll: true,
				flex: 1,
				items: [{
					xtype: 'multiselecttreepanel',
					border: false,
					id: 'calStaffTree',
					rootVisible: false,
					limitSelections: true,
					enableDD: true,
					dropConfig: {
						notifyDrop: function(source, e, data) {
							teacherIDs = [];
							if (source.tree != this.tree) {
								insertions = new Object();
								for (i = 0; i < data.nodes.length; i++) {
									if (data.nodes[i].leaf) {
										curDivision = data.nodes[i].parentNode.attributes.divisionID;
										if (insertions[curDivision] === undefined) {
											insertions[curDivision] = [];
											for (j = 0; j < data.nodes[i].parentNode.childNodes.length; j++) {
												insertions[curDivision].push(data.nodes[i].parentNode.childNodes[j].attributes.teacherID);
											}
										}
										insertions[curDivision].remove(data.nodes[i].attributes.teacherID);
										data.nodes[i].remove();
									}
								}
								Ext.Ajax.request({
									url: 'main_menu/calendar_data.json.php',
									success: function(r) {},
									params: {
										command: 'saveApprovalDivisions2',
										insertions: Ext.encode(insertions)
									}
								});
							}
							return true;
						}
					},
					dragConfig: {},
					root: new Ext.tree.AsyncTreeNode({
						expanded: true
					}),
					loader: new Ext.tree.TreeLoader({
						autoLoad: false,
						url: 'schooldynamics_data.json.php',
						baseParams: {
							command: 'getActiveStaffTree'
						}
					})
				}]
			},
			{
				xtype: 'container',
				width: 50,
				layout: 'vbox',
				layoutConfig: {
					align: 'center',
					pack: 'center'
				},
				items: [{
					xtype: 'button',
					iconCls: 'arrow_right',
					handler: function() {
						staffTree = Ext.getCmp('calStaffTree');
						approvalTree = Ext.getCmp('approvalTree');
						//Get selections from right grid
						approvers = Ext.getCmp('calStaffTree').selModel.getSelectedNodes();
						//Get selections from right grid
						selections = Ext.getCmp('approvalTree').selModel.getSelectedNodes();
						divisionIDs = [];
						for (i = 0; i < selections.length; i++) {
							//If the node is a division
							if (selections[i].attributes.divisionID !== undefined) {
								selections[i].expand();
								divisionIDs.push(selections[i].attributes.divisionID);
								for (j = 0; j < approvers.length; j++) {
									//Append left selections to division if the name is not already present and the number of approvers are < 4
									if (selections[i].childNodes.length < 4 && selections[i].findChild('teacherID', approvers[j].attributes.teacherID) == null) {
										tempNode = new Ext.data.Node({
											leaf: true
										});
										tempNode.text = approvers[j].text;
										tempNode.teacherID = approvers[j].attributes.teacherID;
										tempNode.iconCls = 'user_silhouette';
										tempNode.checked = false;
										selections[i].appendChild(tempNode);
									}
								}
							}
						}
						teacherIDs = [];
						for (i = 0; i < approvers.length; i++) {
							teacherIDs.push(approvers[i].attributes.teacherID);
						}
						Ext.Ajax.request({
							url: 'main_menu/calendar_data.json.php',
							success: function(r) {},
							params: {
								command: 'saveApprovalDivisions',
								divisionIDs: Ext.encode(divisionIDs),
								teacherIDs: Ext.encode(teacherIDs)
							}
						});
						approvalTree.selModel.clearSelections();
						staffTree.selModel.clearSelections();
					}
				},
				{
					xtype: 'button',
					iconCls: 'arrow_left',
					style: 'margin-top: 5px;',
					handler: function() {
						approvalTree = Ext.getCmp('approvalTree');
						//Get selections from approval tree
						selections = approvalTree.selModel.getSelectedNodes();
						deletions = [];
						insertions = new Object();
						for (i = 0; i < selections.length; i++) {
							//If it is not a division remove it
							if (selections[i].attributes.divisionID === undefined) {
								//deletions.push([selections[i].attributes.teacherID, selections[i].parentNode.attributes.divisionID]);
								curDivision = selections[i].parentNode.attributes.divisionID;
								if (insertions[curDivision] === undefined) {
									insertions[curDivision] = [];
									for (j = 0; j < selections[i].parentNode.childNodes.length; j++) {
										insertions[curDivision].push(selections[i].parentNode.childNodes[j].attributes.teacherID);
									}
								}
								insertions[curDivision].remove(selections[i].attributes.teacherID);
								selections[i].remove();
								doRequest = true;
							}
						}
						if (doRequest) {
							Ext.Ajax.request({
								url: 'main_menu/calendar_data.json.php',
								success: function(r) {},
								params: {
									command: 'saveApprovalDivisions2',
									insertions: Ext.encode(insertions)
								}
							});
						}
						approvalTree.selModel.clearSelections();
					}
				}]
			},
			{
				xtype: 'panel',
				title: 'Approval Divisions',
				flex: 1,
				autoScroll: true,
				items: [{
					xtype: 'multiselecttreepanel',
					id: 'approvalTree',
					border: false,
					rootVisible: false,
					useArrows: true,
					enableDD: true,
					dropConfig: {
						notifyDrop: function(source, e, data) {
							targetNode = this.dragOverData.target;
							targetNode.expand();
							for (i = 0; i < data.nodes.length; i++) {
								origNode = data.nodes[i];
								dropped = false;
								if (source.tree != this.tree) { //If staff member is from staff list, duplicate it for the approval tree
									sourceNode = new Ext.data.Node({
										leaf: true
									});
									sourceNode.text = data.nodes[i].text;
									sourceNode.teacherID = data.nodes[i].attributes.teacherID;
									sourceNode.iconCls = 'user_silhouette';
									sourceNode.checked = false;
								} else {
									sourceNode = origNode; //Just reorder node since it is from same tree
									if (sourceNode.getDepth() == 1) {
										return false; //Don't allow divisions to be reordered
									}
								}
								if (targetNode.getDepth() == 1 && this.dragOverData.point == 'append' && targetNode.childNodes.length < 4 && targetNode.findChild('teacherID', sourceNode.teacherID) == null) {
									//Check to see if any children have the same teacherid
									targetNode.appendChild(sourceNode);
									parentNode = targetNode;
									dropped = true;
								}
								if (sourceNode.parentNode == targetNode.parentNode || (targetNode.parentNode.childNodes.length < 4 && targetNode.parentNode.findChild('teacherID', sourceNode.teacherID) == null)) {
									parentNode = targetNode.parentNode;
									if (targetNode.getDepth() == 2 && this.dragOverData.point == 'above') {
										//Check to see if any siblings have the same teacherid
										targetNode.parentNode.insertBefore(sourceNode, targetNode);
										dropped = true;
									}
									if (targetNode.getDepth() == 2 && this.dragOverData.point == 'below') {
										//Check to see if any siblings have the same teacherid
										targetNode.parentNode.insertBefore(sourceNode, targetNode.nextSibling);
										dropped = true;
									}
								}
							}
							if (dropped) {
								divisionIDs = [parentNode.attributes.divisionID];
								teacherIDs = [];
								for (i = 0; i < parentNode.childNodes.length; i++) {
									teacherIDs.push(parentNode.childNodes[i].attributes.teacherID);
								}
								Ext.Ajax.request({
									url: 'main_menu/calendar_data.json.php',
									success: function(r) {},
									params: {
										command: 'saveApprovalDivisions',
										divisionIDs: Ext.encode(divisionIDs),
										teacherIDs: Ext.encode(teacherIDs)
									}
								});
							}
							return dropped;
						}
					},
					root: new Ext.tree.AsyncTreeNode({
						expanded: true
					}),
					loader: new Ext.tree.TreeLoader({
						autoLoad: false,
						url: 'main_menu/calendar_data.json.php',
						baseParams: {
							command: 'getApprovalStaff'
						}
					})
				}]
			}],
			listeners: {
				show: function() {
					Ext.getCmp('approvalTree').root.reload();
				}
			}
		}]
	}],
	buttons: [{
		text: 'Close',
		handler: function(btn) {
			this.ownerCt.ownerCt.hide();
		}
	}]
})
);

