KMC.CALENDAR = (function () {
    var days_in_month = function (m, y) {
        return 32 - new Date(y, m, 32).getDate();
    };
    return {
        calendar_size: 'full',

        date: new Date(),
        // TODO: Change back to today's date
        draw: function () {
            
            var num_days = days_in_month(this.date.getMonth(), this.date.getFullYear());
            var date = new Date();

            date.setFullYear(this.date.getFullYear(), this.date.getMonth(), 1);
            var firstday = date.getDay();
            var $tr, $td, $table = $("<table></table>").addClass("calendar").addClass(this.calendar_size).append("<thead><tr><th>" + KMC.UTIL.weekdays.join("</th><th>") + "</th></tr></thead>");

            var $tbody = $("<tbody></tbody>").appendTo($table);

            var day_text = 1;
            var dow;
            for (var week = 0; week < 6; week += 1) {
                $tr = $("<tr></tr>");
                for (dow = 0; dow < 7; dow += 1) {
                    $td = $("<td></td>");
                    if ((week === 0 && dow < firstday) || day_text > num_days) {
                        $td.addClass("out-of-month");
                    }
                    else {
                        
                        $td.html("<p class = 'day_view'>" + day_text + '</p>').addClass("in-month").attr('id', 'day_' + day_text);
                        //highlight the day if it is selected
                        if (day_text === this.date.getDate() && this.calendar_size === 'mini') {
                            $td.css("background-color", "#ddf");
                        }
						day_text += 1;
                    }
					
                    $tr.append($td);
                }
                $tbody.append($tr);
            }

            $("#calendar").empty().append($table);
            $("#month_name").empty().text(this.date.printMonth()+" "+this.date.getFullYear());
            var shift_dow, month_first_day;
            var sorted_shifts = KMC.UTIL.sorted_keys(KMC.shifts, 'start_time');
            if (this.calendar_size !== 'mini') {
                var shift, start_day, day;
                for (x in sorted_shifts) {
                    shift = KMC.shifts[sorted_shifts[x]];
                    
                    // for prior months...
                    // find out which DOW the shift is on, and then first occurrence of that DOW
                    // this month. That's the first day we show the shift on.
                    if (shift.start_date.getMonth() == KMC.CALENDAR.date.getMonth() && shift.start_date.getFullYear() == KMC.CALENDAR.date.getFullYear()) {						
					
						var start_day = shift.start_date.getDate();

                    } else { // if the shift starts this month, we're done.
                        shift_dow = shift.start_date.getDay();
                        month_first_day = new Date(KMC.CALENDAR.date.getFullYear(), KMC.CALENDAR.date.getMonth(),1);
                        
                        //Calculate the first occurrence of the target dow
                        start_day = KMC.UTIL.mod((7 - month_first_day.getDay() + shift_dow), 7) + 1;   	
                    }
                     //for the end_day
                    if (shift.end_date === "None" || (shift.end_date.getMonth() > KMC.CALENDAR.date.getMonth())) {
				         end_day = 32; 

                    } else { // if the shift ends this month, we're done.
                        
                        var end_day = shift.end_date.getDate();
						
                    }
                    for (day = start_day; day < 32; day = day + 7) {
					    
                        if (end_day >= day) {

                            $("#day_" + day).append(
                                "<p class = 'shift_view' id = 'shift_" + shift.id + "'>"
                                + shift.start_time.printTimeShort() + "-" + shift.end_time.printTimeShort()
                                + " " + shift.doctor.last_name + "</p>");
                        }
                    }
                }
            }
            else {
                $table.find("th").each(function () {
                    var newtext = $(this).text().substr(0, 3);
                    $(this).text(newtext);

                });

            }
        }
    };
})();

