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

var DashboardAssistant = Class.create({
	
	initialize: function() {
		Mojo.Log.info("$$$$$$$$ Dashboard-assistant intialize: start");
		this.requests = [];
		this.reminders = getReminderManager();
	},

	setup: function() {
		Mojo.Log.info("$$$$$$$$ Dashboard-assistant setup:");
		this.reminders.observeReminders('dashboard', this);
		
		this.service = getCalendarService();
		
		this.controller.get('calendar-dashboard').addEventListener(Mojo.Event.tap, this.tapHandler.bindAsEventListener(this));
		this.controller.get('dashboard-icon').addEventListener(Mojo.Event.tap, this.reminderList.bindAsEventListener(this));
		this.controller.get('dashboard-count').addEventListener(Mojo.Event.tap, this.reminderList.bindAsEventListener(this));

		this.update();
	},
	
	cleanup: function() {
		Mojo.Log.info("$$$$$$$$ Dashboard-assistant cleanup: start ");
		this.reminders.stopObservingReminders('dashboard');
		this.reminders.removeAllReminders();
		Mojo.Log.info("$$$$$$$$ Dashboard-assistant cleanup: end ");
	},
	
	updateMostRecentReminder: function() {
		Mojo.Log.info("$$$$$$$$ Dashboard-assistant updateMostRecentReminder: start");
		this.reminder = this.reminders.getMostRecentReminder();
		if (this.reminder) {
			Mojo.Log.info("$$$$$$$$ Dashboard-assistant updateMostRecentReminder: recent Reminder is "+this.reminder.id);
			this.updateHTML(this.controller.get('dashboard-subject'), (this.reminder.subject != null) ? this.reminder.subject : '');
			this.updateHTML(this.controller.get('dashboard-timeloc'), (this.reminder.subtitle != null) ? this.reminder.subtitle : '');
		}
		else
		{
			//close it
			Mojo.Log.info("$$$$$$$$ Dashboard-assistant updateMostRecentReminder: closing dashboard as there are no reminders in reminder manager");
			this.controller.window.close();
		}
		Mojo.Log.info("$$$$$$$$ Dashboard-assistant updateMostRecentReminder: end");
	},
	
	updateReminderCount: function() {
		Mojo.Log.info("$$$$$$$$ Dashboard-assistant updateReminderCount: start");
		var count = this.reminders.getNumReminders();
		var countDiv = this.controller.get('dashboard-count');
    	var dashboardDiv = this.controller.get('calendar-dashboard');
    	Mojo.Log.info("$$$$$$$$ Dashboard-assistant updateReminderCount: count is "+count);
		if (count > 1) {
			
			countDiv.style.display = '';     
      		dashboardDiv.removeClassName('single');                          
			this.updateHTML(countDiv, ""+count);
		} else {
			
      		dashboardDiv.addClassName('single');                          
			countDiv.style.display = 'none';
		}
		Mojo.Log.info("$$$$$$$$ Dashboard-assistant updateReminderCount: end");
	},
	
	update: function() {
		Mojo.Log.info("$$$$$$$$ Dashboard-assistant update: start");
		this.updateReminderCount();
		this.updateMostRecentReminder();
		Mojo.Log.info("$$$$$$$$ Dashboard-assistant update: end");
	},
	
	tapHandler: function(event) {
		Mojo.Log.info("$$$$$$$$ Dashboard-assistant tapHandler: start");
		Event.stop(event);
		this.removeSchedulerTask(this.reminder.id);
		//this.reminders.removeReminder(this.reminder.id);
		this.showEventDetails();
		Mojo.Log.info("$$$$$$$$ Dashboard-assistant tapHandler: end");
	},

	reminderList: function(event) {
		Mojo.Log.info("$$$$$$$$ Dashboard-assistant reminderList: start");
		Event.stop(event);
		this.showReminderList();
		Mojo.Log.info("$$$$$$$$ Dashboard-assistant reminderList: end");
	},

	showEventDetails: function() {
		Mojo.Log.info("$$$$$$$$ Dashboard-assistant showEventDetails: start");
		new Mojo.Service.Request('palm://com.palm.applicationManager', {
					                    method: 'open',
					                    parameters: {
													'id': 'com.palm.app.calendar',
													'params': {'alarmsnooze': this.reminder.id,'dontAlert':true}
													},
					                	});
		Mojo.Log.info("$$$$$$$$ Dashboard-assistant showEventDetails: end");
	},
	
	showReminderList: function() {
		Mojo.Log.info("$$$$$$$$ Dashboard-assistant showReminderList: start");
		new Mojo.Service.Request('palm://com.palm.applicationManager', {
					                    method: 'open',
					                    parameters: {
													'id': 'com.palm.app.calendar',
													'params': {'reminders': 'show-all' }	
													},
					                	});
		Mojo.Log.info("$$$$$$$$ Dashboard-assistant showReminderList: end");
	},
	
	removeSchedulerTask: function(reminder) {
		Mojo.Log.info("$$$$$$$$ Dashboard-assistant removeSchedulerTask: start");
		Mojo.Log.info("$$$$$$$$ Dashboard-assistant removeSchedulerTask: removeTask: calendar-snooze-" + reminder.id);
		new Mojo.Service.Request('palm://com.palm.taskScheduler', {
					                    method: 'removeTask',
					                    parameters: {key: 'calendar-snooze-' + reminder.id}
					                	});
		Mojo.Log.info("$$$$$$$$ Dashboard-assistant removeSchedulerTask: end");
	},
	
	remindersUpdated: function() {
		Mojo.Log.info("$$$$$$$$ Dashboard-assistant remindersUpdated: start");
		if (this.reminders.getNumReminders() == 0) {
			Mojo.Log.info("$$$$$$$$ Dashboard-assistant remindersUpdated: window.close()");
			this.controller.window.close();
		} else {
			Mojo.Log.info("$$$$$$$$ Dashboard-assistant remindersUpdated: update()");
			this.update();
		}
		Mojo.Log.info("$$$$$$$$ Dashboard-assistant remindersUpdated: end");
	},
	
	updateHTML: function(element, content) {
		Mojo.Log.info("$$$$$$$$ Dashboard-assistant updateHTML: start");
		element.innerHTML = content.escapeHTML();
		return element;
	},
	
});

