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


var ReminderListAssistant = Class.create({
	initialize: function() {
		this.reminders = getReminderManager();
		
		this.remindersAttributes = {
			itemTemplate : 'reminder-list/reminderRow',
		  listTemplate : 'reminder-list/reminderContainer',
			swipeToDelete: true,
			autoconfirmDelete: true
			
		};
		this.remindersModel = {
			items : [],
		};
	},

	setup: function() {
    this.controller.setupWidget(Mojo.Menu.commandMenu,
        this.attributes = {
           spacerHeight: 0,
           menuClass: "reminder-menu",
        },
        this.model = {
          visible: true,
          items: [ 
                 { label: $L('Dismiss All'), command: "dismissAll"}
            ]
    });
		
		this.reminders.observeReminders('reminder-list', this);
		
		this.remindersModel.items = this.reminders.getAllReminders();
		this.controller.setupWidget('reminders', this.remindersAttributes, this.remindersModel);
		var reminderList = this.controller.get('reminders');
		reminderList.observe(Mojo.Event.listTap, this.reminderTap.bindAsEventListener(this));
		reminderList.observe(Mojo.Event.listDelete, this.reminderDelete.bindAsEventListener(this));
	},
	
	handleCommand: function(event) {
	  if(event.type == Mojo.Event.command && event.command == "dismissAll") {
  		this.reminders.removeAllReminders();
  		this.controller.stageController.popScene();
    }
	},
  
	cleanup: function() {
		this.reminders.stopObservingReminders('reminder-list');
	},

	update: function() {
		if (this.controller) {
			this.remindersModel.items = this.reminders.getAllReminders();
			this.controller.modelChanged(this.remindersModel);
		}
	},
	
	reminderTap: function(event) {
		var reminder = event.item;
		this.reminders.removeReminder(reminder.id);
		this.showEventDetails(reminder);
	},
	
	reminderDelete: function(event) {
		
		var reminderID = event.item.id;
		var f = function(){
			this.reminders.removeReminder(reminderID);
		}.bind(this);
		f.defer();
	},

	showEventDetails: function(reminder) {
		this.controller.serviceRequest('palm://com.palm.applicationManager', {
					                    method: 'open',
					                    parameters: {
													'id': 'com.palm.app.calendar',
													'params': {'details': reminder.id.toString() }	
													},
					                	});
	},
	
	remindersUpdated: function() {
		this.update();
	},
	
});
