function au_validate_offset($offset, $items_per_page, $max_offset)
{
    if ($offset < 0) {
        $offset = 0;
    } else {
        if ($offset > $max_offset) {
            $offset = $max_offset;
        }
    }
    $new_offset = ceil($offset) % $items_per_page === 0 ? ceil($offset) : round(($offset + $items_per_page / 2) / $items_per_page) * $items_per_page;
    // We need to check it though
    while (au_check_offset($new_offset, $items_per_page, $max_offset) === false) {
        $new_offset = $new_offset - $items_per_page;
    }
    return $new_offset;
}
function au_show_blogindex()
{
    // $aulis might come in handy here
    global $aulis, $setting;
    // Did our lovely user specify an offset?
    if (isset($_GET['offset']) && is_numeric($_GET['offset'])) {
        $offset = $_GET['offset'];
    } else {
        $offset = 0;
    }
    // Extra parameters, in case we need them
    $extra_parameters = '';
    // Are we searching?
    if (isset($_REQUEST['search']) && !isset($_REQUEST['category'], $_REQUEST['tag'])) {
        // Empty searches are not allowed
        if (trim($_REQUEST['search']) == '') {
            au_blog_url('', true);
        }
        // Do we maybe need to clean a search string before redirecting?
        if (isset($_POST['search']) && $_POST['search'] != '') {
            // Do we need to clean it or is urlencode enough?
            if ($setting['enable_blog_url_rewriting'] == 1) {
                $search_string = au_string_clean($_POST['search']);
            } else {
                $search_string = urlencode($_POST['search']);
            }
            // The cleaned string cannot be empty
            if ($search_string != '') {
                // Redirect to clean url if possible
                if ($setting['enable_blog_url_rewriting'] == 1) {
                    au_url('blog/search/' . $search_string, true);
                } else {
                    au_url('?app=blogindex&search=' . $search_string, true);
                }
            }
            // Nothing more here for us now
            die;
        }
        // Do we maybe need to unclean the search string?
        if (isset($_GET['search']) && isset($_GET['rewrite'])) {
            $aulis['blog_search'] = str_replace("+", " ", $_GET['search']);
            $aulis['blog_search'] = str_replace("-plus-", "+", $aulis['blog_search']);
        } else {
            if (isset($_GET['search'])) {
                $aulis['blog_search'] = urldecode($_GET['search']);
            } else {
                $aulis['blog_search'] = '';
            }
        }
        // Time to form the extra parameters
        $exploded_search = explode(' ', $aulis['blog_search']);
        foreach ($exploded_search as $keyword) {
            $regex = "REGEXP '[[:<:]]" . au_db_escape($keyword) . "[[:>:]]'";
            $extra_parameters .= " AND (entries.blog_name {$regex} OR entries.blog_intro {$regex} OR entries.blog_content {$regex})";
        }
    }
    // Do we have to add parameters for category or tag to the query?
    if (isset($_GET['category']) && is_numeric($_GET['category']) && !isset($_GET['search'], $_GET['tag']) && ($aulis['blog_category'] = $_GET['category'])) {
        $extra_parameters .= ' AND ' . (isset($_GET['category']) ? "entries.blog_category = {$_GET['category']}" : '');
    }
    // Let's build the query
    $query = "SELECT entries.*, COUNT(comments.comment_id) AS comment_count, categories.category_name AS category_name\n\tFROM blog_entries AS entries \n\tLEFT JOIN blog_comments AS comments ON entries.entry_id = comments.blog_id \n\tLEFT JOIN blog_categories AS categories ON entries.blog_category = categories.category_id \n\tWHERE entries.blog_activated = 1 and entries.blog_in_queue = 0 {$extra_parameters} GROUP BY entries.entry_id ORDER BY entries.blog_date DESC;";
    // We are building the simple version of the query now, because that's enough to get info from
    $query_simple_parameters = "entries.blog_activated = 1 and entries.blog_in_queue = 0 {$extra_parameters}";
    // We are getting the maximum offset and the initial row count right now
    $query_info = au_get_max_blog_offset($query_simple_parameters);
    // We need to check if our offset is, like, alright, otherwise we need to redirect
    if (!au_check_offset($offset, THEME_BLOG_ENTRIES_PER_PAGE, $query_info['max_offset'])) {
        $_GET['offset'] = au_validate_offset($offset, THEME_BLOG_ENTRIES_PER_PAGE, $query_info['max_offset']);
        au_blog_url($_GET, true);
    }
    // Let's load all blog entries that are activated and are not in the queue (thus are published)
    $entries = au_parse_pagination($query, true, $offset, THEME_BLOG_ENTRIES_PER_PAGE, $query_info['row_count']);
    // For each blog item, we want to show its preview
    while ($entry = $entries['paged']->fetchObject()) {
        au_show_blog_preview($entry);
    }
    // We might want to transfer the information about the pagination, so that it can be used in the template
    $aulis['blog_count'] = $query_info['row_count'];
    $aulis['blog_current_offset'] = $offset;
    $aulis['blog_next_offset'] = $entries['next_position'];
    $aulis['blog_previous_offset'] = $entries['previous_position'];
    $aulis['blog_max_offset'] = $query_info['max_offset'];
    // This will load the wrapper! :)
    return au_load_template('blog_index');
}