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

var ContactReminder = Class.create({
	initialize: function() {
		Mojo.Log.info('<--------------->initialize function');
		this.toRecipientReminders = {};
	},
	
	/**
	 * This function displays a banner if a contact has a reminder value.  
	 * @param {Object} response
	 */
	displayReminder: function(response){
		//Mojo.Log.info('EmailRecipient.getDetails() response=%j', response);
				
		if (response.record && response.record.reminder) {
			var contactId = response.record.id;
			var reminder = response.record.reminder;
			Mojo.Log.info('In contact response: contactId=%s, reminder=%s', contactId, reminder);
			
			if (this.toRecipientReminders[contactId]  
					&& this.toRecipientReminders[contactId].record
					&& this.toRecipientReminders[contactId].record.reminder === reminder) {
				return;
			}
			Mojo.Log.info('Caching recipient details');
			this.toRecipientReminders[contactId] = response;
			
			var reminderNotificationText;
	
	
			if (Contact.recordHasNames(response.record)) {
				reminderNotificationText = $L("Reminder for #{contactName}").interpolate({
					contactName: Contact.formatPersonName(response.record) 
				});	
				response.record.displayText = "";	
			} else {
				reminderNotificationText = $L("Reminder for #{displayText}").interpolate(response.record);
				response.record.firstName = "";
				response.record.lastName = "";
			}
			
			
			var bannerParams = {
				icon: "images/personal-reminder-icon-small.png",
				messageText: reminderNotificationText,
				soundClass: "alerts"
			};
			
			var stageName = "reminderStage" + contactId;
			var bannerLaunchParams = {
				reminderPersonId: contactId,
				stageName: stageName,
				clearBanner: true
			};
			Mojo.Controller.appController.showBanner(bannerParams, bannerLaunchParams, 'reminder');
			Mojo.Log.info('<---------------> shown banner');
			
			var dashboard = Mojo.Controller.appController.getStageController(stageName);
			if (dashboard) {
				dashboard.delegateToSceneAssistant("update");
			}
			else {
				Mojo.Log.info('<---------------> creating functino for dashboard');
				var func = function(stageController){
					stageController.pushScene('contactreminder-dashboard', this.toRecipientReminders, contactId, stageName);
				}.bind(this);
				var params = {
					name: stageName,
					lightweight: true,
					icon: "images/personal-reminder-icon-small.png"
				};
				
				if (this.toRecipientReminders[contactId]) {
					Mojo.Log.info('<---------------> creating callback stage for dashboard');
					Mojo.Controller.appController.createStageWithCallback(params, func, Mojo.Controller.StageType.dashboard);
				}
			}
		}
	}, 
});
