public static async Task LoadRemindersAsync(CalendarAwareDayInfo dayInfo)
        {
            if (_calendar == null)
                throw new InvalidOperationException();
            List<Appointment> appts = (await _calendar.FindAppointmentsAsync(dayInfo.ContextDate.Date, TimeSpan.FromDays(1))).ToList();
            if (appts.Count == 0)
                return;

            if (dayInfo.IsEmptyDay)
            {
                foreach (Appointment a in appts)
                {
                    Appointment fullAppt = await _calendar.GetAppointmentAsync(a.LocalId);
                    dayInfo.RegularClassesInfo.Add(new CustomInfoStub(dayInfo.ContextDate, fullAppt));
                }
                return;
            }

            int i, j;
            Appointment appt = null;
            TimeSpan apptStart = default(TimeSpan);
            for (i = 0, j = 0; j < appts.Count; )
            {
                if (appt == null)
                {
                    appt = await _calendar.GetAppointmentAsync(appts[j].LocalId);
                    apptStart = appt.StartTime.TimeOfDay;
                }
                CalendarAwareStub stub = dayInfo.RegularClassesInfo[i];
                TimeSpan classStart = stub.StartTime;

                if (apptStart == classStart && GetContextCourse(appt).ClassNumber == stub.ContextCourse.ClassNumber
                        && stub.ApptInfo == null)
                    stub.ApptInfo = new AppointmentInfo(appt);
                else if (apptStart <= classStart)
                    dayInfo.RegularClassesInfo.Insert(i++, new CustomInfoStub(dayInfo.ContextDate, appt));
                else
                {
                    if (i < dayInfo.RegularClassesInfo.Count - 1)
                    {
                        i++;
                        continue;
                    }
                    else
                        dayInfo.RegularClassesInfo.Add(new CustomInfoStub(dayInfo.ContextDate, appt));
                }
                j++;
                appt = null;
            }
        }
        private void Pivot_PivotItemLoading(Pivot sender, PivotItemEventArgs args)
        {
            int curIndex = sender.SelectedIndex;
            for (int i = curIndex + 1; i < curIndex + (BUFFER_SIZE - 1); i++)
            {
                _dates[i % BUFFER_SIZE] = _dates[curIndex].AddDays(i - curIndex);
            }
            int previousIndex = (curIndex + (BUFFER_SIZE - 1)) % BUFFER_SIZE;
            _dates[previousIndex] = _dates[curIndex].AddDays(-1);
            for (int i = 0; i < BUFFER_SIZE; i++)
            {
                string header;
                DateTimeOffset date = DateTimeOffset.Now.Date;
                if (_dates[i].Date == date)
                    header = "today";
                else if (_dates[i].AddDays(-1).Date == date)
                    header = "tomorrow";
                else if (_dates[i].AddDays(1).Date == date)
                    header = "yesterday";
                else
                    header = _dates[i].ToString("ddd dd").ToLower();

                (sender.Items[i] as PivotItem).Header = header;
            }

            CurrentDate = _dates[curIndex];
            AwareDayInfo = new CalendarAwareDayInfo(CurrentDate);
            (sender.Items[curIndex] as PivotItem).DataContext = AwareDayInfo;
        }
        private async void SetReminderButton_Click(object sender, RoutedEventArgs e)
        {
            ReminderControl reminderControl = (reminderContentControl.ContentTemplateRoot as FrameworkElement).FindName("reminderControl") as ReminderControl;
            if (reminderControl.Agenda == "")
            {
                await new MessageDialog("Please select or type an agenda for the reminder.", "Missing Input").ShowAsync();
                return;
            }
            else
            {
                if (CurrentStub.ApptInfo != null)
                    await CalendarManager.ModifyAppointmentAsync(CurrentStub.ApptInfo.LocalId, reminderControl.Agenda,
                                                                 reminderControl.ContextDate, reminderControl.StartTime,
                                                                 reminderControl.Duration, reminderControl.Reminder);
                else
                    await CalendarManager.WriteAppointmentAsync(CurrentStub.ContextCourse, reminderControl.Agenda,
                                                                reminderControl.ContextDate, reminderControl.StartTime,
                                                                reminderControl.Duration, reminderControl.Reminder);
            }

            ReminderSetupVisible = false;
            CurrentStub = null;
            rootPivot.Visibility = Windows.UI.Xaml.Visibility.Visible;

            AwareDayInfo = new CalendarAwareDayInfo(CurrentDate);
            (rootPivot.Items[rootPivot.SelectedIndex] as PivotItem).DataContext = AwareDayInfo;
        }
        public async void LoadView(string parameter, Dictionary<string, object> lastState = null)
        {
            try
            {
                addMenuFlyout.Hide();
                modifyMenuFlyout.Hide();
                if (_datePickerFlyout != null)
                    _datePickerFlyout.Hide();
                if (_listPickerFlyout != null)
                    _listPickerFlyout.Hide();

                if (ContentReady == true)
                {
                    AwareDayInfo = new CalendarAwareDayInfo(CurrentDate);
                    (rootPivot.Items[rootPivot.SelectedIndex] as PivotItem).DataContext = AwareDayInfo;
                    return;
                }

                DateTimeOffset startDate;
                // Restore last state if available.
                if (lastState != null)
                    startDate = (DateTimeOffset)lastState["selectedDate"];
                else
                    startDate = DateTimeOffset.Now;

                List<PivotItem> pivotItems = new List<PivotItem>(BUFFER_SIZE);
                for (int i = 0; i < BUFFER_SIZE; i++)
                    pivotItems.Add(new PivotItem());
                _dates[0] = startDate;
                rootPivot.PivotItemLoading += Pivot_PivotItemLoading;
                rootPivot.ItemsSource = pivotItems;
                await CalendarManager.LoadCalendarAsync();
                ContentReady = true;
            }
            catch { }
        }