+#include "calendar.h"
+
+/* These define how high the hour rows are in the day view */
+#define TIMELINE 30
+#define EXTRATIMELINE (TIMELINE / 2)
+
+void embeddable_mini_calendar(int year, int month)
+{
+ struct tm starting_tm;
+ struct tm tm;
+ time_t thetime;
+ int i;
+ time_t previous_month;
+ time_t next_month;
+ time_t colheader_time;
+ struct tm colheader_tm;
+ char colheader_label[32];
+ long weekstart = 0;
+ char url[256];
+ char div_id[256];
+
+ snprintf(div_id, sizeof div_id, "mini_calendar_%d", rand() );
+
+ /* Determine what day to start. If an impossible value is found, start on Sunday.
+ */
+ get_pref_long("weekstart", &weekstart, 17);
+ if (weekstart > 6) weekstart = 0;
+
+ /*
+ * Now back up to the 1st of the month...
+ */
+ memset(&starting_tm, 0, sizeof(struct tm));
+
+ starting_tm.tm_year = year - 1900;
+ starting_tm.tm_mon = month - 1;
+ starting_tm.tm_mday = 1;
+ thetime = mktime(&starting_tm);
+
+ memcpy(&tm, &starting_tm, sizeof(struct tm));
+ while (tm.tm_mday != 1) {
+ thetime = thetime - (time_t)86400; /* go back 24 hours */
+ localtime_r(&thetime, &tm);
+ }
+
+ /* Determine previous and next months ... for links */
+ previous_month = thetime - (time_t)864000L; /* back 10 days */
+ next_month = thetime + (time_t)(31L * 86400L); /* ahead 31 days */
+
+ /* Now back up until we're on the user's preferred start day */
+ localtime_r(&thetime, &tm);
+ while (tm.tm_wday != weekstart) {
+ thetime = thetime - (time_t)86400; /* go back 24 hours */
+ localtime_r(&thetime, &tm);
+ }
+
+ wc_printf("<div class=\"mini_calendar\" id=\"%s\">\n", div_id);
+
+ /* Previous month link */
+ localtime_r(&previous_month, &tm);
+ wc_printf("<a href=\"javascript:minical_change_month(%d,%d);\">«</a>",
+ (int)(tm.tm_year)+1900, tm.tm_mon + 1);
+
+ wc_strftime(colheader_label, sizeof colheader_label, "%B", &starting_tm);
+ wc_printf(" "
+ "<span class=\"mini_calendar_month_label\">"
+ "%s %d"
+ "</span>"
+ " ", colheader_label, year);
+
+ /* Next month link */
+ localtime_r(&next_month, &tm);
+ wc_printf("<a href=\"javascript:minical_change_month(%d,%d);\">»</a>",
+ (int)(tm.tm_year)+1900, tm.tm_mon + 1);
+
+ wc_printf("<table border=\"0\" cellpadding=\"1\" cellspacing=\"1\" class=\"mini_calendar_days\">"
+ "<tr>");
+ colheader_time = thetime;
+ for (i=0; i<7; ++i) {
+ colheader_time = thetime + (i * 86400) ;
+ localtime_r(&colheader_time, &colheader_tm);
+ wc_strftime(colheader_label, sizeof colheader_label, "%A", &colheader_tm);
+ wc_printf("<th>%c</th>", colheader_label[0]);
+
+ }
+ wc_printf("</tr>\n");