GO.tasks.MainPanel = function(config){
	
	if(!config)
	{
		config = {};
	}
		
	this.taskListsStore = new GO.data.JsonStore({
		url: GO.settings.modules.tasks.url+'json.php',
		baseParams: {
			'task': 'tasklists',
			limit:GO.settings.config.nav_page_size
		},
		root: 'results',
		totalProperty: 'total',
		id: 'id',
		fields:['id','dom_id','name','checked']
	});

	this.taskListsPanel = new GO.tasks.TaskListsGrid({
		id:'ta-tasksgrid',
		region:'center',
		loadMask:true,
		store: this.taskListsStore,
		title: GO.tasks.lang.tasklists		
	});

	this.taskListsPanel.on('drop', function(type)
	{
	    this.taskListsPanel.store.reload();
	}, this);

	this.taskListsPanel.on('change', function(grid, tasklists, records)
	{                		                
		this.gridPanel.store.baseParams.tasklists = Ext.encode(tasklists);
		this.gridPanel.store.load();
		this.tasklist_ids = tasklists;

		if(records.length)
		{
			this.addTaskPanel.populateComboBox(records);

			this.tasklist_id = records[0].data.id;
			this.tasklist_name = records[0].data.name;
		}

		delete this.gridPanel.store.baseParams.tasklists;
	}, this);
				
	var filterPanel = new Ext.form.FormPanel({
		//title:GO.tasks.lang.filter,
		height:142,
		cls:'go-form-panel',
		waitMsgTarget:true,
		region:'north',
		border:true,
		split:true,
		items: [{
				hideLabel:true,
				anchor:'100%',
				xtype:'radiogroup',
				value:GO.tasks.show,
				columns: 1,
				listeners:{
					change:function(radiogroup, checkedbox){
						this.gridPanel.store.baseParams['show']=checkedbox.inputValue;
						this.gridPanel.store.load();
						delete this.gridPanel.store.baseParams['show'];
					},
					scope:this
				},
				items: [{
					boxLabel:  GO.tasks.lang.active,
					name: 'show',
					inputValue: 'active',
					checked:true
				},{
					boxLabel: GO.tasks.lang.dueInSevenDays,
					name: 'show',
					inputValue: 'sevendays'
				},{
					boxLabel: GO.tasks.lang.overDue,
					name: 'show',
					inputValue: 'overdue'
				},{
					boxLabel: GO.tasks.lang.completed,
					name: 'show',
					inputValue: 'completed'
				},{
					boxLabel: GO.tasks.lang.futureTasks,
					name: 'show',
					inputValue: 'future'
				},{
					boxLabel: GO.tasks.lang.all,
					name: 'show',
					inputValue: 'all'
				}]
			}]
	});
      
	this.categoriesPanel= new GO.grid.MultiSelectGrid({
		id:'ta-categories-grid',
		title:GO.tasks.lang.categories,
		region:'south',
		loadMask:true,
		height:150,
		allowNoSelection:true,
		store:GO.tasks.categoriesStore
	});
	this.categoriesPanel.on('change', function(grid, categories, records)
	{
		this.gridPanel.store.baseParams.categories = Ext.encode(categories);
		this.gridPanel.store.reload();
                
		delete this.gridPanel.store.baseParams.categories;
	}, this);


	this.addTaskPanel = new GO.tasks.AddTaskPanel({
		region:'north'
	});

	this.gridPanel = new GO.tasks.TasksPanel( {		
		id:'ta-tasks-grid',
		loadMask:true,
		region:'center'
	});
			
	this.gridPanel.on("delayedrowselect",function(grid, rowIndex, r){
		this.taskPanel.load(r.data.id);
	}, this);

	this.gridPanel.on('rowdblclick', function(grid, rowIndex){
		this.taskPanel.editHandler();
	}, this);

	this.gridPanel.on('checked', function(grid, task_id){
		if(this.taskPanel.data && this.taskPanel.data.id==task_id)
			this.taskPanel.reload();
			
	}, this);
			
	this.gridPanel.store.on('load', function(store){
		this.deleteButton.setDisabled(!store.reader.jsonData.data.write_permission);
		this.addButton.setDisabled(!store.reader.jsonData.data.write_permission);

		//this.gridPanel.ownerCt.setTitle(store.reader.jsonData.grid_title);

		var found = false
		for(var i=0; i<this.tasklist_ids.length; i++)
		{
			if(this.tasklist_ids[i] == this.taskPanel.data.tasklist_id)
			{
				found = true;
			}
		}
		if(!found)
		{
			this.taskPanel.reset();
		}
		
	}, this);
	
	this.taskPanel = new GO.tasks.TaskPanel({
		//title:GO.tasks.lang.task,
		region:'east',
		width:400,
		border:true
	});
	

			
	config.layout='border';
	config.tbar=new Ext.Toolbar({
			cls:'go-head-tb',
			items: [this.addButton = new Ext.Button({
				iconCls: 'btn-add',
				text: GO.lang['cmdAdd'],
				cls: 'x-btn-text-icon',
				handler: function(){
					this.taskPanel.reset();
					GO.tasks.showTaskDialog({
						tasklist_id: this.tasklist_id,
						tasklist_name: this.tasklist_name
					});

				},
				scope: this
			}),this.deleteButton = new Ext.Button({
				iconCls: 'btn-delete',
				text: GO.lang['cmdDelete'],
				cls: 'x-btn-text-icon',
				handler: function(){
					this.gridPanel.deleteSelected({
						callback : this.taskPanel.gridDeleteCallback,
						scope: this.taskPanel
					});
				},
				scope: this
			}),{
				iconCls: 'btn-settings',
				text: GO.lang.administration,
				cls: 'x-btn-text-icon',
				handler: function(){
					this.showAdminDialog();
				},
				scope: this
			},{
				iconCls: 'btn-export',
				text: GO.lang.cmdExport,
				cls: 'x-btn-text-icon',
				handler:function(){
					var config = {};
					config.colModel = this.gridPanel.getColumnModel();
					config.title = GO.tasks.lang.tasks;

					var query = this.gridPanel.searchField.getValue();
					if(!GO.util.empty(query))
					{
						config.subtitle= GO.lang.searchQuery+': '+query;
					}else
					{
						config.subtitle='';
					}

					if(!this.exportDialog)
					{
						this.exportDialog = new GO.ExportQueryDialog({
							query:'get_tasks'
						});
					}
					this.exportDialog.show(config);

				},
				scope: this
			},{
				iconCls: 'btn-refresh',
				text: GO.lang['cmdRefresh'],
				cls: 'x-btn-text-icon',
				handler: function(){
					this.taskListsStore.load();
				},
				scope: this
			}
			]
		});
	config.items=[	
	new Ext.Panel({
		region:'west',
		titlebar: false,
		autoScroll:false,
		closeOnTab: true,
		width: 210,
		split:true,
		resizable:true,
		layout:'border',
		baseCls: 'x-plain',
		items:[
		this.taskListsPanel,
		filterPanel,
		this.categoriesPanel
		]
	}),
	{
		//title:GO.tasks.lang.tasks,
		region:'center',
		border:false,
		layout:'border',
		items:[this.gridPanel, this.addTaskPanel]
	},
	this.taskPanel
	];
	
	GO.tasks.MainPanel.superclass.constructor.call(this, config);
	
	this.on('show', function(){
		GO.tasks.notificationEl.setDisplayed(false);
	},this);
	
}
 
Ext.extend(GO.tasks.MainPanel, Ext.Panel,{

	tasklist_ids: [],
	afterRender : function()
	{
		GO.tasks.MainPanel.superclass.afterRender.call(this);


		GO.tasks.tasksObservable.on('save',function(tasksObservable, task_id, loadedStore){
			if(this.gridPanel.store!=loadedStore)
				this.gridPanel.store.reload();
		}, this);
                
		this.taskListsStore.on('load', function(){
			var records = [];
			for(var i=0; i<this.taskListsStore.data.length; i++)
			{
				var item = this.taskListsStore.data.items[i];
				if(item.data.checked)
				{
					records.push(item);
				}
			}

			this.addTaskPanel.populateComboBox(records);

			if(records.length)
			{
				this.tasklist_id = records[0].data.id;
				this.tasklist_name = records[0].data.name;
			}
                       
			this.gridPanel.store.load();                       
		},this);

		
		//this.taskListsStore.load();

		this.getEl().mask(GO.lang.waitMsgLoad);
		Ext.Ajax.request({
			url: GO.settings.modules.tasks.url+'json.php',
			params:{
				task:'init'
			},
			callback: function(options, success, response)
			{

				if(!success)
				{
					alert( GO.lang['strRequestError']);
				}else
				{
					var jsonData = Ext.decode(response.responseText);

					GO.tasks.categoriesStore.loadData(jsonData.categories);
					this.taskListsStore.loadData(jsonData.tasklists);

					this.getEl().unmask();
				}
			},
			scope:this
		});

               
		
		GO.mainLayout.on('linksDeleted', function(deleteConfig, link_types){
			GO.mainLayout.onLinksDeletedHandler(link_types[12], this, this.gridPanel.store);
		}, this);    
	},
  
	showAdminDialog : function() {
		
		if(!this.adminDialog)
		{
			this.tasklistDialog = new GO.tasks.TasklistDialog();
			this.categoryDialog = new GO.tasks.CategoryDialog();

			GO.tasks.writableTasklistsStore.on('load', function(){
				if(GO.tasks.writableTasklistsStore.reader.jsonData.new_default_tasklist){
					GO.tasks.defaultTasklist=GO.tasks.writableTasklistsStore.reader.jsonData.new_default_tasklist;
				}
			
			}, this);
			
			this.tasklistDialog.on('save', function(){
				GO.tasks.writableTasklistsStore.load();
				this.taskListsStore.load();
			}, this);

			this.categoryDialog.on('save', function(){
				GO.tasks.categoriesStore.load();
			},this);
			
			this.tasklistsGrid = new GO.grid.GridPanel( {
				paging:true,
				border:false,
				title: GO.tasks.lang.tasklists,
				store: GO.tasks.writableTasklistsStore,
				deleteConfig: {
					callback:function(){
						this.taskListsStore.load();
					},
					scope:this
				},
				columns:[{
					header:GO.lang['strName'],
					dataIndex: 'name',
					sortable:true
				},{
					header:GO.lang['strOwner'],
					dataIndex: 'user_name'
				}],
				view:new  Ext.grid.GridView({
					autoFill:true
				}),
				sm: new Ext.grid.RowSelectionModel(),
				loadMask: true,
				tbar: [{
					iconCls: 'btn-add',
					text: GO.lang['cmdAdd'],
					cls: 'x-btn-text-icon',
					handler: function(){						
						this.tasklistDialog.show();
					},
					disabled: !GO.settings.modules.tasks.write_permission,
					scope: this
				},{
					iconCls: 'btn-delete',
					text: GO.lang['cmdDelete'],
					cls: 'x-btn-text-icon',
					disabled: !GO.settings.modules.tasks.write_permission,
					handler: function(){
						this.tasklistsGrid.deleteSelected();
					},
					scope:this
				},'-',new GO.form.SearchField({
					store: GO.tasks.writableTasklistsStore,
					width:150
				})]
			});

			this.categoriesGrid = new GO.grid.GridPanel( {
				paging:true,
				border:false,
				title: GO.tasks.lang.categories,
				store: GO.tasks.categoriesStore,
				deleteConfig: {
					callback:function(){
						GO.tasks.categoriesStore.load();
					},
					scope:this
				},
				columns:[{
					header:GO.lang['strName'],
					dataIndex: 'name',
					sortable:true
				},{
					header:GO.lang['strOwner'],
					dataIndex: 'user_name'
				}],
				view:new  Ext.grid.GridView({
					autoFill:true
				}),
				sm: new Ext.grid.RowSelectionModel(),
				loadMask: true,
				tbar: [{
					iconCls: 'btn-add',
					text: GO.lang['cmdAdd'],
					cls: 'x-btn-text-icon',
					handler: function(){
						this.categoryDialog.show();
					},
					disabled: !GO.settings.modules.tasks.write_permission,
					scope: this
				},{
					iconCls: 'btn-delete',
					text: GO.lang['cmdDelete'],
					cls: 'x-btn-text-icon',
					disabled: !GO.settings.modules.tasks.write_permission,
					handler: function(){
						this.categoriesGrid.deleteSelected();
					},
					scope:this
				}]
			});
			
			this.tasklistsGrid.on("rowdblclick", function(grid, rowClicked, e){

				this.tasklistDialog.show(grid.selModel.selections.keys[0]);
			}, this);

			this.categoriesGrid.on('rowdblclick', function(grid, rowIndex)
			{
				var record = grid.getStore().getAt(rowIndex);

				if(GO.settings.has_admin_permission || (record.data.user_id > 0))
				{
					this.categoryDialog.show(record);
				}				

			}, this);

			this.tabPanel = new Ext.TabPanel({
				activeTab:0,
				border:false,
				items:[this.tasklistsGrid,this.categoriesGrid]
			})

			this.adminDialog = new Ext.Window({
				title: GO.lang.cmdSettings,
				layout:'fit',
				modal:false,
				minWidth:300,
				minHeight:300,
				height:400,
				width:600,
				closeAction:'hide',				
				items: this.tabPanel,
				buttons:[{
					text:GO.lang['cmdClose'],
					handler: function(){
						this.adminDialog.hide()
					},
					scope: this
				}]
			});
			
		}
		
		if(!GO.tasks.writableTasklistsStore.loaded){
			GO.tasks.writableTasklistsStore.load();
		}

		if(!GO.tasks.categoriesStore.loaded){
			GO.tasks.categoriesStore.load();
		}
	
		this.adminDialog.show();
	}
	
});


GO.tasks.showTaskDialog = function(config){

	if(!GO.tasks.taskDialog)
		GO.tasks.taskDialog = new GO.tasks.TaskDialog();

	GO.tasks.taskDialog.show(config);
}



GO.tasks.writableTasklistsStore = new GO.data.JsonStore({
	url: GO.settings.modules.tasks.url+'json.php',
	baseParams: {
		'task': 'tasklists',
		'auth_type':'write'
	},
	root: 'results',
	totalProperty: 'total',
	id: 'id',
	fields:['id','name','user_name'],
	remoteSort:true,
	sortInfo: {
		field: 'name',
		direction: 'ASC'
	}
});

GO.tasks.categoriesStore = new GO.data.JsonStore({
	url: GO.settings.modules.tasks.url+'json.php',
	baseParams: {
		'task': 'categories'
	},
	root: 'results',
	totalProperty: 'total',
	id: 'id',
	fields:['id','name','user_name','checked','user_id'],
	remoteSort:true,
	sortInfo: {
		field: 'name',
		direction: 'ASC'
	}
});

/*
 * This will add the module to the main tabpanel filled with all the modules
 */
 
GO.moduleManager.addModule('tasks', GO.tasks.MainPanel, {
	title : GO.tasks.lang.tasks,
	iconCls : 'go-tab-icon-tasks'
});
/*
 * 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 task window when a user clicks on it from a 
 * panel with links. 
 */
GO.linkHandlers[12]=function(id, link_config){

	if(!GO.tasks.taskLinkWindow){
		var taskPanel = new GO.tasks.TaskPanel();
		GO.tasks.taskLinkWindow = new GO.LinkViewWindow({
			title: GO.tasks.lang.task,
			closeAction:'hide',
			items: taskPanel,
			taskPanel: taskPanel
		});
	}
	GO.tasks.taskLinkWindow.taskPanel.load(id);
	GO.tasks.taskLinkWindow.show();
}

GO.linkPreviewPanels[12]=function(config){
	config = config || {};
	return new GO.tasks.TaskPanel(config);
}


GO.newMenuItems.push({
	text: GO.tasks.lang.task,
	iconCls: 'go-link-icon-12',
	itemId:'ta-new-task',
	handler:function(item, e){

		var taskShowConfig = item.parentMenu.taskShowConfig || {};
		taskShowConfig.link_config=item.parentMenu.link_config

		GO.tasks.showTaskDialog(taskShowConfig);
	}
});




GO.mainLayout.onReady(function(){

	//GO.checker is not available in some screens like accept invitation from calendar
	if(GO.checker){
		//create notify icon
		var notificationArea = Ext.get('notification-area');
		if(notificationArea)
		{
			GO.tasks.notificationEl = notificationArea.createChild({
				id: 'ta-notify',
				tag:'a',
				href:'#',
				style:'display:none'
			});
			GO.tasks.notificationEl.on('click', function(){
				GO.mainLayout.openModule('tasks');
			}, this);
		}

		GO.checker.on('check', function(checker, data){
			var tp = GO.mainLayout.getModulePanel('tasks');

			if(data.tasks.active!=GO.tasks.last_active && data.tasks.active>0)
			{
				
				if(!tp || !tp.isVisible())
					GO.tasks.notificationEl.setDisplayed(true);
			}

			GO.tasks.notificationEl.update(data.tasks.active);			
			GO.tasks.last_active=data.tasks.active;			
		});
	}
});
