/* Copyright 2009 Palm, Inc.  All rights reserved. */

function DashboardErrorAssistant(dashboardData){
	this.dashboardData = dashboardData; // hold onto a reference to the dashboardData
	this.appController = Mojo.Controller.getAppController();
	this.Messaging = this.appController.Messaging;
	this.tapHandler = this.tapHandler.bindAsEventListener(this);
}

DashboardErrorAssistant.prototype.setup = function(){
	this.update(true);
	this.messageInfoElement = this.controller.get('message_info');
	this.messageInfoElement.addEventListener(Mojo.Event.tap, this.tapHandler);
}

DashboardErrorAssistant.prototype.cleanup = function(){
	// If the dashboard is unloaded then wipe out our data
	this.dashboardData.list.length = 0;
	this.messageInfoElement.removeEventListener(Mojo.Event.tap, this.tapHandler);
}

DashboardErrorAssistant.prototype.update = function(isNewContent) {
	if (!this.dashboardData || !this.dashboardData.list.length) {
		return;
	}

	var data = this.dashboardData.list.last();

	var dModel = {
		topLine: new Template($L("Messaging - #{username}")).evaluate(data),
		bottomLine: this.Messaging.Error.getLoginErrorFromCode(data.connectionErrorCode),
		leftImage: Mojo.appPath + "/images/notification-large.png",
		rightImage: "",
		rightImageClass: "hidden" // setting this to something other than hidden will make it visible
	};
	var renderedInfo = Mojo.View.render({
		object: dModel,
		template: 'dashboardError/message-info'
	});
	// Insert the HTML into the DOM, replacing the existing content.
	Element.update(this.controller.get('message_info'), renderedInfo);
	
	if(isNewContent) {
		this.controller.stageController.indicateNewContent(true);
	}
};

DashboardErrorAssistant.prototype.tapHandler = function(){
	MessagingMojoService.launchMessaging(this.controller, {});
	// close the dashboard
	this.controller.window.close();
};