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

var Alarms = Class.create({
	initialize: function() {
		this.alarms = [];
		this.alarmIDs = [];
		
		this.alarmsCookieModel = new Mojo.Model.Cookie(Alarms.kAlarmCookie);
		this.alarmsCookie = this.alarmsCookieModel.get();
		if(this.alarmsCookie){
			//console.log('loaded alarm from cookie');
			this.alarmIDs = this.alarmsCookie.alarmIDs;
		}else{
			//this.alarmIDs = [{'id':0}];
			//console.log('loaded default alarm');
			//this.saveAlarms();
		}
		
		//load all the alarms
		this.alarmIDs.each(this.getAlarmFromAlarmID.bind(this));
	},
	
	getAlarmFromAlarmID: function(alarmID){
		this.alarms.push(new Alarm(alarmID.id));	
	},
	
	getAlarmFromID: function(alarmID){
		var newAlarm = new Alarm(alarmID);
		this.alarms.push(newAlarm);
		return newAlarm;
	},
	
	// returns the number of alarms that are currently active
	countEnabledAlarms: function() {
		var count = 0;
		
		this.alarms.each(function(alarm) {
			if (alarm.enabled)
				count++;
		})		
		
		return count;
	},
	
	// returns the next alarm that will ring
	getEarliestEnabledAlarm: function() {
		var earliestAlarm;
		var earliestAlarmDaysUntil = 8;
		var earliestAlarmHours = 24;
		var earliestAlarmMinutes = 60;
		
		this.alarms.each(function(alarm) {
			var daysUntil;
			if (alarm.enabled) {
				daysUntil = alarm.daysUntilAlarm();
				var earlier = false;
				// if this alarm happens on a day sooner than the current earliest
				// alarm, or if it happens on the same day, but earlier, then save
				// this as the earliest alarm
				if (daysUntil < earliestAlarmDaysUntil 
					|| (daysUntil == earliestAlarmDaysUntil
						&& (alarm.time.getHours() < earliestAlarmHours
							|| (alarm.time.getHours() == earliestAlarmHours 
								&& alarm.time.getMinutes() < earliestAlarmMinutes)))) {
					earliestAlarmDaysUntil = daysUntil;
					earliestAlarmHours = alarm.time.getHours();
					earliestAlarmMinutes = alarm.time.getMinutes();
					earliestAlarm = alarm;
				}
			}
		})
		
		return earliestAlarm
	},
	
	findAlarmByID: function(alarmID) {
		var result;
		
		result = this.alarms.find(function(alarm) { 
			if (alarm.id == alarmID)
				return true;
		});
		
		return result;
		
	},
	
	refreshAlarmIDs: function(){
		this.alarmIDs = [];
		for(var i=0;i<this.alarms.length;i++){
			this.alarmIDs[i] = {'id':this.alarms[i].id};
			//console.log('hello'+i + ' ' + this.alarmIDs[i].id);
			//this.alarmIDs[i] = {'id':i};
		}
	},
	
	saveAlarms: function() {
		var lastSaved = new Date();
		this.refreshAlarmIDs();
		this.alarmsCookieModel.put({'alarmIDs':this.alarmIDs,'lastSaved':lastSaved.toString()});
	},
	
	deleteAlarm: function(item){
		//console.log("delete alarm with id = "+item.id);
		item.destroyMyself();
		this.alarms.splice(this.alarms.indexOf(item), 1);
		this.saveAlarms();
	},
	
	findNextNewSafeID: function(id){
		var idExists = this.alarms.find(function(alarm){
			return alarm.id == id; 
		});
		if(idExists){
			//console.log("alarm " + id+ " already exists");
			id++;
			id = this.findNextNewSafeID(id);
		}
		return id;
	},
	
	addAlarm: function(id) {
		id = this.findNextNewSafeID(id);
		//console.log("adding new alarm " + id);
		var newAlarm = this.getAlarmFromID(id);
		this.saveAlarms();
		return newAlarm;
	},
	
});

Alarms.kAlarmCookie = "alarmscookie";
Alarms.localizedOccurs = {
	"daily": $L("Daily"),
	"once": $L("     "), 
	"weekdays": $L("Weekdays"),
	"weekends": $L("Weekends")
}
