if ($calendar_end >= $month_end) {
        $calendar_end = $month_end - 1;
    }
    $wstart = intval(gmdate('d', $calendar_start));
    $wend = intval(gmdate('d', $calendar_end));
    for ($j = $wstart; $j <= $wend; $j++) {
        $days[$j] = true;
    }
}
// Get day events
$events = array();
// topics
$topics_count = 0;
$remaining = $config['topics_per_page'] - $displayed;
$local_start = $start - $displayed;
get_event_topics($events, $topics_count, $start_date, $end_date, true, $local_start, $remaining, $fid);
// set the page title and include the page header
$meta_content['page_title'] = $lang['Calendar_scheduler'];
$meta_content['description'] = '';
$meta_content['keywords'] = '';
$today_birthdays_list = '';
if ($config['calendar_birthday'] == true) {
    $b_year = gmdate('Y', $date);
    $b_month = gmdate('n', $date);
    $b_day = gmdate('j', $date);
    $b_limit = 0;
    $birthdays_list = array();
    $birthdays_list = get_birthdays_list($b_year, true, $b_month, $b_day, 0, $b_limit, false);
    // get the number of occurences
    $number = sizeof($birthdays_list);
    // read users
function display_calendar($main_template, $nb_days = 0, $start = 0, $fid = '')
{
    global $db, $cache, $config, $user, $template, $images, $lang, $bbcode, $tree;
    static $handler;
    if (empty($handler)) {
        $handler = 1;
    } else {
        $handler++;
    }
    $day_of_week = array($lang['datetime']['Sunday'], $lang['datetime']['Monday'], $lang['datetime']['Tuesday'], $lang['datetime']['Wednesday'], $lang['datetime']['Thursday'], $lang['datetime']['Friday'], $lang['datetime']['Saturday']);
    $months = array(' ------------ ', $lang['datetime']['January'], $lang['datetime']['February'], $lang['datetime']['March'], $lang['datetime']['April'], $lang['datetime']['May'], $lang['datetime']['June'], $lang['datetime']['July'], $lang['datetime']['August'], $lang['datetime']['September'], $lang['datetime']['October'], $lang['datetime']['November'], $lang['datetime']['December']);
    // get some parameter
    $first_day_of_week = isset($config['calendar_week_start']) ? intval($config['calendar_week_start']) : 1;
    $nb_row_per_cell = isset($config['calendar_nb_row']) ? intval($config['calendar_nb_row']) : 5;
    // get the start date - calendar doesn't go before 1971
    $cur_date = empty($start) || intval(gmdate('Y', $start)) < 1971 ? cal_date(time(), $config['board_timezone']) : $start;
    $cur_date = gmmktime(0, 0, 0, intval(gmdate('m', $cur_date)), intval(gmdate('d', $cur_date)), intval(gmdate('Y', $cur_date)));
    $cur_month = 0;
    $cur_day = 0;
    // the full month is displayed
    if (empty($nb_days)) {
        // set indicator
        $full_month = true;
        // set the start day on the start of the month
        $start_date = gmmktime(0, 0, 0, intval(gmdate('m', $cur_date)), 01, intval(gmdate('Y', $cur_date)));
        // get the day number set as start of the display
        $cfg_week_day_start = $first_day_of_week;
        // get the number of blank cells
        $start_inc = intval(gmdate('w', $start_date)) - $cfg_week_day_start;
        if ($start_inc < 0) {
            $start_inc = 7 + $start_inc;
        }
        // Used to adjust birthdays SQL
        $cur_month = intval(gmdate('n', $cur_date));
        // get the end date
        $year = intval(gmdate('Y', $start_date));
        $month = intval(gmdate('m', $start_date)) + 1;
        if ($month > 12) {
            $year++;
            $month = 1;
        }
        $end_date = gmmktime(0, 0, 0, $month, 01, $year);
        // set the number of cells per line
        $nb_cells = 7;
        // get the number of rows
        $nb_rows = intval(($start_inc + intval(($end_date - $start_date) / 86400)) / $nb_cells) + 1;
    } else {
        // set indicator
        $full_month = false;
        // set the start date to the day before the date selected
        $start_date = gmmktime(0, 0, 0, gmdate('m', $cur_date), gmdate('d', $cur_date) - 1, gmdate('Y', $cur_date));
        // get the day number set as start of the week
        $cfg_week_day_start = intval(gmdate('w', $start_date));
        // get the numbe of blank cells
        $start_inc = 0;
        // get the end date
        $end_date = gmmktime(0, 0, 0, gmdate('m', $start_date), gmdate('d', $start_date) + $nb_days, gmdate('Y', $start_date));
        // set the number of cells per line
        $nb_cells = $nb_days;
        // set the number of rows
        $nb_rows = 1;
    }
    // Ok, let's get the various events :)
    $events = array();
    $number = 0;
    // topics
    get_event_topics($events, $number, $start_date, $end_date, false, 0, -1, $fid);
    $pages_array = array('calendar.' . PHP_EXT, CMS_PAGE_FORUM, CMS_PAGE_VIEWFORUM);
    //$current_page = $_SERVER['SCRIPT_NAME'];
    $current_page = basename($_SERVER['SCRIPT_NAME']);
    // No limits in calendar
    $day_end = 0;
    $birthdays_limit = 0;
    if ($current_page != 'calendar.' . PHP_EXT) {
        // Limit total birthdays in forum and viewforum... in large could take forever!
        $birthdays_limit = 50;
        // We are not in calendar, so we can force date to today!!!
        $cur_time = time() + 3600 * $config['board_timezone'];
        $cur_month = intval(gmdate('n', $cur_time));
        $cur_day = intval(gmdate('j', $cur_time));
        // Force one week walk forward...
        $days_walk_forward = 7;
        $day_end = intval(gmdate('j', $cur_time + $days_walk_forward * 86400));
    }
    if ($config['calendar_birthday'] == true && in_array(strtolower($current_page), $pages_array)) {
        // get_birthdays(&$events, &$number, $start_date, $end_date, $year = 0, $year_lt = false, $month = 0, $day = 0, $day_end = 0, $limit = 0, $show_inactive = false)
        get_birthdays($events, $number, $start_date, $end_date, 0, false, $cur_month, $cur_day, $day_end, $birthdays_limit, false);
    }
    /*
    for ($i = 0; $i < sizeof($pages_array); $i++)
    {
    	if (strpos(strtolower($current_page), strtolower($pages_array[$i])) !== false)
    	{
    		get_birthdays($events, $number, $start_date, $end_date);
    	}
    }
    */
    // And now display them
    // build a list per date
    $map = array();
    for ($i = 0; $i < sizeof($events); $i++) {
        $event_time = $events[$i]['event_calendar_time'];
        // adjust the event period to the start of day
        $event_time_end = $event_time + $events[$i]['event_calendar_duration'];
        $event_end = gmmktime(0, 0, 0, intval(gmdate('m', $event_time_end)), intval(gmdate('d', $event_time_end)), intval(gmdate('Y', $event_time_end)));
        $event_start = gmmktime(0, 0, 0, intval(gmdate('m', $event_time)), intval(gmdate('d', $event_time)), intval(gmdate('Y', $event_time)));
        if ($event_start < $start_date) {
            $event_start = $start_date;
        }
        if ($event_end > $end_date) {
            $event_end = $end_date;
        }
        // search a free day map offset in the start day
        $event_id = $events[$i]['event_id'];
        $offset_date = $event_start;
        $map_offset = sizeof($map[$event_start]);
        $found = false;
        for ($k = 0; $k < sizeof($map[$event_start]) && !$found; $k++) {
            if ($map[$event_start][$k] == -1) {
                $found = true;
                $map_offset = $k;
            }
        }
        // mark the offset as used for the whole event period
        $offset_date = $event_start;
        while ($offset_date <= $event_end) {
            for ($l = sizeof($map[$offset_date]); $l <= $map_offset; $l++) {
                $map[$offset_date][$l] = -1;
            }
            $map[$offset_date][$map_offset] = $i;
            $offset_date = gmmktime(0, 0, 0, gmdate('m', $offset_date), gmdate('d', $offset_date) + 1, gmdate('Y', $offset_date));
        }
    }
    // template
    $template->set_filenames(array('_calendar_body' . $handler => 'calendar_box.tpl'));
    // buid select list for month
    $month = intval(gmdate('m', $start_date));
    $s_month = '<select name="start_month" onchange="forms[\'f_calendar\'].submit();">';
    for ($i = 1; $i < sizeof($months); $i++) {
        $selected = $month == $i ? ' selected="selected"' : '';
        $s_month .= '<option value="' . $i . '"' . $selected . '>' . $months[$i] . '</option>';
    }
    $s_month .= '</select>';
    // buid select list for year
    $year = intval(gmdate('Y', $start_date));
    $s_year = '<select name="start_year" onchange="forms[\'f_calendar\'].submit();">';
    for ($i = 1971; $i < 2070; $i++) {
        $selected = $year == $i ? ' selected="selected"' : '';
        $s_year .= '<option value="' . $i . '"' . $selected . '>' . $i . '</option>';
    }
    $s_year .= '</select>';
    // build a forum select list
    $s_forum_list = '<select name="selected_id" onchange="forms[\'f_calendar\'].submit();">' . get_tree_option($fid) . '</select>';
    // header
    $config['calendar_display_open'] = false;
    $template->assign_vars(array('UP_ARROW' => $images['cal_up_arrow'], 'DOWN_ARROW' => $images['cal_down_arrow'], 'UP_ARROW2' => $images['arrow_up'], 'DOWN_ARROW2' => $images['arrow_down'], 'TOGGLE_ICON' => $config['calendar_display_open'] == false ? $images['cal_up_arrow'] : $images['cal_down_arrow'], 'TOGGLE_ICON2' => $config['calendar_display_open'] == false ? $images['arrow_up'] : $images['arrow_down'], 'TOGGLE_STATUS' => $config['calendar_display_open'] == false ? 'none' : ''));
    $prec = gmdate('Ym', $start_date) > 197101 ? gmdate('Ymd', gmmktime(0, 0, 0, gmdate('m', $start_date) - 1, 01, gmdate('Y', $start_date))) : gmdate('Ymd', $start_date);
    $next = gmdate('Ymd', gmmktime(0, 0, 0, gmdate('m', $start_date) + 1, 01, gmdate('Y', $start_date)));
    $template->assign_block_vars('_calendar_box', array('L_CALENDAR' => '<a href="' . append_sid(IP_ROOT_PATH . 'calendar.' . PHP_EXT . '?start=' . gmdate('Ymd', cal_date(time(), $config['board_timezone']))) . '"><img src="' . $images['icon_calendar'] . '" hspace="3" style="vertical-align: top;" alt="' . $lang['Calendar_event'] . '" /></a>' . $lang['Calendar'], 'L_CALENDAR_TXT' => $lang['Calendar'], 'SPAN_ALL' => $nb_cells, 'S_MONTH' => $s_month, 'S_YEAR' => $s_year, 'S_FORUM_LIST' => $s_forum_list, 'L_GO' => $lang['Go'], 'ACTION' => append_sid(IP_ROOT_PATH . 'calendar.' . PHP_EXT), 'U_PREC' => append_sid('calendar.' . PHP_EXT . '?start=' . $prec . '&amp;fid=' . $fid), 'U_NEXT' => append_sid('calendar.' . PHP_EXT . '?start=' . $next . '&amp;fid=' . $fid)));
    if ($full_month) {
        $template->assign_block_vars('_calendar_box.switch_full_month', array());
        $offset = $cfg_week_day_start;
        for ($j = 0; $j < $nb_cells; $j++) {
            if ($offset >= sizeof($day_of_week)) {
                $offset = 0;
            }
            $template->assign_block_vars('_calendar_box.switch_full_month._cell', array('WIDTH' => floor(100 / $nb_cells), 'L_DAY' => $day_of_week[$offset]));
            $offset++;
        }
    } else {
        $template->assign_block_vars('_calendar_box.switch_full_month_no', array());
    }
    // display
    $offset_date = gmmktime(0, 0, 0, gmdate('m', $start_date), gmdate('d', $start_date) - $start_inc, gmdate('Y', $start_date));
    for ($i = 0; $i < $nb_rows; $i++) {
        $template->assign_block_vars('_calendar_box._row', array());
        for ($j = 0; $j < $nb_cells; $j++) {
            $span = 1;
            // date less than start
            if (intval(gmdate('Ymd', $offset_date)) < intval(gmdate('Ymd', $start_date))) {
                // compute the cell to span
                $span = $start_inc;
                $j = $start_inc - 1;
                $offset_date = gmmktime(0, 0, 0, gmdate('m', $start_date), gmdate('d', $start_date) - 1, gmdate('Y', $start_date));
            }
            // date greater than last
            if (intval(gmdate('Ymd', $offset_date)) >= intval(gmdate('Ymd', $end_date))) {
                // compute the cell to span
                $span = $nb_cells - $j;
                $j = $nb_cells;
            }
            $format = intval(gmdate('Ymd', $offset_date)) == intval(gmdate('Ymd', cal_date(time(), $config['board_timezone']))) ? '<b>%s</b>' : '%s';
            $template->assign_block_vars('_calendar_box._row._cell', array('WIDTH' => floor(100 / $nb_cells), 'SPAN' => $span, 'DATE' => sprintf($format, date_dsp(($full_month ? '' : 'D ') . $lang['DATE_FORMAT_CALENDAR'], $offset_date)), 'U_DATE' => append_sid(IP_ROOT_PATH . 'calendar_scheduler.' . PHP_EXT . '?d=' . $offset_date . '&amp;fid=' . $fid)));
            // blank cells
            if (intval(gmdate('Ymd', $offset_date)) >= intval(gmdate('Ymd', $start_date)) && intval(gmdate('Ymd', $offset_date)) < intval(gmdate('Ymd', $end_date))) {
                $template->assign_block_vars('_calendar_box._row._cell.switch_filled', array('EVENT_DATE' => $offset_date, 'TOGGLE_STATUS' => 'none', 'TOGGLE_ICON' => $images['arrow_down']));
                // send events
                $more = false;
                $over = sizeof($map[$offset_date]) > $nb_row_per_cell;
                for ($k = 0; $k < sizeof($map[$offset_date]); $k++) {
                    // we are just over the limit
                    if ($over && $k == $nb_row_per_cell) {
                        $more = true;
                        $template->assign_block_vars('_calendar_box._row._cell.switch_filled._event._more_header', array());
                    }
                    $ind = $map[$offset_date][$k];
                    $template->assign_block_vars('_calendar_box._row._cell.switch_filled._event', array('U_EVENT' => $events[$ind]['event_link'], 'EVENT_TYPE' => $events[$ind]['event_type_icon'], 'EVENT_TITLE' => $events[$ind]['event_short_title'], 'EVENT_CLASS' => $events[$ind]['event_txt_class'], 'EVENT_MESSAGE' => str_replace(array('"'), array('&quot;'), addslashes($events[$ind]['event_message']))));
                    $flag = $over && $k == $nb_row_per_cell - 1;
                    if ($ind > -1) {
                        $template->assign_block_vars('_calendar_box._row._cell.switch_filled._event.switch_event', array());
                        if ($flag) {
                            $template->assign_block_vars('_calendar_box._row._cell.switch_filled._event.switch_event._more', array());
                        } else {
                            $template->assign_block_vars('_calendar_box._row._cell.switch_filled._event.switch_event._more_no', array());
                        }
                    } else {
                        $template->assign_block_vars('_calendar_box._row._cell.switch_filled._event.switch_event_no', array());
                        if ($flag) {
                            $template->assign_block_vars('_calendar_box._row._cell.switch_filled._event.switch_event_no._more', array());
                        } else {
                            $template->assign_block_vars('_calendar_box._row._cell.switch_filled._event.switch_event_no._more_no', array());
                        }
                    }
                    if ($k == sizeof($map[$offset_date]) - 1 && $more) {
                        $template->assign_block_vars('_calendar_box._row._cell.switch_filled._event._more_footer', array());
                    }
                }
            } else {
                $template->assign_block_vars('_calendar_box._row._cell.switch_filled_no', array());
            }
            $offset_date = gmmktime(0, 0, 0, gmdate('m', $offset_date), gmdate('d', $offset_date) + 1, gmdate('Y', $offset_date));
        }
    }
    // fill the main template
    $template->assign_var_from_handle($main_template, '_calendar_body' . $handler);
}