/**
 * Assistant for the dashboard scene
 
Copyright 2009 Palm, Inc.  All rights reserved.

*/
var DashboardAssistant = Class.create(
{
	initialize: function()
	{
		Mojo.Log.info("DashboardAssistant::initialize");
	},
	
	setup: function()
	{
		Mojo.Log.info("DashboardAssistant::setup");
		
		this._dashmain = this.controller.get('dashboard-tasks');
		this._dashtitle = this.controller.get('dashboard-title');
		this._dashsubtitle = this.controller.get('dashboard-subtitle');
		this._dashcount = this.controller.get('dashboard-count');
		this._dashcountbox = this.controller.get('dashboard-countbox');

	    this.controller.get('dashboard-item').addEventListener(Mojo.Event.tap, this._onTaskTap.bindAsEventListener(this));
	    this.controller.get('dashboard-list').addEventListener(Mojo.Event.tap, this._onTaskListTap.bindAsEventListener(this));
		
		var self = this;
		State.dashboard.addUpdateListener(this, function(model, diff)
		{
			Mojo.Log.info("Dashboard update %j %j", model, diff);
			self._update(model);
		});
		
		this._update(State.dashboard);
	},
	
	cleanup: function()
	{
		State.dashboard.removeUpdateListener(this);
	},
	
	_update: function(model)
	{
		Mojo.Log.info("_update %j", model);

		this._dashtitle.innerHTML = model.taskName;
		this._dashsubtitle.innerHTML = model.taskListName ? model.taskListName : $L('Unfiled');
		if (model.dueItemCount == 1)
		{
			this._dashmain.addClassName("single");
		}
		else
		{
			this._dashmain.removeClassName("single");
		}
		if (model.dueItemCount > 1)
		{
			this._dashcount.innerHTML = model.dueItemCount;
			this._dashcountbox.style.display = null;
		}
		else
		{
			this._dashcountbox.style.display = 'none';
		}
		this.controller.stageController.indicateNewContent(true);
	},
	
	_onTaskTap: function(event)
	{
		Weave.Services.ApplicationManager.openApplication('com.palm.app.tasks', 
		{
			details: 
			{
				show: 
				{
					taskListId: State.dashboard.taskListId,
					taskId: State.dashboard.taskId
				}
			}
		});
		Event.stop(event);
	},
	
	_onTaskListTap: function(event)
	{
		if (State.dashboard.dueItemCount == 1) 
		{
			this._onTaskTap(event);
		}
		else 
		{
			Weave.Services.ApplicationManager.openApplication('com.palm.app.tasks', 
			{
				details: 
				{
					show: 
					{
						taskListId: State.dashboard.taskListId
					}
				}
			});
			Event.stop(event);
		}
	},
});
