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

function DashboardReminderAssistant(dashboardData, personId) {
	this.dashboardData = dashboardData; // hold onto a reference to the dashboardData
	this.personId = personId;
	this.tapHandler = this.tapHandler.bindAsEventListener(this);
}

DashboardReminderAssistant.prototype.setup = function() {
	this.update(true);
	this.reminderInfoElement = this.controller.get('reminder_info');
	this.reminderInfoElement.addEventListener(Mojo.Event.tap, this.tapHandler);	
};

DashboardReminderAssistant.prototype.cleanup = function() {
	this.reminderInfoElement.removeEventListener(Mojo.Event.tap, this.tapHandler);	
	if (!this.dashboardData || !this.dashboardData.data) {
		return;
	}

	if (this.personId && this.dashboardData.data[this.personId]) {
		delete this.dashboardData.data[this.personId];
	}
};

DashboardReminderAssistant.prototype.update = function(isNewContent) {
	if(!this.dashboardData && !this.dashboardData.data)
		return;
	var data = this.dashboardData.data[this.personId];
	var renderedInfo = Mojo.View.render({object: data, template: 'dashboardReminder/reminder-info'});

	// Insert the HTML into the DOM, replacing the existing content.
	Element.update(this.controller.get('reminder_info'),renderedInfo);
	
	if(isNewContent) {
		this.controller.stageController.indicateNewContent(true);
	}
};

DashboardReminderAssistant.prototype.tapHandler = function(event) {
    this.controller.serviceRequest("palm://com.palm.applicationManager", {
        method: "open",
        parameters: {
            id: "com.palm.app.contacts",
            params: {
                launchType: "reminder",
                personId: this.personId
            }
        }
    });
	this.controller.window.close(); 
};