function smarty_function_calendar($params, &$smartyObj)
{
    $bBlog =& $smartyObj->get_template_vars("bBlog_object");
    $date = getdate();
    $today = $date["mday"];
    $month = $date["mon"];
    $year = $date["year"];
    $new_month = $_GET["month"];
    $new_year = $_GET["year"];
    if ($new_month && $new_year) {
        $date = getdate(mktime(0, 0, 0, $new_month, 1, $new_year));
        $show_month = $date["mon"];
        $show_year = $date["year"];
    } else {
        $show_month = $month;
        $show_year = $year;
    }
    $q = $bBlog->make_post_query(array("where" => " AND month(FROM_UNIXTIME(posttime)) = {$show_month} and year(FROM_UNIXTIME(posttime)) = {$show_year} ", "num" => "999"));
    $dayindex = array();
    global $dayindex;
    $posts = $bBlog->get_posts($q);
    if (is_array($posts)) {
        // Check to see if we've retrieved "No posts found"
        if (count($posts) > 1 || $posts[0]['title'] != "No posts found") {
            foreach ($posts as $post) {
                $d = date('j', $post['posttime']);
                $dayindex[$d][] = array("id" => $post['post'], "title" => $post['title'], "url" => $bBlog->_get_entry_permalink($post['postid']));
            }
        }
    }
    $left_year = $right_year = $show_year;
    $left_month = $show_month - 1;
    if ($left_month < 1) {
        $left_month = 12;
        $left_year--;
    }
    $right_month = $show_month + 1;
    if ($right_month > 12) {
        $right_month = 1;
        $right_year++;
    }
    $bBlog->smartyObj->assign("left", $_SERVER["PHP_SELF"] . "?month={$left_month}&amp;year={$left_year}");
    $bBlog->smartyObj->assign("right", $_SERVER["PHP_SELF"] . "?month={$right_month}&amp;year={$right_year}");
    $bBlog->smartyObj->assign("header", strftime("%B %Y", mktime(0, 0, 0, $show_month, 1, $show_year)));
    $first_date = mktime(0, 0, 0, $show_month, 1, $show_year);
    $date = getdate($first_date);
    $first_wday = $date["wday"];
    $last_date = mktime(0, 0, 0, $show_month + 1, 0, $show_year);
    $date = getdate($last_date);
    $last_day = $date["mday"];
    $wday = "";
    // echo($params["locale"]);
    if ($params["locale"]) {
        @setlocale(LC_TIME, $params["locale"]);
    }
    $week_start = $params["week_start"];
    if ($week_start < 0 || $week_start > 6) {
        $week_start = 1;
    }
    for ($counter = $week_start; $counter < $week_start + 7; $counter++) {
        if ($counter > 6) {
            $wday[] = strftime("%a", mktime(0, 0, 0, 3, $counter - 7, 2004));
        } else {
            $wday[] = strftime("%a", mktime(0, 0, 0, 3, $counter, 2004));
        }
    }
    $bBlog->smartyObj->assign("wday", $wday);
    $week_array = "";
    $month_array = "";
    $pre_counter = $first_wday - $week_start;
    if ($pre_counter < 0) {
        $pre_counter += 7;
    }
    $day = 1;
    while (true) {
        $week_array = "";
        for ($counter = 0; $counter < 7; $counter++) {
            if ($day > $last_day) {
                $week_array[] = array(0 => false, 1 => "&nbsp;", 2 => false);
            } else {
                if ($pre_counter > 0) {
                    $week_array[] = array(0 => false, 1 => "&nbsp;", 2 => false);
                    $pre_counter--;
                } else {
                    getDateLink($day, $values);
                    $week_array[] = array(0 => $dayindex["{$day}"] ? true : false, 1 => $day, 2 => $day == $today && $month == $show_month && $year == $show_year ? true : false);
                    $day++;
                }
            }
        }
        $month_array[] = $week_array;
        if ($day > $last_day) {
            break;
        }
    }
    $bBlog->smartyObj->assign("month", $month_array);
    $bBlog->smartyObj->assign("values", $values);
    $content = $bBlog->smartyObj->fetch("calendar.html", FALSE);
    return $content;
}
function smarty_function_calendar($params, &$loq)
{
    $date = getdate();
    $today = $date["mday"];
    $month = $date["mon"];
    $year = $date["year"];
    $new_month = isset($_GET["month"]) ? $_GET['month'] : false;
    $new_year = isset($_GET["year"]) ? $_GET['year'] : false;
    if ($new_month && $new_year) {
        $date = getdate(mktime(0, 0, 0, $new_month, 1, $new_year));
        $show_month = $date["mon"];
        $show_year = $date["year"];
    } else {
        $show_month = $month;
        $show_year = $year;
    }
    $dayindex = array();
    global $dayindex;
    $posts = $loq->_ph->get_posts(array("where" => " AND month(FROM_UNIXTIME(posttime)) = {$show_month} and year(FROM_UNIXTIME(posttime)) = {$show_year} ", "num" => "999"));
    if ($posts) {
        //var_dump($posts);
        if (!isset($posts['title'])) {
            while ($post = $posts->FetchRow()) {
                $d = date('j', $post['posttime']);
                $dayindex[$d][] = array("id" => $post['post'], "title" => $post['title'], "url" => $loq->_get_entry_permalink($post['postid']));
            }
        }
    }
    $right_year = $show_year;
    $left_year = $right_year;
    $left_month = $show_month - 1;
    if ($left_month < 1) {
        $left_month = 12;
        $left_year--;
    }
    $right_month = $show_month + 1;
    if ($right_month > 12) {
        $right_month = 1;
        $right_year++;
    }
    $loq->assign("left", $_SERVER["PHP_SELF"] . "?month={$left_month}&year={$left_year}");
    $loq->assign("right", $_SERVER["PHP_SELF"] . "?month={$right_month}&year={$right_year}");
    $loq->assign("header", strftime("%B %Y", mktime(0, 0, 0, $show_month, 1, $show_year)));
    $first_date = mktime(0, 0, 0, $show_month, 1, $show_year);
    $date = getdate($first_date);
    $first_wday = $date["wday"];
    $last_date = mktime(0, 0, 0, $show_month + 1, 0, $show_year);
    $date = getdate($last_date);
    $last_day = $date["mday"];
    $wday = array();
    if ($params["locale"]) {
        @setlocale(LC_TIME, $params["locale"]);
    }
    $week_start = isset($params["week_start"]) ? $params['week_start'] : 0;
    if ($week_start < 0 || $week_start > 6) {
        $week_start = 1;
    }
    //To generate the date, we use March, 2004 as the first day of the month falls on a sunday
    //This way we are assured of generating the weekday names in order without needing AI
    for ($counter = $week_start; $counter < $week_start + 7; $counter++) {
        $wday[] = strftime('%a', mktime(0, 0, 0, 3, $counter, 2004));
    }
    $loq->assign("wday", $wday);
    $week_array = array();
    $month_array = array();
    $pre_counter = $first_wday - $week_start;
    if ($pre_counter < 0) {
        $pre_counter += 7;
    }
    $day = 1;
    while ($day < $last_day) {
        $week_array = array();
        for ($counter = 0; $counter < 7; $counter++) {
            if ($day > $last_day) {
                $week_array[] = array(0 => false, 1 => "&nbsp;", 2 => false);
            } else {
                if ($pre_counter > 0) {
                    $week_array[] = array(0 => false, 1 => "&nbsp;", 2 => false);
                    $pre_counter--;
                } else {
                    getDateLink($day, $values);
                    $week_array[] = array(0 => $dayindex["{$day}"] ? true : false, 1 => $day, 2 => $day == $today && $month == $show_month && $year == $show_year ? true : false);
                    $day++;
                }
            }
        }
        $month_array[] = $week_array;
    }
    $loq->assign("month", $month_array);
    $loq->assign("values", $values);
    $loq->display("calendar.html", FALSE);
}