GO.projects2.MainPanel = function(config){

	if(!config)
	{
		config = {};
	}

	config.id=config.id || Ext.id();


//	this.projectsView = new GO.projects2.ProjectsView({
//		layout:'fit',
//		region:'center',
//		border: false,
//		id:config.id+'-projects-view'
//	});
//

	this.selectProject = new GO.projects2.SelectProject({
			emptyText:GO.projects2.lang.searchProject,
			hideLabel:true,
			anchor:'100%',
			minListWidth:600,
			region: 'northdele',
			store:new GO.data.JsonStore({
				url: GO.url('projects2/project/store'),
				fields:['id', 'path','use_tasks_panel'],
				remoteSort: true
			}),
			listeners:{
				select:function(cmb, record){
					this.project_id=record.id;
					this.tasksPanel.setProjectId(this.project_id,record.data.use_tasks_panel);
					if(this.project_id > 0)
					{
						this.projectPanel.load(this.project_id);
					}
				},
				scope:this
			}
	});


	this.treePanel = new GO.projects2.ProjectsTree({
		//		title:GO.projects2.lang.projects,
		width:250,
		split:true,
		autoScroll:true,
		region:'center',
		border:true
	});


	this.treePanel.on('click',function(){
		// NEXT LINE TO BE COMMENTED OUT
//		this.selectProject.reset();

		this.project_id=this.treePanel.project_id;
// THIS IS NEW
		this._switchProject(this.project_id)

		// NEXT 7 LINES TO BE COMMENTED OUT
//		if(this.project_id > 0)
//		{
//			this.projectPanel.load(this.project_id);
//		}else
//		{
//			this.projectPanel.reset();
//		}
	}, this);



	//	this.treePanel.treeLoader.on('load',function(){
	//		var node = this.treePanel.getNodeById(this.projectsView.projectsPanel.store.baseParams.parent_project_id);
	//		this.treePanel.getSelectionModel().select(node);
	//	}, this,{
	//		single:true
	//	});





//	this.filterPanel= new GO.grid.MultiSelectGrid({
//		id:'pm-status-filter',
//		title:GO.projects2.lang.statuses,
//		region:'south',
//		loadMask:true,
//		height:200,
//		split:true,
//		relatedStore: this.projectsView.projectsPanel.store,
//		autoLoadRelatedStore:false,
//		allowNoSelection:true,
//		store:new GO.data.JsonStore({
//			url: GO.url("projects2/status/store"),
//			fields: ['id','name','checked'],
//			baseParams:{
//				filterable:1
//			},
//			remoteSort: true
//		})
//	});
//


	this.statusesFilterGrid = new GO.projects2.StatusesFilterGrid({
		region:'south',
		id:'pr2-statuses',
		width:180,
		height:250
	});

	var items = [];

	items.push({
		region:'north',
		height:30,
		cls:'go-form-panel',
		layout:'form',
		items:[
			this.selectProject
		]

	},this.treePanel

	,this.statusesFilterGrid

	);

	var westPanel = new Ext.Panel({
		id:config.id+'-west',
		region:'west',
		layout:'border',
		border:false,
		split:true,
		width:250,
		items:items,
		collapsible:true,
		collapseMode:'mini',
		header:false
	});


	this.projectPanel = new GO.projects2.ProjectPanel({
		region: 'center',
		id:'pr2-project-panel'
//		height: '350',
//		width: 500,
//		split:true,
//		collapseMode:'mini',
//		hideCollapseTool:true,
//		collapsible:true
	});

	var subProjectFields = {
		fields: ['id', 'name','reference_no','status_name','user_name','type_name','template_name','responsible_user_name','icon','due_time'],
		columns: [{
				header: '',
				dataIndex: 'icon',
				id: 'icon',
				width: 20,
				renderer: function(v) {
					if (!GO.util.empty(v))
						return '<img src="'+v+'" />';
					else
						return '<img class="x-tree-node-icon go-model-icon-GO_Projects2_Model_Project" src="'+GO.settings.config.full_url+'views/Extjs3/ext/resources/images/default/s.gif" />';
				}
			},{
				header: 'ID',
				dataIndex: 'id',
				id: 'id',
				width: 50
			},{
				header:GO.lang['strName'],
				dataIndex: 'name',
				id:'name',
				width: 150
			},{
				header:GO.projects2.lang['referenceNo'],
				dataIndex: 'reference_no',
				id:'name',
				width: 150
			},{
				header:GO.projects2.lang['status'],
				dataIndex: 'status_name',
				id:'status_name',
				width: 100
			},{
				header:GO.projects2.lang['dueAt'],
				dataIndex: 'due_time',
				id:'due_time',
				width: 80,
				renderer: function(value,metaData,record) {
					return '<span class="'+this.projectPanel.templateConfig.getClass(record.data)+'">'+value+'</span>';
				},
				scope: this
			},{
				header: GO.lang['strUser'],
				dataIndex: 'user_name',
				id: 'user_name',
				width: 120,
				sortable:false
			},{
				header: GO.projects2.lang['type'],
				dataIndex: 'type_name',
				id: 'type_name',
				width: 80
			},{
				header: GO.projects2.lang['template'],
				dataIndex: 'template_name',
				id: 'template_name',
				width: 80
			},{
				header: GO.projects2.lang['projectManager'],
				dataIndex: 'responsible_user_name',
				id: 'responsible_user_name',
				width: 120,
				sortable:false
			}]
	}

	if(GO.customfields)
		GO.customfields.addColumns("GO\\Projects2\\Model\\Project", subProjectFields);

	this.subProjectsGrid = new GO.grid.GridPanel({
		border: false,
		region: 'south',
		height:300,
		hidden:true,
		layout:'fit',
		id:'pr2-sub-projects',
//		collapseMode:'mini',
//		hideCollapseTool:true,
//		collapsible:true,
//		hidden: true,
//		title:GO.projects2.lang['subprojects'],
		split: true,
//		renderTo: 'projects2-subprojectsgrid',
		store: new GO.data.JsonStore({
			url: GO.url('projects2/project/store'),
			baseParams: {
				parent_project_id: 0
			},
			root: 'results',
			totalProperty: 'total',
			id: 'id',
			fields: subProjectFields.fields,
			remoteSort:true
		}),
		autoScroll:true,
		paging : true,
		sm: new Ext.grid.RowSelectionModel(),
		cm: new Ext.grid.ColumnModel({
			defaultSortable:true,
			columns:subProjectFields.columns

		}),
		view:new Ext.grid.GridView({
			forceFit:false,
			autoFill:false
			}),
		scope: this
	});

	this.subProjectsGrid.on('rowdblclick',function(grid,rowIndex,event){
//		THIS IS NEW
		var record = grid.store.getAt(rowIndex);
		this._switchProject(record.data.id);


		// NEXT 10 LINES TO BE COMMENTED OUT
//		var record = grid.store.getAt(rowIndex);
//				this.selectProject.reset();
//
//		if(record.data.id > 0)
//		{
//			this.projectPanel.load(record.data.id);
//		}else
//		{
//			this.projectPanel.reset();
//		}
	},this);

	this.projectPanel.on('load', function(tp, project_id){

		this.project_id=project_id;
		this.tasksPanel.setProjectId(project_id,tp.data.use_tasks_panel==1);

		var node = this.treePanel.getNodeById(project_id);
		if(node){
			this.treePanel.getSelectionModel().select(node);
			node.expand();
		}

		//if (!(project_id>0) || this.projectPanel.data.project_type!=1) { // 1 means: leaf project.
		if(project_id>0 && this.projectPanel.data.has_children){
			this.subProjectsGrid.store.baseParams['parent_project_id'] = project_id;
			this.subProjectsGrid.store.load();
//			this.subProjectsGrid.expand();
			this.subProjectsGrid.setVisible(true);
		} else {
			this.subProjectsGrid.store.baseParams['parent_project_id'] = 0;
			this.subProjectsGrid.store.removeAll();
			this.subProjectsGrid.setVisible(false);
		}

		this.centerPanel.doLayout();

	}, this);
	
	this.projectPanel.on('reset', function(){
		this.subProjectsGrid.store.baseParams['parent_project_id'] = 0;
		this.subProjectsGrid.store.load();
		this.subProjectsGrid.setVisible(true);
		
		this.centerPanel.doLayout();
	}, this);

	this.centerPanel = new Ext.Panel({
		region: 'center',
		layout:'border',
		split:true,
		id:this.id+'_east',
		items: [
			this.projectPanel,
			this.subProjectsGrid
		]
	})

	this.tasksPanel = new GO.projects2.TasksGrid({
		region:'east',
		width: 500,
		id:'pm-tasks',
		collapsible:true
	});

	config.items=[
	westPanel,
	this.tasksPanel,
	this.centerPanel
	];

	this.statusesFilterGrid.on('change', function(grid, statuses, records)
	{
		
		this.treePanel.treeLoader.baseParams.pr2_statuses = Ext.encode(statuses);
		var node = this.treePanel.getNodeById(this.project_id) || this.treePanel.getRootNode();
		this.treePanel.getRootNode().reload();
//		this.treePanel.treeLoader.load(node);
		delete this.subProjectsGrid.store.baseParams.statuses;
		
		this.subProjectsGrid.store.baseParams['parent_project_id'] = 0;
		this.subProjectsGrid.store.load();
		this.subProjectsGrid.setVisible(true);
		
		this.centerPanel.doLayout();
	}, this);

	var items = [
	{
		xtype:'htmlcomponent',
		html:GO.projects2.lang.projects,
		cls:'go-module-title-tbar'
	}];
	if(GO.settings.modules.projects2.permission_level==GO.permissionLevels.manage)
	{
		items.push({
			iconCls:'btn-settings',
			text:GO.lang.administration,
			handler:function(){
				if(!this.settingsDialog)
				{
					this.settingsDialog = new GO.projects2.SettingsDialog();
				}
				this.settingsDialog.show();
			},
			scope:this
		});

//		if(GO.billing){
//			items.push({
//				iconCls:'btn-add',
//				text:GO.projects2.lang.bill,
//				handler:function(){
//					if(!GO.projects2.invoiceDialog)
//					{
//						GO.projects2.invoiceDialog= new GO.projects2.InvoiceDialog();
//					}
//					GO.projects2.invoiceDialog.show({
//						project_id:0
//					});
//				},
//				scope:this
//			});
//		}


	}
	items.push({
		iconCls:'btn-actions',
		text:GO.projects2.lang.report,
		handler:function(){
			if(!this.reportDialog)
			{
				this.reportDialog = new GO.projects2.ReportDialog();
			}
			this.reportDialog.show();
		},
		scope:this
	});

//	if(GO.hoursapproval){
//		items.push({
//			iconCls:'btn-email',
//			text:GO.projects2.lang.remindForApproval,
//			handler:function(){
//				if(!this.remindDialog){
//					this.remindDialog = new GO.projects2.RemindDialog();
//				}
//				this.remindDialog.show({
//					task:'remind_hoursapproval_users'
//				});
//			},
//			scope:this
//		});
//	}

	items.push({
		iconCls:'btn-refresh',
		text:GO.lang.cmdRefresh,
		handler:function(){
			this.refresh();
		},
		scope:this
	});


	if(GO.settings.modules.projects2.permission_level==GO.permissionLevels.manage)
	{
		items.push({
			iconCls:'btn-add',
			text:GO.lang.cmdImport,
			handler:function(){
				if(!this.importDialog)
				{
					this.importDialog = new GO.projects2.CsvImportDialog();
				}
				this.importDialog.show();
			},
			scope:this
		});

	}

//	var templateMenu = new Ext.menu.Menu({
//		items:[]
//	});
//	GO.projects2.templatesStore.on('load', function(el,rec,op){
//		templateMenu.removeAll();
//		for(var i=0; i<rec.length; i++) {
//			var item = {
//				text: rec[i].data.name,
//				cls: 'x-btn-text-icon',
//				data: rec[i].data,
//				handler: function(item) {
//					if(GO.projects2.max_projects>0 && this.store.totalLength>=GO.projects2.max_projects) {
//						Ext.Msg.alert(GO.lang.strError, GO.projects2.lang.maxProjectsReached);
//						return;
//					}
//					this.projectPanel.reset();
//					GO.projects2.showProjectDialog({
//						parent_project_id: this.project_id,
//						template_id: item.data.id
//					});
//				},
//				scope:this
//			};
//			templateMenu.addItem(item);
//		}
//    },this);

	this.invoiceButton = new Ext.Button({
		iconCls: 'btn-add',
		text: GO.projects2.lang['invoice'],
		hidden: !GO.projects2.has_finance_permission,
		cls: 'x-btn-text-icon',
		handler: function() {
			if(!this.invoiceDialog)
				this.invoiceDialog = GO.projects2.invoiceDialog = new GO.projects2.InvoiceDialog();
			this.invoiceDialog.show();
		},
		scope:this
	});
	items.push(this.invoiceButton);

//	this.addButton = new Ext.Button({
//		iconCls: 'btn-add',
//		text: GO.projects2.lang['addProject'],
//		cls: 'x-btn-text-icon',
//		menu: templateMenu,
//		handler: function() {
//			templateMenu.removeAll();
//			templateMenu.addItem({
//				text: 'Loading...',
//				disabled: true
//			});
//			GO.projects2.templatesStore.load();
//		}
//	});

	this.addButton = new Ext.Button({
		iconCls: 'btn-add',
		text: GO.projects2.lang['addProject'],
		cls: 'x-btn-text-icon',
//		disabled:true,
		handler: function(){
			if(GO.projects2.max_projects>0 && this.store.totalLength>=GO.projects2.max_projects)
			{
				Ext.Msg.alert(GO.lang.strError, GO.projects2.lang.maxProjectsReached);
			}else
			{
				this.projectPanel.reset();
				GO.projects2.showProjectDialog({
					parent_project_id: this.project_id/*,
					values:{
						type_id:this.parentProject ? this.parentProject.type_id : null
					}*/
				});
			}
		},
		scope: this
	});
	items.push(this.addButton);


	config.tbar = new Ext.Toolbar({
		cls:'go-head-tb',
		items:items
	});


	config.border=false;
	config.layout='border';
	GO.projects2.MainPanel.superclass.constructor.call(this, config);

//	THIS IS NEW
	this.tasksPanel.on('saved', function(projectId){
		if (this._saveTaskPanelBeforeLeaving) {
			this.selectProject.reset();
			if(projectId > 0)
			{
				this.projectPanel.load(projectId);
			}else
			{
				this.projectPanel.reset();
			}
			this._saveTaskPanelBeforeLeaving = false;
		}
	}, this);

	this.on('show', function() {
		this.statusesFilterGrid.store.load();
	}, this);
};

Ext.extend(GO.projects2.MainPanel, Ext.Panel, {

	project_id : 0,
// THIS IS NEW
	_saveTaskPanelBeforeLeaving : false,


	refresh : function(){
//		this.filterPanel.store.load();
//		this.projectsView.projectsPanel.store.load();
		this.treePanel.rootNode.reload();
	},

	afterRender : function(){

		GO.projects2.MainPanel.superclass.afterRender.call(this);
//		this.filterPanel.store.load();

		GO.dialogListeners.add('project',{
			scope:this,
			save:function(e, project_id, parent_project_id){
//				this.projectsView.projectsPanel.store.reload();
				this.treePanel.project_id = parent_project_id;
				this.treePanel.reloadActiveNode();
			}
		});
		
		
		this.subProjectsGrid.store.baseParams['parent_project_id'] = 0;
		this.subProjectsGrid.store.load();
		this.subProjectsGrid.setVisible(true);
		
	}
//	THIS IS NEW
	,
	_switchProject : function(projectId) {

		if (!this.tasksPanel.isDirty()) {
			this.selectProject.reset();

			if(projectId > 0)
			{
				this.projectPanel.load(projectId);
			}else
			{
				this.projectPanel.reset();
			}
		} else {
			Ext.Msg.show({
				title: GO.projects2.lang['saveBeforeLeavingTitle'],
				msg: GO.projects2.lang['saveBeforeLeavingMsg'],
				buttons: Ext.Msg.YESNOCANCEL,
				scope: this,
				fn: function(btn) {
					if (btn=='no') {
						this.selectProject.reset();

						if(projectId > 0)
						{
							this.projectPanel.load(projectId);
						}else
						{
							this.projectPanel.reset();
						}
					} else if (btn=='yes') {
						this._saveTaskPanelBeforeLeaving = true;
						this.tasksPanel.save(projectId);
					}
				}
			});
		}

	}

});

GO.projects2.showProjectDialog = function(config){
	if(!GO.projects2.projectDialog)
		GO.projects2.projectDialog = new GO.projects2.ProjectDialog();

	GO.projects2.projectDialog.show(config);
}

/*
 * This will add the module to the main tabpanel filled with all the modules
 */

GO.moduleManager.addModule('projects2', GO.projects2.MainPanel, {
	title : GO.projects2.lang.projects,
	iconCls : 'go-tab-icon-projects'
});

GO.linkPreviewPanels["GO\\Projects2\\Model\\Project"]=function(config){
	config = config || {};
	return new GO.projects2.ProjectPanel(config);
}

/*
 * If your module has a linkable item, you should add a link handler like this.
 * The index (no. 1 in this case) should be a unique identifier of your item.
 * See classes/base/links.class.inc for an overview.
 *
 * Basically this function opens a project window when a user clicks on it from a
 * panel with links.
 */
GO.linkHandlers["GO\\Projects2\\Model\\Project"]=function(id){
	if(!GO.projects2.linkWindow){
		var projectPanel = new GO.projects2.ProjectPanel();
		GO.projects2.linkWindow = new GO.LinkViewWindow({
			title: GO.projects2.lang.project,
			projectPanel: projectPanel,
			items: projectPanel,
			width:600,
			closeAction:"hide"
		});
	}
	GO.projects2.linkWindow.show();
	GO.projects2.linkWindow.projectPanel.load(id);
	return GO.projects2.linkWindow;
}

//GO.newMenuItems.push({
//	text: GO.projects2.lang.project,
//	iconCls: 'go-model-icon-GO_Projects2_Model_Project',
//	handler:function(item, e){
//		GO.projects2.showProjectDialog({
//			link_config: item.parentMenu.link_config
//		});
//	}
//});


