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

function DashboardClass0Assistant(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);
}

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

DashboardClass0Assistant.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);
};

DashboardClass0Assistant.prototype.update = function(isNewContent) {
	if (!this.dashboardData || this.dashboardData.list.length == 0) {
		Mojo.Log.error("DashboardClass0Assistant.update called with empty data.");
		Mojo.Log.error("%j", this.dashboardData);
		this.closeDashboard();
		return;
	}
	
	var data = this.dashboardData.list;
	var numConversations = data.length;

	var dModel = {
		conversationCount: numConversations,
		singleMessageClass: "single",
		unreadMessageCount: data.length,
		topLine: "",
		bottomLine: "",
		messageId: "",
		rightImageClass: "hidden", // setting this to something other than hidden will make it visible
		displayAttachment: "style='display:none;'"
	};
  
	if (numConversations > 1) {
		dModel.singleMessageClass = "multi";
	}

	var lastMessage = data.last();
	dModel.topLine = lastMessage.alertTitle;
	dModel.bottomLine = lastMessage.messageText;

	// Reusing template from dashboardMessage/
	var renderedInfo = Mojo.View.render({object: dModel, template: 'dashboardMessage/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);
	}
};

DashboardClass0Assistant.prototype.tapHandler = function(event) {
	var data = this.dashboardData.list;
	this.appController.notificationAssistant.renderClass0PopupAlert();
};

DashboardClass0Assistant.prototype.closeDashboard = function(){
	this.controller.window.close(); 
};