public NewCalendarEvent(CalendarEvent calEvent)
 {
     if (calEvent != null)
     {
         this.RDNationLink = calEvent.RDNationLink;
         this.TicketUrl = calEvent.TicketUrl;
         this.EventType = calEvent.EventType;
         this.Attendees = calEvent.Attendees.OrderBy(x => x.MemberName).ToList();
         this.IsPublicEvent = calEvent.IsPublicEvent;
         this.CalendarItemId = calEvent.CalendarItemId;
         this.CalendarId = calEvent.CalendarId;
         this.CalendarType = calEvent.CalendarType;
         this.EndDate = calEvent.EndDate;
         this.EndDateDisplay = calEvent.EndDate.ToShortDateString() + " " + calEvent.EndDate.ToShortTimeString();
         this.StartDate = calEvent.StartDate;
         this.StartDateDisplay = calEvent.StartDate.ToShortDateString() + " " + calEvent.StartDate.ToShortTimeString();
         this.Link = calEvent.Link;
         this.Location = calEvent.Location;
         this.Name = calEvent.Name;
         this.Notes = calEvent.Notes;
         this.NotesHtml = calEvent.NotesHtml;
         this.IsReoccurring = calEvent.IsReoccurring;
         this.CalendarReoccurringId = calEvent.CalendarReoccurringId;
         this.EndDateReoccurring = calEvent.EndDateReoccurring;
         this.EndDateReoccurringDisplay = calEvent.EndDateReoccurringDisplay;
         this.StartDateReoccurring = calEvent.StartDateReoccurring;
         this.StartDateReoccurringDisplay = calEvent.StartDateReoccurringDisplay;
         this.EventType.CalendarEventTypeId = calEvent.EventType.CalendarEventTypeId;
         this.NextEventId = calEvent.NextEventId;
         this.PreviousEventId = calEvent.PreviousEventId;
         this.GoogleCalendarUrl = calEvent.GoogleCalendarUrl;
         this.ColorTempSelected = calEvent.ColorTempSelected;
         this.MembersApartOfEvent = calEvent.MembersApartOfEvent;
         this.MembersToCheckIn = calEvent.MembersToCheckIn;
         this.GroupsForEvent = calEvent.GroupsForEvent;
         foreach (var group in calEvent.GroupsForEvent)
         {
             this.ToGroupIds += group.Id + ",";
         }
         if (calEvent.EventReoccurring != null)
         {
             this.IsReoccurring = true;
             this.IsSunday = calEvent.EventReoccurring.DaysOfWeekOptions.HasFlag(DayOfWeekEnum.Sun);
             this.IsMonday = calEvent.EventReoccurring.DaysOfWeekOptions.HasFlag(DayOfWeekEnum.Mon);
             this.IsTuesday = calEvent.EventReoccurring.DaysOfWeekOptions.HasFlag(DayOfWeekEnum.Tue);
             this.IsWednesday = calEvent.EventReoccurring.DaysOfWeekOptions.HasFlag(DayOfWeekEnum.Wed);
             this.IsThursday = calEvent.EventReoccurring.DaysOfWeekOptions.HasFlag(DayOfWeekEnum.Thu);
             this.IsFriday = calEvent.EventReoccurring.DaysOfWeekOptions.HasFlag(DayOfWeekEnum.Fri);
             this.IsSaturday = calEvent.EventReoccurring.DaysOfWeekOptions.HasFlag(DayOfWeekEnum.Sat);
             this.RepeatsFrequencySelectedId = calEvent.EventReoccurring.Frequency.ToString();
             this.MonthlyIntervalId = calEvent.EventReoccurring.MonthlyInterval.ToString();
             if (this.EndDateReoccurring.HasValue)
             {
                 this.EndsWhen =RDN.Portable.Classes.Controls.Calendar.Enums. EndsWhenReoccuringEnum.On.ToString();
             }
             else
                 this.EndsWhen = RDN.Portable.Classes.Controls.Calendar.Enums.EndsWhenReoccuringEnum.Never.ToString();
         }
     }
 }
        /// <summary>
        /// any changes made here need to be mirrored in getcalendarscheduleForView
        /// </summary>
        /// <param name="id"></param>
        /// <param name="ownerEntity"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public static RDN.Portable.Classes.Controls.Calendar.Calendar GetCalendarSchedule(Guid id, CalendarOwnerEntityEnum ownerEntity, DateTime startDate, DateTime endDate, Guid memberId, bool isAttendanceManagerOrBetter)
        {
            RDN.Portable.Classes.Controls.Calendar.Calendar newCal = new RDN.Portable.Classes.Controls.Calendar.Calendar();
            try
            {
                var dc = new ManagementContext();
                var calDb = (from xx in dc.Calendar.Include("CalendarEventsReocurring").Include("CalendarEvents").Include("CalendarEvents.ReocurringEvent").Include("CalendarEvents.PointsForEvent")
                             where xx.CalendarId == id
                             select new
                             {
                                 xx.IsCalendarInUTC,
                                 xx.TimeZone,
                                 Events = xx.CalendarEvents.Where(x => x.StartDate >= startDate && x.EndDate <= endDate && x.IsRemovedFromCalendar == false),
                                 FedOwners = xx.FederationOwners,
                                 LeagueOwners = xx.LeagueOwners,
                                 AllowSelfCheckIn = xx.AllowSelfCheckIn,
                                 xx.DisableBirthdaysFromShowing,
                                 xx.DisableStartSkatingDays,
                                 EventsReocurring = xx.CalendarEventsReocurring.Where(x => (x.EndReocurring >= startDate || x.EndReocurring == null) && x.StartReocurring <= endDate && x.IsRemovedFromCalendar == false),
                             }).FirstOrDefault();
                newCal.IsCalendarInUTC = calDb.IsCalendarInUTC;
                newCal.TimeZone = calDb.TimeZone;
                newCal.CalendarId = id;
                newCal.OwnerEntity = ownerEntity;
                newCal.AllowSelfCheckIn = calDb.AllowSelfCheckIn;
                newCal.DisableBirthdays = calDb.DisableBirthdaysFromShowing;
                newCal.DisableSkatingStartDates = calDb.DisableStartSkatingDays;
                if (ownerEntity == CalendarOwnerEntityEnum.federation)
                {
                    foreach (var own in calDb.FedOwners)
                    {
                        CalendarOwner owner = new CalendarOwner();
                        owner.OwnerId = own.Federation.FederationId;
                        owner.OwnerName = own.Federation.Name;
                        newCal.EntityName = own.Federation.Name;

                        newCal.Owners.Add(owner);
                    }
                }
                else if (ownerEntity == CalendarOwnerEntityEnum.league)
                {
                    foreach (var own in calDb.LeagueOwners)
                    {
                        CalendarOwner owner = new CalendarOwner();
                        owner.OwnerId = own.League.LeagueId;
                        owner.OwnerName = own.League.Name;
                        newCal.EntityName = own.League.Name;
                        newCal.Owners.Add(owner);
                    }
                }

                foreach (var ev in calDb.Events)
                {
                    newCal.Events.Add(CalendarEventFactory.DisplayEvent(ev, memberId, isAttendanceManagerOrBetter));
                }
                foreach (var ev in calDb.EventsReocurring)
                {
                    //used so we can refresh the last day events were created..

                    // ev.LastDateEventsWereCreated = endDate.AddMonths(-1);
                    if (ev.LastDateEventsWereCreated.GetValueOrDefault() < endDate)
                    {
                        var eventRe = dc.CalendarEventsReocurring.Where(x => x.CalendarItemId == ev.CalendarItemId && x.IsRemovedFromCalendar == false).FirstOrDefault();
                        eventRe.LastDateEventsWereCreated = endDate;
                        eventRe.Calendar = eventRe.Calendar;
                        dc.SaveChanges();

                        var aEvent = new CalendarViewEventJson()
                        {
                            id = ev.CalendarItemId,
                            Title = ev.Name,
                            Frequency = ev.FrequencyReocurring,
                            DaysOfWeek = ev.DaysOfWeekReocurring,
                            MonthlyInterval = ev.MonthlyIntervalReocurring
                        };
                        var schedule = new Schedule(aEvent);

                        var range = new DateRange()
                        {
                            StartDateTime = ev.StartReocurring
                        };
                        //date is null if the event is never ending.
                        if (ev.EndReocurring.HasValue)
                            range.EndDateTime = ev.EndReocurring.Value;
                        else
                            range.EndDateTime = endDate.AddMonths(1);

                        foreach (var date in schedule.Occurrences(range))
                        {
                            CalendarEvent calEvent = new CalendarEvent();
                            Guid locationId = new Guid();
                            if (ev.Location != null)
                            {
                                calEvent.Location.LocationName = ev.Location.LocationName;
                                locationId = ev.Location.LocationId;
                            }
                            calEvent.Name = ev.Name;
                            calEvent.CalendarItemId = ev.CalendarItemId;
                            if (eventRe.Color != null)
                            {
                                var c = Color.FromArgb(eventRe.Color.ColorIdCSharp);
                                calEvent.ColorTempSelected = ColorTranslator.ToHtml(c);
                            }

                            if (!ev.IsInUTCTime)
                            {
                                calEvent.StartDate = new DateTime(date.Year, date.Month, date.Day, ev.StartDate.Hour, ev.StartDate.Minute, ev.StartDate.Second);
                                calEvent.EndDate = new DateTime(date.Year, date.Month, date.Day, ev.EndDate.Hour, ev.EndDate.Minute, ev.EndDate.Second);
                            }
                            else
                            {
                                //we have to create a temp dates so we can add the timezone information without going back a day
                                //if the time being used is on the border.
                                //without the tempdates 1/4/2013 7pm turned into 1/3/2013 7pm because the timezones didn't account for the 
                                //fact the dates were already in utc.
                                var startTempDate = new DateTime(date.Year, date.Month, date.Day, ev.StartDate.Hour, ev.StartDate.Minute, ev.StartDate.Second) + new TimeSpan(ev.Calendar.TimeZone, 0, 0);
                                var endTempDate = new DateTime(date.Year, date.Month, date.Day, ev.EndDate.Hour, ev.EndDate.Minute, ev.EndDate.Second) + new TimeSpan(ev.Calendar.TimeZone, 0, 0);
                                calEvent.StartDate = new DateTime(date.Year, date.Month, date.Day, startTempDate.Hour, startTempDate.Minute, startTempDate.Second);
                                calEvent.EndDate = new DateTime(date.Year, date.Month, date.Day, endTempDate.Hour, endTempDate.Minute, endTempDate.Second);
                            }

                            calEvent.Notes = ev.Notes;
                            calEvent.AllowSelfCheckIn = ev.AllowSelfCheckIn;

                            //var evs = (from xx in newCal.Events
                            //           where xx.CalendarReoccurringId == ev.CalendarItemId
                            //           where xx.StartDate == calEvent.StartDate
                            //           where xx.EndDate == calEvent.EndDate
                            //           select xx).FirstOrDefault();
                            ////dirty bit to check if event was already in list.  If it is, we don't add it.
                            ////the date check here is to only create events within the calendar selected dates
                            //if (evs == null && calEvent.StartDate >= startDate && calEvent.EndDate <= endDate)
                            //{
                            //    string colorHex = String.Empty;
                            //    if (ev.Color != null)
                            //    {
                            //        var c = Color.FromArgb(ev.Color.ColorIdCSharp);
                            //        colorHex = ColorTranslator.ToHtml(c);
                            //    }
                            //    Guid calItemId = CalendarEventFactory.CreateNewEvent(ev.Calendar.CalendarId, calEvent.StartDate, calEvent.EndDate, locationId, ev.Name, ev.Link, ev.Notes, ev.AllowSelfCheckIn, ev.IsPublic, true, calEvent.CalendarItemId, ev.EventType.CalendarEventTypeId, false, ev.TicketUrl, colorHex, new List<long>(), memberId);
                            //    calEvent.CalendarItemId = calItemId;
                            //    newCal.Events.Add(calEvent);
                            //}
                        }

                    }
                }
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return newCal;
        }
        public static RDN.Library.Classes.Controls.Calendar.CalendarEvent GetEvent(Guid eventId, Guid currentMemberId, Guid calendarId = new Guid())
        {
            RDN.Library.Classes.Controls.Calendar.CalendarEvent ev = new RDN.Library.Classes.Controls.Calendar.CalendarEvent();
            try
            {
                var dc = new ManagementContext();
                var e = (from xx in dc.CalendarEvents.Include("Location").Include("Location.Contact").Include("Location.Contact.Addresses").Include("Location.Contact.Communications")
                         where xx.Calendar.CalendarId == calendarId
                         where xx.CalendarItemId == eventId
                         where xx.IsRemovedFromCalendar == false
                         select new
                         {
                             xx.TicketUrl,
                             xx.IsInUTCTime,
                             xx.Calendar.TimeZone,
                             xx.Calendar.IsCalendarInUTC,
                             xx.AllowSelfCheckIn,
                             xx.CalendarItemId,
                             xx.Calendar.LeagueOwners,
                             xx.EndDate,
                             xx.Link,
                             xx.Name,
                             xx.Notes,
                             xx.StartDate,
                             xx.Attendees,
                             xx.Location,
                             xx.IsPublicEvent,
                             contactCart = xx.Location.Contact,
                             addresses = xx.Location.Contact.Addresses,
                             coms = xx.Location.Contact.Communications,
                             xx.PointsForEvent,
                             xx.EventType,
                             xx.ReocurringEvent,
                             nextEventId = dc.CalendarEvents.Where(x => x.Calendar.CalendarId == calendarId && x.IsRemovedFromCalendar == false && x.StartDate >= xx.StartDate && x.CalendarItemId != eventId).OrderBy(x => x.StartDate).Select(x => x.CalendarItemId).FirstOrDefault(),
                             previousEventId = dc.CalendarEvents.Where(x => x.Calendar.CalendarId == calendarId && x.IsRemovedFromCalendar == false && x.StartDate <= xx.StartDate && x.CalendarItemId != eventId).OrderByDescending(x => x.StartDate).Select(x => x.CalendarItemId).FirstOrDefault(),
                             nextStartDate = dc.CalendarEvents.Where(x => x.Calendar.CalendarId == calendarId && x.IsRemovedFromCalendar == false && x.StartDate >= xx.StartDate && x.CalendarItemId != eventId).OrderBy(x => x.StartDate).Select(x => x.StartDate).FirstOrDefault(),
                             previousStartDate = dc.CalendarEvents.Where(x => x.Calendar.CalendarId == calendarId && x.IsRemovedFromCalendar == false && x.StartDate <= xx.StartDate && x.CalendarItemId != eventId).OrderByDescending(x => x.StartDate).Select(x => x.StartDate).FirstOrDefault(),
                             xx.LastModified,
                             xx.Created,
                             xx.Color,
                             xx.Groups
                         }).FirstOrDefault();
                if (calendarId == new Guid() && e == null)
                {
                    e = (from xx in dc.CalendarEvents.Include("Location").Include("Location.Contact").Include("Location.Contact.Addresses").Include("Location.Contact.Communications")
                         where xx.CalendarItemId == eventId
                         where xx.IsRemovedFromCalendar == false
                         select new
                         {
                             xx.TicketUrl,
                             xx.IsInUTCTime,
                             xx.Calendar.TimeZone,
                             xx.Calendar.IsCalendarInUTC,
                             xx.AllowSelfCheckIn,
                             xx.CalendarItemId,
                             xx.Calendar.LeagueOwners,
                             xx.EndDate,
                             xx.Link,
                             xx.Name,
                             xx.Notes,
                             xx.StartDate,
                             xx.Attendees,
                             xx.Location,
                             xx.IsPublicEvent,
                             contactCart = xx.Location.Contact,
                             addresses = xx.Location.Contact.Addresses,
                             coms = xx.Location.Contact.Communications,
                             xx.PointsForEvent,
                             xx.EventType,
                             xx.ReocurringEvent,
                             nextEventId = dc.CalendarEvents.Where(x => x.Calendar.CalendarId == calendarId && x.IsRemovedFromCalendar == false && x.StartDate >= xx.StartDate && x.CalendarItemId != eventId).OrderBy(x => x.StartDate).Select(x => x.CalendarItemId).FirstOrDefault(),
                             previousEventId = dc.CalendarEvents.Where(x => x.Calendar.CalendarId == calendarId && x.IsRemovedFromCalendar == false && x.StartDate <= xx.StartDate && x.CalendarItemId != eventId).OrderByDescending(x => x.StartDate).Select(x => x.CalendarItemId).FirstOrDefault(),
                             nextStartDate = dc.CalendarEvents.Where(x => x.Calendar.CalendarId == calendarId && x.IsRemovedFromCalendar == false && x.StartDate >= xx.StartDate && x.CalendarItemId != eventId).OrderBy(x => x.StartDate).Select(x => x.StartDate).FirstOrDefault(),
                             previousStartDate = dc.CalendarEvents.Where(x => x.Calendar.CalendarId == calendarId && x.IsRemovedFromCalendar == false && x.StartDate <= xx.StartDate && x.CalendarItemId != eventId).OrderByDescending(x => x.StartDate).Select(x => x.StartDate).FirstOrDefault(),
                             xx.LastModified,
                             xx.Created,
                             xx.Color,
                             xx.Groups
                         }).FirstOrDefault();

                }

                if (e == null)
                {
                    return GetEventReocurring(calendarId, eventId);
                }
                //need to get the league first for the calendar.
                foreach (var owner in e.LeagueOwners)
                {
                    if (owner.League != null)
                    {
                        ev.OrganizersId = owner.League.LeagueId;
                        ev.OrganizersName = owner.League.Name;
                        break;
                    }
                }
                //adding the members of the event to it.
                foreach (var g in e.Groups)
                {
                    var gTemp = League.Classes.LeagueGroupFactory.DisplayGroup(g.Group);
                    ev.GroupsForEvent.Add(gTemp);
                    foreach (var mem in gTemp.GroupMembers)
                    {
                        if (ev.MembersApartOfEvent.Where(x => x.MemberId == mem.MemberId).FirstOrDefault() == null)
                        {
                            RDN.Portable.Classes.Controls.Calendar.CalendarAttendance a = new RDN.Portable.Classes.Controls.Calendar.CalendarAttendance();
                            a.MemberId = mem.MemberId;
                            a.MemberName = mem.DerbyName;
                            a.IsCheckedIn = false;
                            a.FullName = mem.Firstname + " " + mem.LastName;
                            a.MemberNumber = mem.PlayerNumber;
                            ev.MembersApartOfEvent.Add(a);
                            ev.MembersToCheckIn.Add(a);
                        }
                    }
                }
                //if the event had no groups attached, we add the league members to the event.
                if (ev.MembersApartOfEvent.Count == 0)
                {
                    var members = League.LeagueFactory.GetLeagueMembers(ev.OrganizersId);
                    for (int i = 0; i < members.Count; i++)
                    {
                        RDN.Portable.Classes.Controls.Calendar.CalendarAttendance a = new Portable.Classes.Controls.Calendar.CalendarAttendance();
                        a.MemberId = members[i].MemberId;
                        a.MemberName = members[i].DerbyName;
                        a.IsCheckedIn = false;
                        a.FullName = members[i].Firstname + " " + members[i].LastName;
                        a.MemberNumber = members[i].PlayerNumber;
                        ev.MembersApartOfEvent.Add(a);
                        ev.MembersToCheckIn.Add(a);
                    }
                }
                //event type needs to stay above attendees since attendees relies on event type.
                ev.EventType = new Portable.Classes.Controls.Calendar.CalendarEventType();
                if (e.EventType != null)
                {
                    ev.EventType.EventTypeName = e.EventType.EventTypeName;
                    ev.EventType.PointsForExcused = e.EventType.PointsForExcused;
                    ev.EventType.PointsForNotPresent = e.EventType.PointsForNotPresent;
                    ev.EventType.PointsForPartial = e.EventType.PointsForPartial;
                    ev.EventType.PointsForPresent = e.EventType.PointsForPresent;
                    ev.EventType.PointsForTardy = e.EventType.PointsForTardy;
                    ev.EventType.CalendarEventTypeId = e.EventType.CalendarEventTypeId;
                }


                //add the attendees to the event.
                foreach (var att in e.Attendees)
                {
                    RDN.Portable.Classes.Controls.Calendar.CalendarAttendance a = new Portable.Classes.Controls.Calendar.CalendarAttendance()
                    {
                        AttedanceId = att.CalendarAttendanceId,
                        MemberId = att.Attendant.MemberId,
                        FullName = att.Attendant.Firstname + " " + att.Attendant.Lastname,
                        MemberName = att.Attendant.DerbyName,
                        Note = att.Note,
                        PointType = (CalendarEventPointTypeEnum)Enum.Parse(typeof(CalendarEventPointTypeEnum), att.PointTypeEnum.ToString()),
                        SecondaryPointType = (CalendarEventPointTypeEnum)Enum.Parse(typeof(CalendarEventPointTypeEnum), att.SecondaryPointTypeEnum.ToString()),
                        MemberNumber = att.Attendant.PlayerNumber,
                        AdditionalPoints = att.AdditionalPoints,
                        Availability = (AvailibilityEnum)Enum.Parse(typeof(AvailibilityEnum), att.AvailibityEnum.ToString()),
                        AvailableNotes = att.AvailabilityNote,
                    };

                    switch (a.PointType)
                    {
                        case CalendarEventPointTypeEnum.Present:
                            if (a.SecondaryPointType == CalendarEventPointTypeEnum.Tardy && a.AdditionalPoints == 0)
                                a.PointsStringForReading = ev.EventType.PointsForPresent + " + " + ev.EventType.PointsForTardy + " = " + (ev.EventType.PointsForPresent + ev.EventType.PointsForTardy);
                            else if (a.SecondaryPointType == CalendarEventPointTypeEnum.Tardy)
                                a.PointsStringForReading = ev.EventType.PointsForPresent + " + " + a.AdditionalPoints + " + " + ev.EventType.PointsForTardy + " = " + (ev.EventType.PointsForPresent + ev.EventType.PointsForTardy + a.AdditionalPoints);
                            else if (a.AdditionalPoints != 0)
                                a.PointsStringForReading = ev.EventType.PointsForPresent + " + " + a.AdditionalPoints + " = " + (ev.EventType.PointsForPresent + a.AdditionalPoints);
                            else
                                a.PointsStringForReading = ev.EventType.PointsForPresent.ToString();
                            break;
                        case CalendarEventPointTypeEnum.Partial:
                            if (a.SecondaryPointType == CalendarEventPointTypeEnum.Tardy && a.AdditionalPoints == 0)
                                a.PointsStringForReading = ev.EventType.PointsForPartial + " + " + ev.EventType.PointsForTardy + " = " + (ev.EventType.PointsForPartial + ev.EventType.PointsForTardy);
                            else if (a.SecondaryPointType == CalendarEventPointTypeEnum.Tardy)
                                a.PointsStringForReading = ev.EventType.PointsForPartial + " + " + a.AdditionalPoints + " + " + ev.EventType.PointsForTardy + " = " + (ev.EventType.PointsForPartial + ev.EventType.PointsForTardy + a.AdditionalPoints);
                            else if (a.AdditionalPoints != 0)
                                a.PointsStringForReading = ev.EventType.PointsForPartial + " + " + a.AdditionalPoints + " = " + (ev.EventType.PointsForPartial + a.AdditionalPoints);
                            else
                                a.PointsStringForReading = ev.EventType.PointsForPartial.ToString();
                            break;
                        case CalendarEventPointTypeEnum.Not_Present:
                            if (a.SecondaryPointType == CalendarEventPointTypeEnum.Tardy && a.AdditionalPoints == 0)
                                a.PointsStringForReading = ev.EventType.PointsForNotPresent + " + " + ev.EventType.PointsForTardy + " = " + (ev.EventType.PointsForNotPresent + ev.EventType.PointsForTardy);
                            else if (a.SecondaryPointType == CalendarEventPointTypeEnum.Tardy)
                                a.PointsStringForReading = ev.EventType.PointsForNotPresent + " + " + a.AdditionalPoints + " + " + ev.EventType.PointsForTardy + " = " + (ev.EventType.PointsForNotPresent + ev.EventType.PointsForTardy + a.AdditionalPoints);
                            else if (a.AdditionalPoints != 0)
                                a.PointsStringForReading = ev.EventType.PointsForNotPresent + " + " + a.AdditionalPoints + " = " + (ev.EventType.PointsForNotPresent + a.AdditionalPoints);
                            else
                                a.PointsStringForReading = ev.EventType.PointsForNotPresent.ToString();
                            break;
                        case CalendarEventPointTypeEnum.Excused:
                            if (a.SecondaryPointType == CalendarEventPointTypeEnum.Tardy && a.AdditionalPoints == 0)
                                a.PointsStringForReading = ev.EventType.PointsForExcused + " + " + ev.EventType.PointsForTardy + " = " + (ev.EventType.PointsForExcused + ev.EventType.PointsForTardy);
                            else if (a.SecondaryPointType == CalendarEventPointTypeEnum.Tardy)
                                a.PointsStringForReading = ev.EventType.PointsForExcused + " + " + a.AdditionalPoints + " + " + ev.EventType.PointsForTardy + " = " + (ev.EventType.PointsForExcused + ev.EventType.PointsForTardy + a.AdditionalPoints);
                            else if (a.AdditionalPoints != 0)
                                a.PointsStringForReading = ev.EventType.PointsForExcused + " + " + a.AdditionalPoints + " = " + (ev.EventType.PointsForExcused + a.AdditionalPoints);
                            else
                                a.PointsStringForReading = ev.EventType.PointsForExcused.ToString();
                            break;
                    }

                    if (a.PointType != CalendarEventPointTypeEnum.None)
                        a.IsCheckedIn = true;

                    //since the member is attending the event, we remove them from having members to check in.
                    var checkedInMember = ev.MembersToCheckIn.Where(x => x.MemberId == a.MemberId).FirstOrDefault();
                    if (checkedInMember != null)
                        ev.MembersToCheckIn.Remove(checkedInMember);

                    ev.Attendees.Add(a);
                }

                var avail = ev.Attendees.Where(x => x.MemberId == currentMemberId && x.Availability != AvailibilityEnum.None).FirstOrDefault();
                if (avail != null)
                    ev.HasCurrentMemberSetAvailability = true;
                var attend = ev.Attendees.Where(x => x.MemberId == currentMemberId && x.IsCheckedIn == true).FirstOrDefault();
                if (attend != null)
                    ev.IsCurrentMemberCheckedIn = true;
                if (e.Color != null)
                {
                    var c = Color.FromArgb(e.Color.ColorIdCSharp);
                    ev.ColorTempSelected = ColorTranslator.ToHtml(c);
                }
                ev.IsCalendarInUTC = e.IsCalendarInUTC;
                ev.IsInUTCTime = e.IsInUTCTime;
                ev.CalendarTimeZone = e.TimeZone;
                ev.AllowSelfCheckIn = e.AllowSelfCheckIn;
                ev.CalendarItemId = e.CalendarItemId;
                ev.CalendarId = calendarId;
                ev.RDNationLink = ServerConfig.WEBSITE_EVENT_URL + "/" + RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(e.Name) + "/" + e.CalendarItemId.ToString().Replace("-", "");
                if (!ev.IsInUTCTime)
                {
                    ev.EndDate = e.EndDate;
                    ev.StartDate = e.StartDate;
                }
                else
                {
                    ev.StartDate = (e.StartDate + new TimeSpan(e.TimeZone, 0, 0));
                    ev.EndDate = (e.EndDate + new TimeSpan(e.TimeZone, 0, 0));
                }
                if (!String.IsNullOrEmpty(e.Link) && !e.Link.Contains("http://"))
                    ev.Link = "http://" + e.Link;
                else
                    ev.Link = e.Link;
                ev.IsPublicEvent = e.IsPublicEvent;
                ev.TicketUrl = e.TicketUrl;
                if (e.ReocurringEvent != null)
                {
                    ev.CalendarReoccurringId = e.ReocurringEvent.CalendarItemId;
                    ev.IsReoccurring = true;
                }

                GoogleCalendar gc = new GoogleCalendar();
                if (e.Location != null)
                {
                    ev.Location.LocationName = e.Location.LocationName;
                    ev.Location.LocationId = e.Location.LocationId;
                    gc.Location = ev.Location.LocationName + ", ";
                }

                if (e.addresses.FirstOrDefault() != null)
                {
                    Address address = new Address();

                    address.Address1 = e.addresses.FirstOrDefault().Address1;
                    address.Address2 = e.addresses.FirstOrDefault().Address2;
                    address.AddressId = e.addresses.FirstOrDefault().AddressId;
                    address.CityRaw = e.addresses.FirstOrDefault().CityRaw;
                    if (e.addresses.FirstOrDefault().Country != null)
                        address.Country = e.addresses.FirstOrDefault().Country.Code;
                    address.StateRaw = e.addresses.FirstOrDefault().StateRaw;
                    address.Zip = e.addresses.FirstOrDefault().Zip;
                    gc.Location += address.Address1 + " " + address.Address2 + ", " + address.CityRaw + ", " + address.StateRaw + " " + address.Zip;
                    ev.Location.Contact.Addresses.Add(address);
                }
                ev.Name = e.Name;
                ev.NextEventId = e.nextEventId;
                ev.PreviousEventId = e.previousEventId;

                //if two or more events lay on the same StartDate and Time, we do this to Sort them
                //into previous and next so that we can walk through them one by one.
                //before this, if we hit next, we would skip over dates with the same datetime.
                if (ev.NextEventId != ev.PreviousEventId)
                {
                    var oneDateManyEvents = (from xx in dc.CalendarEvents
                                             where xx.Calendar.CalendarId == calendarId
                                             where xx.IsRemovedFromCalendar == false
                                             where xx.StartDate == e.previousStartDate
                                             select xx.CalendarItemId).OrderBy(x => x).ToList();
                    if (oneDateManyEvents.Count > 1)
                    {
                        var indexOfCurrentEvent = oneDateManyEvents.IndexOf(oneDateManyEvents.Where(x => x == ev.PreviousEventId).FirstOrDefault());
                        //if we get to this point, the event always gets the first event with the same times.  
                        //We we need to reverse it to make it get the last event in the sequence.
                        if (indexOfCurrentEvent == 0)
                        {
                            ev.NextEventId = dc.CalendarEvents.Where(x => x.Calendar.CalendarId == calendarId && x.IsRemovedFromCalendar == false && x.StartDate > e.StartDate).OrderBy(x => x.StartDate).Select(x => x.CalendarItemId).FirstOrDefault();
                            ev.PreviousEventId = oneDateManyEvents[oneDateManyEvents.Count - 1];
                        }
                    }
                }
                else
                {
                    try
                    {
                        var oneDateManyEvents = (from xx in dc.CalendarEvents
                                                 where xx.Calendar.CalendarId == calendarId
                                                 where xx.IsRemovedFromCalendar == false
                                                 where xx.StartDate == e.StartDate
                                                 select xx.CalendarItemId).OrderBy(x => x).ToList();
                        var indexOfCurrentEvent = oneDateManyEvents.IndexOf(oneDateManyEvents.Where(x => x == eventId).FirstOrDefault());
                        if (indexOfCurrentEvent == 0)
                        {
                            try
                            {
                                ev.PreviousEventId = dc.CalendarEvents.Where(x => x.Calendar.CalendarId == calendarId && x.IsRemovedFromCalendar == false && x.StartDate < e.StartDate && x.CalendarItemId != eventId).OrderByDescending(x => x.StartDate).Select(x => x.CalendarItemId).FirstOrDefault();
                                if (oneDateManyEvents.Count > indexOfCurrentEvent && oneDateManyEvents.Count > 1)
                                    ev.NextEventId = oneDateManyEvents[indexOfCurrentEvent + 1];
                            }
                            catch (Exception exception)
                            {
                                ErrorDatabaseManager.AddException(exception, exception.GetType(), additionalInformation: indexOfCurrentEvent + " " + oneDateManyEvents.Count);
                            }
                        }
                        else if (indexOfCurrentEvent >= oneDateManyEvents.Count - 1)
                        {
                            if (oneDateManyEvents.Count > 1)
                                ev.PreviousEventId = oneDateManyEvents[indexOfCurrentEvent - 1];
                            ev.NextEventId = dc.CalendarEvents.Where(x => x.Calendar.CalendarId == calendarId && x.IsRemovedFromCalendar == false && x.StartDate > e.StartDate && x.CalendarItemId != eventId).OrderBy(x => x.StartDate).Select(x => x.CalendarItemId).FirstOrDefault();
                        }
                        else
                        {
                            ev.PreviousEventId = oneDateManyEvents[indexOfCurrentEvent - 1];
                            ev.NextEventId = oneDateManyEvents[indexOfCurrentEvent + 1];
                        }
                    }
                    catch (Exception exception)
                    {
                        ErrorDatabaseManager.AddException(exception, exception.GetType());
                    }
                }

                ev.Notes = e.Notes;

                if (e.LastModified > new DateTime(2013, 11, 23) || e.Created > new DateTime(2013, 11, 23))
                {
                    ev.NotesHtml = e.Notes;
                }
                else if (e.Created < new DateTime(2013, 11, 23))
                {
                    if (!String.IsNullOrEmpty(e.Notes))
                    {
                        RDN.Library.Util.MarkdownSharp.Markdown markdown = new RDN.Library.Util.MarkdownSharp.Markdown();
                        markdown.AutoHyperlink = true;
                        markdown.LinkEmails = true;
                        ev.NotesHtml = HtmlSanitize.FilterHtmlToWhitelist(markdown.Transform(e.Notes)).Replace("</p>", "</p><br/>");
                    }
                }

                gc.Description = ev.Notes;
                gc.EndDate = ev.EndDate.ToUniversalTime();
                gc.StartDate = ev.StartDate.ToUniversalTime();
                gc.Website = ev.Link;
                gc.WebsiteName = ev.Link;
                gc.What = ev.Name;


                ev.GoogleCalendarUrl = gc.GetGoogleUrl();
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return ev;
        }
        public static RDN.Library.Classes.Controls.Calendar.CalendarEvent GetStartedSkatingEvent(Guid memberId)
        {
            RDN.Library.Classes.Controls.Calendar.CalendarEvent ev = new RDN.Library.Classes.Controls.Calendar.CalendarEvent();
            try
            {
                var dc = new ManagementContext();
                var mem = MemberCache.GetMemberDisplay(memberId);

                ev.EndDate = mem.StartedSkating.GetValueOrDefault();
                ev.StartDate = mem.StartedSkating.GetValueOrDefault();
                ev.EventType = new Portable.Classes.Controls.Calendar.CalendarEventType();
                ev.EventType.EventType = CalendarEventTypeEnum.StartSkatingDate;
                ev.Name = mem.DerbyName;
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return ev;
        }
        public static RDN.Library.Classes.Controls.Calendar.CalendarEvent GetEventReocurring(Guid eventId, Guid calendarId = new Guid())
        {
            RDN.Library.Classes.Controls.Calendar.CalendarEvent ev = new RDN.Library.Classes.Controls.Calendar.CalendarEvent();
            try
            {
                var dc = new ManagementContext();
                var e = (from xx in dc.CalendarEventsReocurring.Include("Location").Include("Location.Contact").Include("Location.Contact.Addresses").Include("Location.Contact.Communications")
                         where xx.Calendar.CalendarId == calendarId
                         where xx.CalendarItemId == eventId
                         where xx.IsRemovedFromCalendar == false
                         select new
                         {
                             xx.TicketUrl,
                             xx.IsInUTCTime,
                             xx.Calendar.IsCalendarInUTC,
                             xx.Calendar.TimeZone,
                             xx.AllowSelfCheckIn,
                             xx.CalendarItemId,
                             xx.EndDate,
                             xx.Link,
                             xx.Calendar.LeagueOwners,
                             xx.Name,
                             xx.Notes,
                             xx.StartDate,
                             xx.Location,
                             contactCart = xx.Location.Contact,
                             addresses = xx.Location.Contact.Addresses,
                             coms = xx.Location.Contact.Communications,
                             xx.PointsForEvent,
                             xx.EventType,
                             xx.DaysOfWeekReocurring,
                             xx.StartReocurring,
                             xx.EndReocurring,
                             xx.FrequencyReocurring,
                             xx.IsPublic,
                             xx.MonthlyIntervalReocurring,
                             xx.LastModified,
                             xx.Created,
                             xx.Color,
                             xx.Groups
                         }).FirstOrDefault();
                if (calendarId == new Guid() && e == null)
                {
                    e = (from xx in dc.CalendarEventsReocurring.Include("Location").Include("Location.Contact").Include("Location.Contact.Addresses").Include("Location.Contact.Communications")
                         where xx.Calendar.CalendarId == calendarId
                         where xx.CalendarItemId == eventId
                         where xx.IsRemovedFromCalendar == false
                         select new
                         {
                             xx.TicketUrl,
                             xx.IsInUTCTime,
                             xx.Calendar.IsCalendarInUTC,
                             xx.Calendar.TimeZone,
                             xx.AllowSelfCheckIn,
                             xx.CalendarItemId,
                             xx.EndDate,
                             xx.Link,
                             xx.Calendar.LeagueOwners,
                             xx.Name,
                             xx.Notes,
                             xx.StartDate,
                             xx.Location,
                             contactCart = xx.Location.Contact,
                             addresses = xx.Location.Contact.Addresses,
                             coms = xx.Location.Contact.Communications,
                             xx.PointsForEvent,
                             xx.EventType,
                             xx.DaysOfWeekReocurring,
                             xx.StartReocurring,
                             xx.EndReocurring,
                             xx.FrequencyReocurring,
                             xx.IsPublic,
                             xx.MonthlyIntervalReocurring,
                             xx.LastModified,
                             xx.Created,
                             xx.Color,
                             xx.Groups
                         }).FirstOrDefault();
                }

                if (e == null)
                    return null;

                ev.AllowSelfCheckIn = e.AllowSelfCheckIn;
                ev.CalendarItemId = e.CalendarItemId;
                if (e.Color != null)
                {
                    var c = Color.FromArgb(e.Color.ColorIdCSharp);
                    ev.ColorTempSelected = ColorTranslator.ToHtml(c);
                }

                ev.Link = e.Link;

                var aEvent = new ScheduleWidget.ScheduledEvents.Event()
                {
                    Title = e.Name,
                    FrequencyTypeOptions = (FrequencyTypeEnum)Enum.Parse(typeof(FrequencyTypeEnum), e.FrequencyReocurring.ToString()),
                    DaysOfWeek = e.DaysOfWeekReocurring,
                    MonthlyInterval = e.MonthlyIntervalReocurring
                };
                if (e.EndReocurring.HasValue)
                {
                    ev.EndDateReoccurring = e.EndReocurring.GetValueOrDefault();
                    ev.EndDateReoccurringDisplay = e.EndReocurring.GetValueOrDefault().ToShortDateString();
                }
                ev.StartDateReoccurring = e.StartReocurring;
                ev.StartDateReoccurringDisplay = e.StartReocurring.ToShortDateString() + " " + e.StartReocurring.ToShortTimeString();
                ev.EventReoccurring = aEvent;
                ev.TicketUrl = e.TicketUrl;
                ev.RDNationLink = ServerConfig.WEBSITE_EVENT_URL + "/" + RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(e.Name) + "/" + e.CalendarItemId.ToString().Replace("-", "");

                foreach (var owner in e.LeagueOwners)
                {
                    if (owner.League != null)
                    {
                        ev.OrganizersId = owner.League.LeagueId;
                        ev.OrganizersName = owner.League.Name;
                        break;
                    }
                }
                GoogleCalendar gc = new GoogleCalendar();
                if (e.Location != null)
                {
                    ev.Location.LocationName = e.Location.LocationName;
                    ev.Location.LocationId = e.Location.LocationId;
                }
                gc.Location = ev.Location.LocationName + ", ";
                if (e.addresses.FirstOrDefault() != null)
                {
                    RDN.Portable.Classes.ContactCard.Address address = new RDN.Portable.Classes.ContactCard.Address();
                    address.Address1 = e.addresses.FirstOrDefault().Address1;
                    address.Address2 = e.addresses.FirstOrDefault().Address2;
                    address.AddressId = e.addresses.FirstOrDefault().AddressId;
                    address.CityRaw = e.addresses.FirstOrDefault().CityRaw;
                    if (e.addresses.FirstOrDefault().Country != null)
                        address.Country = e.addresses.FirstOrDefault().Country.Code;
                    address.StateRaw = e.addresses.FirstOrDefault().StateRaw;
                    address.Zip = e.addresses.FirstOrDefault().Zip;
                    gc.Location += address.Address1 + " " + address.Address2 + ", " + address.CityRaw + ", " + address.StateRaw + " " + address.Zip;
                    ev.Location.Contact.Addresses.Add(address);
                }
                ev.EventType = new Portable.Classes.Controls.Calendar.CalendarEventType();
                if (e.EventType != null)
                {
                    ev.EventType.EventTypeName = e.EventType.EventTypeName;
                    ev.EventType.PointsForExcused = e.EventType.PointsForExcused;
                    ev.EventType.PointsForNotPresent = e.EventType.PointsForNotPresent;
                    ev.EventType.PointsForPartial = e.EventType.PointsForPartial;
                    ev.EventType.PointsForPresent = e.EventType.PointsForPresent;
                    ev.EventType.PointsForTardy = e.EventType.PointsForTardy;
                    ev.EventType.CalendarEventTypeId = e.EventType.CalendarEventTypeId;
                }
                ev.Name = e.Name;
                ev.Notes = e.Notes;

                foreach (var g in e.Groups)
                {
                    var gTemp = League.Classes.LeagueGroupFactory.DisplayGroup(g.Group);
                    ev.GroupsForEvent.Add(gTemp);
                    foreach (var mem in gTemp.GroupMembers)
                    {
                        if (ev.MembersApartOfEvent.Where(x => x.MemberId == mem.MemberId).FirstOrDefault() == null)
                        {
                            RDN.Portable.Classes.Controls.Calendar.CalendarAttendance a = new Portable.Classes.Controls.Calendar.CalendarAttendance();
                            a.MemberId = mem.MemberId;
                            a.MemberName = mem.DerbyName;
                            a.IsCheckedIn = false;
                            a.FullName = mem.Firstname + " " + mem.LastName;
                            a.MemberNumber = mem.PlayerNumber;
                            ev.MembersApartOfEvent.Add(a);
                            ev.MembersToCheckIn.Add(a);
                        }
                    }
                }

                if (e.LastModified > new DateTime(2013, 11, 23) || e.Created > new DateTime(2013, 11, 23))
                {
                    ev.NotesHtml = e.Notes;
                }
                else if (e.Created < new DateTime(2013, 11, 23))
                {
                    if (!String.IsNullOrEmpty(e.Notes))
                    {
                        RDN.Library.Util.MarkdownSharp.Markdown markdown = new RDN.Library.Util.MarkdownSharp.Markdown();
                        markdown.AutoHyperlink = true;
                        markdown.LinkEmails = true;
                        ev.NotesHtml = HtmlSanitize.FilterHtmlToWhitelist(markdown.Transform(e.Notes)).Replace("</p>", "</p><br/>");
                    }
                }
                ev.IsPublicEvent = e.IsPublic;

                gc.Description = ev.Notes;
                if (!e.IsInUTCTime)
                {
                    ev.EndDate = e.EndDate;
                    ev.StartDate = e.StartDate;
                }
                else
                {
                    ev.StartDate = (e.StartDate + new TimeSpan(e.TimeZone, 0, 0));
                    ev.EndDate = (e.EndDate + new TimeSpan(e.TimeZone, 0, 0));
                }
                gc.EndDate = ev.EndDate.ToUniversalTime();
                gc.StartDate = ev.StartDate.ToUniversalTime();
                gc.Website = ev.Link;
                gc.WebsiteName = ev.Link;
                gc.What = ev.Name;

                ev.GoogleCalendarUrl = gc.GetGoogleUrl();
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return ev;
        }
        public static CalendarEvent DisplayEvent(DataModels.Calendar.CalendarEvent ev, Guid currentMemberId, bool isAttendanceManagerOrBetter)
        {
            CalendarEvent calEvent = new CalendarEvent();
            try
            {
                calEvent.IsAttendanceManagerOrBetter = isAttendanceManagerOrBetter;
                foreach (var att in ev.Attendees)
                {
                    RDN.Portable.Classes.Controls.Calendar.CalendarAttendance a = new Portable.Classes.Controls.Calendar.CalendarAttendance();
                    a.MemberId = att.Attendant.MemberId;
                    a.MemberName = att.Attendant.DerbyName;
                    a.FullName = att.Attendant.Firstname + " " + att.Attendant.Lastname;
                    a.AttedanceId = att.CalendarAttendanceId;
                    a.Note = att.Note;
                    a.PointType = (CalendarEventPointTypeEnum)Enum.Parse(typeof(CalendarEventPointTypeEnum), att.PointTypeEnum.ToString());
                    a.SecondaryPointType = (CalendarEventPointTypeEnum)Enum.Parse(typeof(CalendarEventPointTypeEnum), att.SecondaryPointTypeEnum.ToString());
                    a.Availability = (AvailibilityEnum)Enum.Parse(typeof(AvailibilityEnum), att.AvailibityEnum.ToString());
                    a.AvailableNotes = att.AvailabilityNote;
                    if (a.PointType != CalendarEventPointTypeEnum.None)
                        a.IsCheckedIn = true;
                    calEvent.Attendees.Add(a);
                }

                if (ev.Groups.Count == 0)
                    calEvent.IsCurrentMemberApartOfEvent = true;
                else
                {
                    foreach (var g in ev.Groups)
                    {
                        var gTemp = League.Classes.LeagueGroupFactory.DisplayGroup(g.Group);
                        foreach (var mem in gTemp.GroupMembers)
                        {
                            RDN.Portable.Classes.Controls.Calendar.CalendarAttendance a = new RDN.Portable.Classes.Controls.Calendar.CalendarAttendance();
                            a.MemberId = mem.MemberId;
                            a.MemberName = mem.DerbyName;
                            if (calEvent.MembersApartOfEvent.Where(x => x.MemberId == a.MemberId).FirstOrDefault() == null)
                                calEvent.MembersApartOfEvent.Add(a);
                        }
                    }
                }

                if (ev.EventType != null)
                {
                    calEvent.EventType.CalendarEventTypeId = ev.EventType.CalendarEventTypeId;
                    calEvent.EventType.PointsForExcused = ev.EventType.PointsForExcused;
                    calEvent.EventType.PointsForNotPresent = ev.EventType.PointsForNotPresent;
                    calEvent.EventType.PointsForPartial = ev.EventType.PointsForPartial;
                    calEvent.EventType.PointsForPresent = ev.EventType.PointsForPresent;
                    calEvent.EventType.PointsForTardy = ev.EventType.PointsForTardy;
                }
                foreach (var owner in ev.Calendar.LeagueOwners)
                {
                    if (owner.League != null)
                    {
                        calEvent.OrganizersId = owner.League.LeagueId;
                        calEvent.OrganizersName = owner.League.Name;
                        if (owner.League.Logo != null)
                            calEvent.ImageUrl = owner.League.Logo.ImageUrlThumb;
                        calEvent.OrganizerUrl = ServerConfig.WEBSITE_DEFAULT_LOCATION + "/roller-derby-league/" + RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(calEvent.OrganizersName) + "/" + calEvent.OrganizersId.ToString().Replace("-", "");
                        break;
                    }
                }
                GoogleCalendar gc = new GoogleCalendar();
                if (ev.Location != null)
                {
                    calEvent.Location.LocationName = ev.Location.LocationName;
                    gc.Location = calEvent.Location.LocationName + " ";
                }
                if (ev.Location != null)
                {
                    calEvent.Location.LocationName = ev.Location.LocationName;
                    calEvent.Location.LocationId = ev.Location.LocationId;

                    if (ev.Location.Contact != null && ev.Location.Contact.Addresses.FirstOrDefault() != null)
                    {
                        Address address = new Address();
                        address.Address1 = ev.Location.Contact.Addresses.FirstOrDefault().Address1;
                        address.Address2 = ev.Location.Contact.Addresses.FirstOrDefault().Address2;
                        address.AddressId = ev.Location.Contact.Addresses.FirstOrDefault().AddressId;
                        address.CityRaw = ev.Location.Contact.Addresses.FirstOrDefault().CityRaw;
                        if (ev.Location.Contact.Addresses.FirstOrDefault().Country != null)
                            address.Country = ev.Location.Contact.Addresses.FirstOrDefault().Country.Code;
                        address.StateRaw = ev.Location.Contact.Addresses.FirstOrDefault().StateRaw;
                        address.Zip = ev.Location.Contact.Addresses.FirstOrDefault().Zip;
                        gc.Location += address.Address1 + " " + address.Address2 + ", " + address.CityRaw + ", " + address.StateRaw + " " + address.Zip;
                        address.Coords = new Portable.Classes.Location.GeoCoordinate();
                        address.Coords.Longitude = ev.Location.Contact.Addresses.FirstOrDefault().Coords.Longitude;
                        address.Coords.Latitude = ev.Location.Contact.Addresses.FirstOrDefault().Coords.Latitude;

                        if (!String.IsNullOrEmpty(address.CityRaw))
                            calEvent.Address += address.CityRaw + ", ";
                        if (!String.IsNullOrEmpty(address.StateRaw))
                            calEvent.Address += address.StateRaw + ",";
                        calEvent.Address += address.Country;
                        calEvent.AddressUrl += "http://www.bing.com/maps/default.aspx?q=";
                        calEvent.AddressUrl += address.Address1 + address.Address2;

                        calEvent.AddressUrl += " ," + address.CityRaw;
                        calEvent.AddressUrl += "," + address.StateRaw;
                        calEvent.AddressUrl += " " + address.Zip;
                        calEvent.AddressUrl += address.Country;

                        calEvent.Location.Contact.Addresses.Add(address);
                    }
                }
                if (ev.ReocurringEvent != null)
                    calEvent.CalendarReoccurringId = ev.ReocurringEvent.CalendarItemId;
                calEvent.Name = ev.Name;
                calEvent.NameUrl = ServerConfig.WEBSITE_DEFAULT_LOCATION + "/roller-derby-event/" + RDN.Utilities.Strings.StringExt.ToSearchEngineFriendly(ev.Name) + "/" + ev.CalendarItemId.ToString().Replace("-", "");
                calEvent.TicketUrl = ev.TicketUrl;
                calEvent.CalendarItemId = ev.CalendarItemId;

                var avail = calEvent.Attendees.Where(x => x.MemberId == currentMemberId && x.Availability != AvailibilityEnum.None).FirstOrDefault();
                if (avail != null)
                    calEvent.HasCurrentMemberSetAvailability = true;
                var attend = calEvent.Attendees.Where(x => x.MemberId == currentMemberId && x.IsCheckedIn == true).FirstOrDefault();
                if (attend != null)
                    calEvent.IsCurrentMemberCheckedIn = true;

                if (ev.Color != null)
                {
                    var c = Color.FromArgb(ev.Color.ColorIdCSharp);
                    calEvent.ColorTempSelected = ColorTranslator.ToHtml(c);
                }

                if (!ev.IsInUTCTime)
                {
                    calEvent.StartDate = ev.StartDate;
                    calEvent.EndDate = ev.EndDate;
                }
                else
                {
                    calEvent.StartDate = (ev.StartDate + new TimeSpan(ev.Calendar.TimeZone, 0, 0));
                    calEvent.EndDate = (ev.EndDate + new TimeSpan(ev.Calendar.TimeZone, 0, 0));
                }

                calEvent.StartDateDisplay = calEvent.StartDate.ToShortDateString();
                calEvent.EndDateDisplay = calEvent.EndDate.ToShortDateString();
                calEvent.Link = ev.Link;
                if (!String.IsNullOrEmpty(ev.Link) && !ev.Link.Contains("http://") && !ev.Link.Contains("https://"))
                    calEvent.Link = "http://" + ev.Link;
                else
                    calEvent.Link = ev.Link;
                calEvent.Notes = ev.Notes;

                if (ev.LastModified > new DateTime(2013, 11, 23) || ev.Created > new DateTime(2013, 11, 23))
                {
                    calEvent.NotesHtml = ev.Notes;
                }
                else if (ev.Created < new DateTime(2013, 11, 23))
                {
                    if (!String.IsNullOrEmpty(ev.Notes))
                    {
                        RDN.Library.Util.MarkdownSharp.Markdown markdown = new RDN.Library.Util.MarkdownSharp.Markdown();
                        markdown.AutoHyperlink = true;
                        markdown.LinkEmails = true;
                        calEvent.NotesHtml = HtmlSanitize.FilterHtmlToWhitelist(markdown.Transform(ev.Notes)).Replace("</p>", "</p><br/>");
                    }
                }

                //gc.Description = calEvent.Notes;
                gc.EndDate = calEvent.EndDate.ToUniversalTime();
                gc.StartDate = calEvent.StartDate.ToUniversalTime();
                gc.Website = calEvent.Link;
                gc.WebsiteName = calEvent.Link;
                gc.What = calEvent.Name;

                calEvent.AllowSelfCheckIn = ev.AllowSelfCheckIn;
                var isApartOfEvent = calEvent.MembersApartOfEvent.Where(x => x.MemberId == currentMemberId).FirstOrDefault();
                if (calEvent.AllowSelfCheckIn && calEvent.IsCurrentMemberApartOfEvent || (calEvent.AllowSelfCheckIn && isApartOfEvent != null))
                    calEvent.CanCurrentUserCheckIn = true;
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return calEvent;
        }