                 i = i + 1
             weekInfo
 
+        getWeekNumber: (currentMonth, currentDate, currentYear) =>
+            self = @
+            weekNum = 1
+            weekNums = []
+            weekInfo = self.getDatesInWeek currentMonth, currentDate, currentYear
+
+            $.each self.months, (key, value) ->
+                numberDays = self.getDaysInMonth key, currentYear
+                firstDay = self.getDayOfWeek key, 1, currentYear
+                dayShift = if firstDay is self.daysPerWeek then 0 else firstDay
+                numberWeeks = self.getWeeksInMonth currentMonth, currentYear
+                week = 1
+                if $.isNumeric numberWeeks
+                    until week > numberWeeks
+                        if week is 1 and firstDay isnt 0
+                            weekNum = weekNum
+                        else
+                            weekNum = weekNum + 1
+                        week = week + 1
+                        if currentMonth is key
+                            weekNums.push weekNum
+            weekNumber = weekNums[weekInfo.weekNum]
+
         createEvent: (newEvent) =>
             id = @events.length
             obj =

            
            @changeCalendar currentMonth, currentDate, currentYear

        changeCalendar: (month, date, year) =>
            self = @
            calendarInner = self.calendar.find "div.#{@calendarInnerClass}"
            calendarInner.fadeOut(300).queue (next) ->
                $.when(calendarInner.remove()).then(self.createCalendar(month, date, year))
                next()

        createMonthView: (currentMonth, currentYear) =>
            self = @
            self.calendarInnerClass = "drm-calendar-#{self.view}-view"
            firstDay = self.getDayOfWeek currentMonth, 1, currentYear
            dayShift = if firstDay is self.daysPerWeek then 0 else firstDay
            numberDays = self.getDaysInMonth currentMonth, currentYear
            numberWeeks = self.getWeeksInMonth currentMonth, currentYear
            nextYear = currentYear + 1
            lastYear = currentYear - 1
            nextMonth = if currentMonth is 11 then 0 else currentMonth + 1
            lastMonth = if currentMonth is 0 then 11 else currentMonth - 1
            prevMonthNumberDays = self.getDaysInMonth lastMonth, currentYear
            calendar = null
            heading = null
            weekdays = null
            weeks = null

            weekdays = '<table><thead><tr>'
            $.each self.days, (key, value) ->
                weekdays += "<th>#{value}</th>"
            weekdays += '</tr></thead>'

            weeks = "<tbody class='#{self.classes.month}'>"
            i = 1
            currentDate = 1
            l = 1
            prevDays = (prevMonthNumberDays - dayShift) + 1
            nextDates = 1

            while i <= numberWeeks
                j = 1
                weeks += "<tr class='#{self.classes.week}'>"
                # if we are in week 1 we need to shift to the correct day of the week
                if i is 1 and firstDay isnt 0
                    # add cells for the previous month until we get to the first day
                    while l <= dayShift
                        weeks += "<td class='#{self.classes.muted}' data-day='#{j}'>#{prevDays}</td>"
                        prevDays += 1
                        l += 1
                        j += 1
                    # start adding cells for the current month
                    while j <= self.daysPerWeek
                        weeks += "<td class='#{self.classes.date}' data-month='#{currentMonth}' data-date='#{currentDate}' data-year='#{currentYear}' data-day='#{j - 1}'>#{currentDate}</td>"
                        j += 1
                        currentDate += 1
                # if we are in the last week of the month we need to add blank cells for next month
                else if i is numberWeeks
                    while j <= self.daysPerWeek
                        # finish adding cells for the current month
                        if currentDate <= numberDays
                            weeks += "<td class='#{self.classes.date}' data-month='#{currentMonth}' data-date='#{currentDate}' data-year='#{currentYear}' data-day='#{j - 1}'>#{currentDate}</td>"
                        # start adding cells for next month
                        else
                            weeks += "<td class='#{self.classes.muted}' data-day='#{j}'>#{nextDates}</td>"
                            nextDates += 1
                        j += 1
                        currentDate += 1
                else
                    # if we are in the middle of the month add cells for the current month
                    while j <= self.daysPerWeek
                        weeks += "<td class='#{self.classes.date}' data-month='#{currentMonth}' data-date='#{currentDate}' data-year='#{currentYear}' data-day='#{j - 1}'>#{currentDate}</td>"
                        j += 1
                        currentDate += 1
                weeks += '</tr>'
                i += 1
            weeks += '</tbody></table>'

            calendar = $ '<div></div>',
                class: self.calendarInnerClass
                html: weekdays + weeks
                'data-month': currentMonth
                'data-year': currentYear

            heading = $ '<h1></h1>',
                class: 'drm-calendar-header'
                text: "#{@months[currentMonth]} #{currentYear}"
            
            calendar.appendTo ".#{self.calendarClass}"
            heading.prependTo ".#{self.calendarInnerClass}"

            self.highlightCurrentDay()
            self.highlightWeekends()
            self.addWeekNumbers()

            $.each self.events, (key, value) ->
                self.addEventsToCalendar value

            $('.drm-calendar-year-prev').text lastYear
            $('.drm-calendar-year-next').text nextYear

            $('.drm-calendar-month-prev').text self.months[lastMonth]
            $('.drm-calendar-month-next').text self.months[nextMonth]

            $('.drm-calendar-week-prev, .drm-calendar-week-next').hide()
            $('.drm-calendar-date-prev, .drm-calendar-date-next').hide()

        createWeekView: (currentMonth, currentDate, currentYear) =>
            self = @
            self.calendarInnerClass = "drm-calendar-#{self.view}-view"
            firstDay = self.getDayOfWeek currentMonth, 1, currentYear
            dayShift = if firstDay is self.daysPerWeek then 0 else firstDay
            numberWeeks = self.getWeeksInMonth currentMonth, currentYear
            nextYear = currentYear + 1
            lastYear = currentYear - 1
            nextMonth = if currentMonth is 11 then 0 else currentMonth + 1
            lastMonth = if currentMonth is 0 then 11 else currentMonth - 1
            prevMonthNumberDays = self.getDaysInMonth lastMonth, currentYear
            calendar = null
            heading = null
            weekdays = null
            weekInfo = self.getDatesInWeek currentMonth, currentDate, currentYear
            datesInWeek = weekInfo.datesInWeek
            weekNumber = self.getWeekNumber currentMonth, currentDate, currentYear
            weekClass = if weekNumber % 2 is 0 then 'even-week' else 'odd-week'

            getDates = (datesInWeek, key) ->
                dates = {}
                # if its the first week of the month
                if datesInWeek.length < self.daysPerWeek and datesInWeek[0] is 1
                    if key is firstDay
                        dates.date = 1
                        dates.month = currentMonth
                    else if key > firstDay
                        dates.date = (key - firstDay) + 1
                        dates.month = currentMonth
                    else
                        dates.date = prevMonthNumberDays - (firstDay - (key + 1))
                        dates.month = lastMonth
                else if datesInWeek.length is self.daysPerWeek
                    dates.date = datesInWeek[key]
                    dates.month = currentMonth
                # if its the last week of the month
                else if datesInWeek.length < self.daysPerWeek and datesInWeek[0] isnt 1
                    if key < datesInWeek.length
                        dates.date = datesInWeek[key]
                        dates.month = currentMonth
                    else
                        dates.date = Math.abs (datesInWeek.length - (key + 1))
                        dates.month = nextMonth
                dates

            weekdaysHtml = "<thead><tr><th></th>"
            $.each self.days, (key, value) ->
                dates = getDates datesInWeek, key
                weekdaysHtml += "<th>#{value}<br>#{self.months[dates.month]} #{dates.date}</th>"
            weekdaysHtml += '</tr></thead>'

            weekHtml = "<tbody class='#{self.classes.week} #{weekClass}' data-week='#{weekNumber}'>"
            $.each self.hours, (key, value) ->
                hour = value.name
                weekHtml += "<tr><td><span class='hour'>#{hour}</span></td>"
                $.each self.days, (key, value) ->
                    dates = getDates datesInWeek, key
                    weekHtml += "<td class='#{self.classes.date}' data-month='#{dates.month}' data-date='#{dates.date}' data-year='#{currentYear}' data-day='#{key}' data-hour='#{hour}'></td>"
                weekHtml += '</tr>'
            weekHtml += '</tbody>'

            weekView = $ '<table></table>',
                html: weekdaysHtml + weekHtml

            calendar = $ '<div></div>',
                class: self.calendarInnerClass
                html: weekView
                'data-month': currentMonth
                'data-year': currentYear

            weekDates = if datesInWeek.length > 1 then "#{datesInWeek[0]} - #{datesInWeek[datesInWeek.length - 1]}" else "#{datesInWeek[0]}"

            heading = $ '<h1></h1>',
                class: 'drm-calendar-header'
                text: "#{@months[currentMonth]} #{weekDates}: Week #{weekNumber} of #{currentYear}"
            
            calendar.appendTo ".#{self.calendarClass}"
            heading.prependTo "div.#{self.calendarInnerClass}"

            $("div.#{self.calendarInnerClass}").find("tbody td[data-month=#{lastMonth}]").addClass(self.classes.muted).removeClass 'drm-date'
            $("div.#{self.calendarInnerClass}").find("tbody td[data-month=#{nextMonth}]").addClass(self.classes.muted).removeClass 'drm-date'
            self.highlightCurrentDay()
            self.highlightWeekends()

            $.each self.events, (key, value) ->
                self.addEventsToCalendar value

            $('.drm-calendar-year-prev').text lastYear
            $('.drm-calendar-year-next').text nextYear

            $('.drm-calendar-month-prev').text self.months[lastMonth]
            $('.drm-calendar-month-next').text self.months[nextMonth]

            $('.drm-calendar-week-prev, .drm-calendar-week-next').show()
            $('.drm-calendar-date-prev, .drm-calendar-date-next').hide()

        createDateView: (currentMonth, currentDate, currentYear) =>
            self = @
            self.calendarInnerClass = "drm-calendar-#{self.view}-view"
            day = self.getDayOfWeek currentMonth, currentDate, currentYear
            weekNumber = self.getWeekNumber currentMonth, currentDate, currentYear
            weekClass = if weekNumber % 2 is 0 then 'even-week' else 'odd-week'
            nextYear = currentYear + 1
            lastYear = currentYear - 1
            nextMonth = if currentMonth is 11 then 0 else currentMonth + 1
            lastMonth = if currentMonth is 0 then 11 else currentMonth - 1

            dayListHtml = "<ul class='drm-week drm-day #{weekClass}' data-week='#{weekNumber}'>"
            $.each self.hours, (key, value) ->
                hour = value.name
                dayListHtml += "<li class='#{self.classes.date}' data-month='#{currentMonth}' data-date='#{currentDate}' data-year='#{currentYear}' data-day='#{day}' data-hour='#{hour}'><span class='hour'>#{hour}</span></li>"
            dayListHtml += '</ul>'

            calendar = $ '<div></div>',
                class: self.calendarInnerClass
                'data-month': currentMonth
                'data-year': currentYear
                html: dayListHtml

            headingText = 
                if currentMonth is self.currentMonth and currentDate is self.currentDate and currentYear is self.currentYear
                    "Today, #{self.days[day]}, #{self.months[currentMonth]} #{currentDate} #{currentYear}"
                else
                    "#{self.days[day]}, #{self.months[currentMonth]} #{currentDate} #{currentYear}"

            heading = $ '<h1></h1>',
                class: 'drm-calendar-header'
                text: headingText
            
            calendar.appendTo ".#{self.calendarClass}"
            heading.prependTo ".#{self.calendarInnerClass}"

            $.each self.events, (key, value) ->
                self.addEventsToCalendar value

            $('.drm-calendar-year-prev').text lastYear
            $('.drm-calendar-year-next').text nextYear

            $('.drm-calendar-month-prev').text self.months[lastMonth]
            $('.drm-calendar-month-next').text self.months[nextMonth]

            $('.drm-calendar-week-prev, .drm-calendar-week-next').show()
            $('.drm-calendar-date-prev, .drm-calendar-date-next').show()

        createCalendar: (month, date, year) =>
            self = @

            switch self.view
                when 'month' then self.createMonthView month, year
                when 'week' then self.createWeekView month, date, year
                when 'date' then self.createDateView month, date, year

    drmCalendar = new DrmCalendar()
    drmCalendar.createEvent

                notes: if newEvent.notes? then newEvent.notes else null
            @events.push obj
            @addEventsToCalendar @events[obj.id]

        removeCalendarEvent: (eventId, index) =>
            events = @calendar.find "ul.#{@eventClass} a[data-event=#{eventId}]"
