function dbem_get_calendar($args = "")
{
    $defaults = array('full' => 0, 'month' => '', 'echo' => 1, 'long_events' => 0);
    $r = wp_parse_args($args, $defaults);
    extract($r, EXTR_SKIP);
    $full = $r['full'];
    $month = $r['month'];
    $echo = $r['echo'];
    $week_starts_on_sunday = get_option('dbem_week_starts_sunday');
    $start_of_week = get_option('start_of_week');
    global $wpdb;
    if (isset($_GET['calmonth']) && $_GET['calmonth'] != '') {
        $month = $_GET['calmonth'];
    } else {
        if ($month == '') {
            $month = date('m');
        }
    }
    if (isset($_GET['calyear']) && $_GET['calyear'] != '') {
        $year = $_GET['calyear'];
    } else {
        if ($year == '') {
            $year = date('Y');
        }
    }
    $date = mktime(0, 0, 0, $month, date('d'), $year);
    $day = date('d', $date);
    // $month = date('m', $date);
    // $year = date('Y', $date);
    // Get the first day of the month
    $month_start = mktime(0, 0, 0, $month, 1, $year);
    // Get friendly month name
    $month_name = mysql2date('M', "{$year}-{$month}-{$day} 00:00:00");
    // Figure out which day of the week
    // the month starts on.
    $month_start_day = date('D', $month_start);
    switch ($month_start_day) {
        case "Sun":
            $offset = 0;
            break;
        case "Mon":
            $offset = 1;
            break;
        case "Tue":
            $offset = 2;
            break;
        case "Wed":
            $offset = 3;
            break;
        case "Thu":
            $offset = 4;
            break;
        case "Fri":
            $offset = 5;
            break;
        case "Sat":
            $offset = 6;
            break;
    }
    $offset -= $start_of_week;
    if ($offset < 0) {
        $offset += 7;
    }
    // determine how many days are in the last month.
    if ($month == 1) {
        $num_days_last = dbem_days_in_month(12, $year - 1);
    } else {
        $num_days_last = dbem_days_in_month($month - 1, $year);
    }
    // determine how many days are in the current month.
    $num_days_current = dbem_days_in_month($month, $year);
    // Build an array for the current days
    // in the month
    for ($i = 1; $i <= $num_days_current; $i++) {
        $num_days_array[] = mktime(0, 0, 0, date('m'), $i, date('Y'));
    }
    // Build an array for the number of days
    // in last month
    for ($i = 1; $i <= $num_days_last; $i++) {
        $num_days_last_array[] = $i;
    }
    // If the $offset from the starting day of the
    // week happens to be Sunday, $offset would be 0,
    // so don't need an offset correction.
    if ($offset > 0) {
        $offset_correction = array_slice($num_days_last_array, -$offset, $offset);
        $new_count = array_merge($offset_correction, $num_days_array);
        $offset_count = count($offset_correction);
    } else {
        $offset_count = 0;
        $new_count = $num_days_array;
    }
    // count how many days we have with the two
    // previous arrays merged together
    $current_num = count($new_count);
    // Since we will have 5 HTML table rows (TR)
    // with 7 table data entries (TD)
    // we need to fill in 35 TDs
    // so, we will have to figure out
    // how many days to appened to the end
    // of the final array to make it 35 days.
    if ($current_num > 35) {
        $num_weeks = 6;
        $outset = 42 - $current_num;
    } elseif ($current_num < 35) {
        $num_weeks = 5;
        $outset = 35 - $current_num;
    }
    if ($current_num == 35) {
        $num_weeks = 5;
        $outset = 0;
    }
    // Outset Correction
    for ($i = 1; $i <= $outset; $i++) {
        $new_count[] = $i;
    }
    // Now let's "chunk" the $all_days array
    // into weeks. Each week has 7 days
    // so we will array_chunk it into 7 days.
    $weeks = array_chunk($new_count, 7);
    // Build Previous and Next Links
    $base_link = "?" . $_SERVER['QUERY_STRING'] . "&amp;";
    if ($month == 1) {
        $back_month = 12;
        $back_year = $year - 1;
    } else {
        $back_month = $month - 1;
        $back_year = $year;
    }
    $full ? $link_extra_class = "full-link" : ($link_extra_class = '');
    $previous_link = "<a class='prev-month {$link_extra_class}' href=\"" . $base_link . "calmonth={$back_month}&amp;calyear={$back_year} \">&lt;&lt;</a>";
    if ($month == 12) {
        $next_month = 1;
        $next_year = $year + 1;
    } else {
        $next_month = $month + 1;
        $next_year = $year;
    }
    $next_link = "<a class='next-month {$link_extra_class}' href=\"" . $base_link . "calmonth={$next_month}&amp;calyear={$next_year} \">&gt;&gt;</a>";
    $random = rand(100, 200);
    $full ? $class = 'dbem-calendar-full' : ($class = 'dbem-calendar');
    $calendar = "<div class='{$class}' id='dbem-calendar-{$random}'><div style='display:none' class='month_n'>{$month}</div><div class='year_n' style='display:none' >{$year}</div>";
    $weekdays = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
    $n = 0;
    while ($n < $start_of_week) {
        $last_day = array_shift($weekdays);
        $weekdays[] = $last_day;
        $n++;
    }
    $days_initials = "";
    foreach ($weekdays as $weekday) {
        $days_initials .= "<td>" . dbem_translate_and_trim($weekday) . "</td>";
    }
    $full ? $fullclass = 'fullcalendar' : ($fullclass = '');
    // Build the heading portion of the calendar table
    $calendar .= "<table class='dbem-calendar-table {$fullclass}'>\n" . "<thead>\n<tr>\n" . "<td>{$previous_link}</td><td class='month_name' colspan='5'>{$month_name} {$year}</td><td>{$next_link}</td>\n" . "</tr>\n</thead>\n" . "<tr class='days-names'>\n" . $days_initials . "</tr>\n";
    // Now we break each key of the array
    // into a week and create a new table row for each
    // week with the days of that week in the table data
    $i = 0;
    foreach ($weeks as $week) {
        $calendar .= "<tr>\n";
        foreach ($week as $d) {
            if ($i < $offset_count) {
                //if it is PREVIOUS month
                $calendar .= "<td class='eventless-pre'>{$d}</td>\n";
            }
            if ($i >= $offset_count && $i < $num_weeks * 7 - $outset) {
                // if it is THIS month
                $fullday = $d;
                $d = date('j', $d);
                $day_link = "{$d}";
                // original :
                //if($date == mktime(0,0,0,$month,$d,$year)){
                // proposed patch (http://davidebenini.it/events-manager-forum/topic.php?id=73 )
                // if(($date == mktime(0,0,0,$month,$d,$year)) && (date('F') == $month_name)) {
                // my solution:
                if ($d == date('j') && $month == date('m') && $year == date('Y')) {
                    $calendar .= "<td class='eventless-today'>{$d}</td>\n";
                } else {
                    $calendar .= "<td class='eventless'>{$day_link}</td>\n";
                }
            } elseif ($outset > 0) {
                //if it is NEXT month
                if ($i >= $num_weeks * 7 - $outset) {
                    $calendar .= "<td class='eventless-post'>{$d}</td>\n";
                }
            }
            $i++;
        }
        $calendar .= "</tr>\n";
    }
    $calendar .= " </table>\n</div>";
    // query the database for events in this time span
    if ($month == 1) {
        $month_pre = 12;
        $month_post = 2;
        $year_pre = $year - 1;
        $year_post = $year;
    } elseif ($month == 12) {
        $month_pre = 11;
        $month_post = 1;
        $year_pre = $year;
        $year_post = $year + 1;
    } else {
        $month_pre = $month - 1;
        $month_post = $month + 1;
        $year_pre = $year;
        $year_post = $year;
    }
    $limit_pre = date("Y-m-d", mktime(0, 0, 0, $month_pre, 1, $year_pre));
    $limit_post = date("Y-m-d", mktime(0, 0, 0, $month_post, 30, $year_post));
    $events_table = $wpdb->prefix . EVENTS_TBNAME;
    $sql = "SELECT event_id, \n\t\tevent_name, \n\t \tevent_start_date,\n\t\tevent_start_time, \n\t\tevent_end_date,\n\t\tDATE_FORMAT(event_start_date, '%w') AS 'event_weekday_n',\n\t\tDATE_FORMAT(event_start_date, '%e') AS 'event_day',\n\t\tDATE_FORMAT(event_start_date, '%c') AS 'event_month_n',\n\t\tDATE_FORMAT(event_start_time, '%Y') AS 'event_year',\n\t\tDATE_FORMAT(event_start_time, '%k') AS 'event_hh',\n\t\tDATE_FORMAT(event_start_time, '%i') AS 'event_mm'\n\t\tFROM {$events_table} \n\t\tWHERE (event_start_date BETWEEN '{$limit_pre}' AND '{$limit_post}') OR (event_end_date BETWEEN '{$limit_pre}' AND '{$limit_post}') ORDER BY event_start_date";
    $events = $wpdb->get_results($sql, ARRAY_A);
    //----- DEBUG ------------
    //foreach($events as $event) { //DEBUG
    //	$calendar .= ("$event->event_day / $event->event_month_n - $event->event_name<br/>");
    //}
    // ------------------
    $eventful_days = array();
    if ($events) {
        //Go through the events and slot them into the right d-m index
        foreach ($events as $event) {
            if ($long_events) {
                //If $long_events is set then show a date as eventful if there is an multi-day event which runs during that day
                $event_start_date = strtotime($event['event_start_date']);
                $event_end_date = strtotime($event['event_end_date']);
                while ($event_start_date <= $event_end_date) {
                    $event_eventful_date = date('Y-m-d', $event_start_date);
                    //Only show events on the day that they start
                    if (is_array($eventful_days[$event_eventful_date])) {
                        $eventful_days[$event_eventful_date][] = $event;
                    } else {
                        $eventful_days[$event_eventful_date] = array($event);
                    }
                    $event_start_date += 60 * 60 * 24;
                }
            } else {
                //Only show events on the day that they start
                if (is_array($eventful_days[$event['event_start_date']])) {
                    $eventful_days[$event['event_start_date']][] = $event;
                } else {
                    $eventful_days[$event['event_start_date']] = array($event);
                }
            }
        }
    }
    $events_page = get_option('dbem_events_page');
    $event_format = get_option('dbem_full_calendar_event_format');
    $event_title_format = get_option('dbem_small_calendar_event_title_format');
    $event_title_separator_format = get_option('dbem_small_calendar_event_title_separator');
    $cells = array();
    foreach ($eventful_days as $day_key => $events) {
        //Set the date into the key
        $event_date = explode('-', $day_key);
        $cells[$day_key]['day'] = $event_date[2];
        $cells[$day_key]['month'] = $event_date[1];
        $events_titles = array();
        foreach ($events as $event) {
            $events_titles[] = dbem_replace_placeholders($event_title_format, $event);
        }
        $link_title = implode($event_title_separator_format, $events_titles);
        $cells[$day_key]['cell'] = "<a title='{$link_title}' href='?page_id={$events_page}&amp;calendar_day={$day_key}'>{$cells[$day_key]['day']}</a>";
        if ($full) {
            $cells[$day_key]['cell'] .= "<ul>";
            foreach ($events as $event) {
                $cells[$day_key]['cell'] .= dbem_replace_placeholders($event_format, $event);
            }
            $cells[$day_key]['cell'] .= "</ul>";
        }
    }
    //	print_r($cells);
    if ($events) {
        foreach ($cells as $cell) {
            if ($cell['month'] == $month_pre) {
                $calendar = str_replace("<td class='eventless-pre'>" . $cell['day'] . "</td>", "<td class='eventful-pre'>" . $cell['cell'] . "</td>", $calendar);
            } elseif ($cell['month'] == $month_post) {
                $calendar = str_replace("<td class='eventless-post'>" . $cell['day'] . "</td>", "<td class='eventful-post'>" . $cell['cell'] . "</td>", $calendar);
            } elseif ($cell['day'] == $day) {
                $calendar = str_replace("<td class='eventless-today'>" . $cell['day'] . "</td>", "<td class='eventful-today'>" . $cell['cell'] . "</td>", $calendar);
            } elseif ($cell['month'] == $month) {
                $calendar = str_replace("<td class='eventless'>" . $cell['day'] . "</td>", "<td class='eventful'>" . $cell['cell'] . "</td>", $calendar);
            }
        }
    }
    $output = $calendar;
    if ($echo) {
        echo $output;
    } else {
        return $output;
    }
}
function dbem_get_calendar($args = "")
{
    $defaults = array('month' => '', 'echo' => 1);
    $r = wp_parse_args($args, $defaults);
    extract($r, EXTR_SKIP);
    $month = $r['month'];
    $echo = $r['echo'];
    global $wpdb;
    if (isset($_GET['calmonth']) && $_GET['calmonth'] != '') {
        $month = $_GET['calmonth'];
    } else {
        if ($month == '') {
            $month = date('m');
        }
    }
    if (isset($_GET['calyear']) && $_GET['calyear'] != '') {
        $year = $_GET['calyear'];
    } else {
        if ($year == '') {
            $year = date('Y');
        }
    }
    $date = mktime(0, 0, 0, $month, date('d'), $year);
    $day = date('d', $date);
    // $month = date('m', $date);
    // $year = date('Y', $date);
    // Get the first day of the month
    $month_start = mktime(0, 0, 0, $month, 1, $year);
    // Get friendly month name
    $month_name = mysql2date('M', "{$year}-{$month}-{$day} 00:00:00");
    // Figure out which day of the week
    // the month starts on.
    $month_start_day = date('D', $month_start);
    switch ($month_start_day) {
        case "Sun":
            $offset = 6;
            break;
        case "Mon":
            $offset = 0;
            break;
        case "Tue":
            $offset = 1;
            break;
        case "Wed":
            $offset = 2;
            break;
        case "Thu":
            $offset = 3;
            break;
        case "Fri":
            $offset = 4;
            break;
        case "Sat":
            $offset = 5;
            break;
    }
    // determine how many days are in the last month.
    if ($month == 1) {
        $num_days_last = dbem_days_in_month(12, $year - 1);
    } else {
        $num_days_last = dbem_days_in_month($month - 1, $year);
    }
    // determine how many days are in the current month.
    $num_days_current = dbem_days_in_month($month, $year);
    // Build an array for the current days
    // in the month
    for ($i = 1; $i <= $num_days_current; $i++) {
        $num_days_array[] = mktime(0, 0, 0, date('m'), $i, date('Y'));
    }
    // Build an array for the number of days
    // in last month
    for ($i = 1; $i <= $num_days_last; $i++) {
        $num_days_last_array[] = $i;
    }
    // If the $offset from the starting day of the
    // week happens to be Sunday, $offset would be 0,
    // so don't need an offset correction.
    if ($offset > 0) {
        $offset_correction = array_slice($num_days_last_array, -$offset, $offset);
        $new_count = array_merge($offset_correction, $num_days_array);
        $offset_count = count($offset_correction);
    } else {
        $offset_count = 0;
        $new_count = $num_days_array;
    }
    // count how many days we have with the two
    // previous arrays merged together
    $current_num = count($new_count);
    // Since we will have 5 HTML table rows (TR)
    // with 7 table data entries (TD)
    // we need to fill in 35 TDs
    // so, we will have to figure out
    // how many days to appened to the end
    // of the final array to make it 35 days.
    if ($current_num > 35) {
        $num_weeks = 6;
        $outset = 42 - $current_num;
    } elseif ($current_num < 35) {
        $num_weeks = 5;
        $outset = 35 - $current_num;
    }
    if ($current_num == 35) {
        $num_weeks = 5;
        $outset = 0;
    }
    // Outset Correction
    for ($i = 1; $i <= $outset; $i++) {
        $new_count[] = $i;
    }
    // Now let's "chunk" the $all_days array
    // into weeks. Each week has 7 days
    // so we will array_chunk it into 7 days.
    $weeks = array_chunk($new_count, 7);
    // Build Previous and Next Links
    $base_link = "?" . $_SERVER['QUERY_STRING'] . "&amp;";
    if ($month == 1) {
        $back_month = 12;
        $back_year = $year - 1;
    } else {
        $back_month = $month - 1;
        $back_year = $year;
    }
    $previous_link = "<a class='prev-month' href=\"" . $base_link . "calmonth={$back_month}&amp;calyear={$back_year} \">&lt;&lt;</a>";
    if ($month == 12) {
        $next_month = 1;
        $next_year = $year + 1;
    } else {
        $next_month = $month + 1;
        $next_year = $year;
    }
    $next_link = "<a class='next-month' href=\"" . $base_link . "calmonth={$next_month}&amp;calyear={$next_year} \">&gt;&gt;</a>";
    $random = rand(100, 200);
    $calendar = "<div class='dbem-calendar' id='dbem-calendar-{$random}'><div style='display:none' class='month_n'>{$month}</div><div class='year_n' style='display:none' >{$year}</div>";
    $days_initials = "<td>" . dbem_translate_and_trim("Monday") . "</td><td>" . dbem_translate_and_trim("Tuesday") . "</td><td>" . dbem_translate_and_trim("Wednesday") . "</td><td>" . dbem_translate_and_trim("Thursday") . "</td><td>" . dbem_translate_and_trim("Friday") . "</td><td>" . dbem_translate_and_trim("Saturday") . "</td><td>" . dbem_translate_and_trim("Sunday") . "</td>\n";
    // Build the heading portion of the calendar table
    $calendar .= "<table class='dbem-calendar-table'>\n" . "<thead>\n<tr>\n" . "<td>{$previous_link}</td><td class='month_name' colspan='5'>{$month_name} {$year}</td><td>{$next_link}</td>\n" . "</tr>\n</thead>\n" . "<tr class='days-names'>\n" . $days_initials . "</tr>\n";
    // Now we break each key of the array
    // into a week and create a new table row for each
    // week with the days of that week in the table data
    $i = 0;
    foreach ($weeks as $week) {
        $calendar .= "<tr>\n";
        foreach ($week as $d) {
            if ($i < $offset_count) {
                //if it is PREVIOUS month
                $calendar .= "<td class='eventless-pre'>{$d}</td>\n";
            }
            if ($i >= $offset_count && $i < $num_weeks * 7 - $outset) {
                // if it is THIS month
                $fullday = $d;
                $d = date('j', $d);
                $day_link = "{$d}";
                // Apllying this patch http://davidebenini.it/events-manager-forum/topic.php?id=73; was
                //if($date == mktime(0,0,0,$month,$d,$year)){
                if ($date == mktime(0, 0, 0, $month, $d, $year) && date('F') == $month_name) {
                    $calendar .= "<td class='eventless-today'>{$d}</td>\n";
                } else {
                    $calendar .= "<td class='eventless'>{$day_link}</td>\n";
                }
            } elseif ($outset > 0) {
                //if it is NEXT month
                if ($i >= $num_weeks * 7 - $outset) {
                    $calendar .= "<td class='eventless-post'>{$d}</td>\n";
                }
            }
            $i++;
        }
        $calendar .= "</tr>\n";
    }
    $calendar .= " </table>\n</div>";
    // query the database for events in this time span
    if ($month == 1) {
        $month_pre = 12;
        $month_post = 2;
        $year_pre = $year - 1;
        $year_post = $year;
    } elseif ($month == 12) {
        $month_pre = 11;
        $month_post = 1;
        $year_pre = $year;
        $year_post = $year + 1;
    } else {
        $month_pre = $month - 1;
        $month_post = $month + 1;
        $year_pre = $year;
        $year_post = $year;
    }
    $limit_pre = date("Y-m-d", mktime(0, 0, 0, $month_pre, 1, $year_pre));
    $limit_post = date("Y-m-d", mktime(0, 0, 0, $month_post, 30, $year_post));
    $events_table = $wpdb->prefix . EVENTS_TBNAME;
    $sql = "SELECT event_id, \n\t\t\t\t\t\t\t\t\tevent_name, \n\t\t\t\t\t\t\t\t \tevent_start_date, \n\t\t\t\t\t\t\t\t\tDATE_FORMAT(event_start_date, '%w') AS 'event_weekday_n',\n\t\t\t\t\t\t\t\t\tDATE_FORMAT(event_start_date, '%e') AS 'event_day',\n\t\t\t\t\t\t\t\t\tDATE_FORMAT(event_start_date, '%c') AS 'event_month_n',\n\t\t\t\t\t\t\t\t\tDATE_FORMAT(event_start_time, '%Y') AS 'event_year',\n\t\t\t\t\t\t\t\t\tDATE_FORMAT(event_start_time, '%k') AS 'event_hh',\n\t\t\t\t\t\t\t\t\tDATE_FORMAT(event_start_time, '%i') AS 'event_mm'\n\t\tFROM {$events_table} WHERE event_start_date BETWEEN '{$limit_pre}' AND '{$limit_post}' ORDER BY event_start_date";
    $events = $wpdb->get_results($sql);
    //----- DEBUG ------------
    //foreach($events as $event) { //DEBUG
    //	$calendar .= ("$event->event_day / $event->event_month_n - $event->event_name<br/>");
    //}
    // ------------------
    // inserts the events
    // $eventful_months= array();
    // if($events){
    // 	foreach($events as $event) {
    // 		if($eventful_months[$event->event_month_n]){
    // 			$eventful_months[$event->event_month_n][] = $event;
    // 		} else {
    // 			$eventful_months[$event->event_month_n] = array($event);
    // 		}
    // 	}
    // 	$eventful_days = array();
    // 	foreach($eventful_months as $month) {
    // 		print_r($month);
    // 		$eventful_days[$month] = array();
    //
    // 		foreach($month as $event) {
    // 			// if($eventful_days[$month][$event->event_day]){
    // 			// 	$eventful_days[$month][$event->event_day][] = $event;
    // 			// } else {
    // 			// 	$eventful_days[$month][$event->event_day] = array($event);
    // 			// }
    //
    // 		}
    // 	}
    //
    //
    //
    // }
    // print_r("ECCO: <br/><pre>");
    // print_r($eventful_days);
    // print_r("</pre>");
    $events_page = get_option('dbem_events_page');
    if ($events) {
        foreach ($events as $event) {
            if ($event->event_month_n == $month_pre) {
                $calendar = str_replace("<td class='eventless-pre'>{$event->event_day}</td>", "<td class='eventful-pre'><a href='?page_id={$events_page}&amp;calendar_day=" . "{$event->event_start_date}'>{$event->event_day}</a></td>", $calendar);
            } elseif ($event->event_month_n == $month_post) {
                $calendar = str_replace("<td class='eventless-post'>{$event->event_day}</td>", "<td class='eventful-post'><a href='?page_id={$events_page}&amp;calendar_day=" . "{$event->event_start_date}'>{$event->event_day}</a></td>", $calendar);
            } elseif ($event->event_day == $day) {
                $calendar = str_replace("<td class='eventless-today'>{$event->event_day}</td>", "<td class='eventful-today'><a href='?page_id={$events_page}&amp;calendar_day=" . "{$event->event_start_date}'>{$event->event_day}</a></td>", $calendar);
            } else {
                $calendar = str_replace("<td class='eventless'>{$event->event_day}</td>", "<td class='eventful'><a href='?page_id={$events_page}&amp;calendar_day=" . "{$event->event_start_date}'>{$event->event_day}</a></td>", $calendar);
            }
        }
    }
    $output = $calendar;
    if ($echo) {
        echo $output;
    } else {
        return $output;
    }
}