function album_build_picture_table($user_id, $cat_ids, $AH_thiscat, $auth_data, $start, $sort_method, $sort_order, $total_pics)
{
    global $config, $album_data, $album_config, $template, $user, $lang, $db;
    $viewmode = strpos($cat_ids, ',') != false ? '&mode=' . ALBUM_VIEW_ALL : '';
    if (intval($cat_ids) == album_get_personal_root_id($user_id) && $user_id != ALBUM_PUBLIC_GALLERY) {
        $album_pagination_page_url = 'album.' . PHP_EXT;
    } else {
        $album_pagination_page_url = 'album_cat.' . PHP_EXT;
    }
    $pics_per_page = $album_config['rows_per_page'] * $album_config['cols_per_page'];
    $limit_sql = $start == 0 ? $pics_per_page : $start . ',' . $pics_per_page;
    $pic_approval_sql = 'AND p.pic_approval = 1';
    //if (($AH_thiscat['cat_approval'] != ALBUM_USER) || (($album_config['personal_pics_approval'] == 1) && ($AH_thiscat['cat_user_id'] > 0)))
    if ($AH_thiscat['cat_approval'] != ALBUM_USER || $album_config['personal_pics_approval'] == 1 && album_get_cat_user_id($cat_ids) != false) {
        if ($user->data['user_level'] == ADMIN || $auth_data['moderator'] == 1 && $AH_thiscat['cat_approval'] == ALBUM_MOD) {
            $pic_approval_sql = '';
        }
    }
    $sort_methods_array = array('pic_time', 'pic_title', 'username', 'pic_view_count', 'rating', 'comments', 'new_comment');
    $sort_method = in_array($sort_method, $sort_methods_array) ? $sort_method : $album_config['sort_method'];
    $sort_order_array = array('ASC', 'DESC');
    $sort_order = in_array($sort_order, $sort_order_array) ? $sort_order : $album_config['sort_order'];
    $sort_append = '&sort_method=' . $sort_method . '&sort_order=' . $sort_order;
    switch ($sort_method) {
        case 'pic_time':
            $sort_method_sql = 'p.pic_time';
            break;
        case 'pic_title':
            $sort_method_sql = 'p.pic_title';
            break;
        case 'username':
            $sort_method_sql = 'u.username';
            break;
        case 'pic_view_count':
            $sort_method_sql = 'p.pic_view_count';
            break;
        case 'rating':
            $sort_method_sql = 'rating';
            break;
        case 'comments':
            $sort_method_sql = 'comments';
            break;
        case 'new_comment':
            $sort_method_sql = 'new_comment';
            break;
        default:
            $sort_method_sql = 'p.pic_id';
    }
    $sql = "SELECT ct.cat_user_id, ct.cat_id, ct.cat_title, p.*, u.user_id, u.username, u.user_active, u.user_color, r.rate_pic_id, AVG(r.rate_point) AS rating, COUNT(DISTINCT c.comment_id) AS comments, MAX(c.comment_id) as new_comment\n\t\t\tFROM " . ALBUM_TABLE . " AS p\n\t\t\t\tLEFT JOIN " . USERS_TABLE . " AS u ON p.pic_user_id = u.user_id\n\t\t\t\tLEFT JOIN " . ALBUM_RATE_TABLE . " AS r ON p.pic_id = r.rate_pic_id\n\t\t\t\tLEFT JOIN " . ALBUM_COMMENT_TABLE . " AS c ON p.pic_id = c.comment_pic_id\n\t\t\t\tLEFT JOIN " . ALBUM_CAT_TABLE . " AS ct ON p.pic_cat_id = ct.cat_id\n\t\t\tWHERE p.pic_cat_id IN ({$cat_ids}) {$pic_approval_sql}\n\t\t\tGROUP BY p.pic_id\n\t\t\tORDER BY {$sort_method_sql} {$sort_order}\n\t\t\tLIMIT {$limit_sql}";
    $result = $db->sql_query($sql);
    $picrow = array();
    while ($row = $db->sql_fetchrow($result)) {
        $picrow[] = $row;
    }
    $tot_unapproved = 0;
    for ($i = 0; $i < sizeof($picrow); $i++) {
        if ($picrow[$i]['pic_approval'] == 0) {
            $tot_unapproved++;
        }
    }
    $db->sql_freeresult($result);
    $template->assign_block_vars('index_pics_block', array());
    $template->assign_block_vars('index_pics_block.enable_gallery_title', array());
    for ($i = 0; $i < sizeof($picrow); $i += $album_config['cols_per_page']) {
        $template->assign_block_vars('index_pics_block.picrow', array());
        for ($j = $i; $j < $i + $album_config['cols_per_page']; $j++) {
            if ($j >= sizeof($picrow)) {
                $template->assign_block_vars('index_pics_block.picrow.nopiccol', array());
                $template->assign_block_vars('index_pics_block.picrow.picnodetail', array());
                continue;
                //break;
            }
            //if (($AH_thiscat['cat_approval'] != ALBUM_USER) || (($album_config['personal_pics_approval'] == 1) && ($AH_thiscat['cat_user_id'] > 0)))
            if ($AH_thiscat['cat_approval'] != ALBUM_USER || $album_config['personal_pics_approval'] == 1 && album_get_cat_user_id($cat_ids) != false) {
                if ($user->data['user_level'] == ADMIN || $auth_data['moderator'] == 1 && $AH_thiscat['cat_approval'] == ALBUM_MOD) {
                    $approval_mode = $picrow[$j]['pic_approval'] == 0 ? 'approval' : 'unapproval';
                    $approval_link = '<a href="' . append_sid(album_append_uid('album_modcp.' . PHP_EXT . '?mode=' . $approval_mode . '&amp;pic_id=' . $picrow[$j]['pic_id'])) . '">';
                    $approval_link .= $picrow[$j]['pic_approval'] == 0 ? '<b>' . $lang['Approve'] . '</b>' : $lang['Unapprove'];
                    $approval_link .= '</a>';
                }
            }
            $pic_preview = '';
            $pic_preview_hs = '';
            if ($album_config['lb_preview']) {
                $slideshow_cat = '';
                $slideshow = !empty($slideshow_cat) ? ', { slideshowGroup: \'' . $slideshow_cat . '\' } ' : '';
                $pic_preview_hs = ' class="highslide" onclick="return hs.expand(this' . $slideshow . ');"';
                $pic_preview = 'onmouseover="showtrail(\'' . append_sid(album_append_uid('album_picm.' . PHP_EXT . '?pic_id=' . $picrow[$j]['pic_id'])) . '\',\'' . addslashes($picrow[$j]['pic_title']) . '\', ' . $album_config['midthumb_width'] . ', ' . $album_config['midthumb_height'] . ')" onmouseout="hidetrail()"';
            }
            $template_vars = array('PIC_PREVIEW_HS' => $pic_preview_hs, 'PIC_PREVIEW' => $pic_preview, 'APPROVAL' => $approval_link);
            album_build_column_vars($template_vars, $picrow[$j], $sort_append);
            $template->assign_block_vars('index_pics_block.picrow.piccol', $template_vars);
            if ($picrow[$j]['user_id'] == ALBUM_GUEST || $picrow[$j]['username'] == '') {
                $pic_poster = $picrow[$j]['pic_username'] == '' ? $lang['Guest'] : $picrow[$j]['pic_username'];
            } else {
                $pic_poster = colorize_username($picrow[$j]['user_id'], $picrow[$j]['username'], $picrow[$j]['user_color'], $picrow[$j]['user_active']);
            }
            $edit_rights = $auth_data['edit'] && $picrow[$j]['pic_user_id'] == $user->data['user_id'] || $auth_data['moderator'] && $AH_thiscat['cat_edit_level'] != ALBUM_ADMIN || $user->data['user_level'] == ADMIN ? true : false;
            $delete_rights = $auth_data['delete'] && $picrow[$j]['pic_user_id'] == $user->data['user_id'] || $auth_data['moderator'] && $AH_thiscat['cat_delete_level'] != ALBUM_ADMIN || $user->data['user_level'] == ADMIN ? true : false;
            $admin_rights = $auth_data['moderator'];
            $user_rights = array('edit' => $edit_rights, 'delete' => $delete_rights, 'admin' => $admin_rights);
            $template_vars = array('POSTER' => $pic_poster, 'PIC_PREVIEW_HS' => $pic_preview_hs, 'PIC_PREVIEW' => $pic_preview, 'GROUP_NAME' => 'index', 'APPROVAL' => $approval_link, 'AVATAR_PIC' => $album_config['personal_allow_avatar_gallery'] == 1 && $user->data['user_id'] == $picrow[$j]['pic_user_id'] && $picrow[$j]['cat_user_id'] != 0 ? '<br /><a href="' . append_sid('album_avatar.' . PHP_EXT . '?pic_id=' . $picrow[$j]['pic_id']) . '">' . $lang['Avatar_Set'] . '</a>' : '', 'IMG_BBCODE' => $user->data['user_level'] == ADMIN || $user->data['user_id'] == $picrow[$j]['pic_user_id'] ? '<br /><a href="javasript://" OnClick="window.clipboardData.setData(\'Text\', \'[albumimg]' . $picrow[$j]['pic_id'] . '[/albumimg]\'); return false;">' . $lang['BBCode_Copy'] . '</a>' : '');
            album_build_detail_vars($template_vars, $picrow[$j], $sort_append, $user_rights);
            $template->assign_block_vars('index_pics_block.picrow.pic_detail', $template_vars);
            // Mighty Gorgon - Slideshow - BEGIN
            if ($album_config['show_slideshow']) {
                $last_pic_id = $picrow[$j]['pic_id'];
                $slideshow_link = append_sid(album_append_uid('album_showpage.' . PHP_EXT . '?pic_id=' . $last_pic_id . '&amp;slideshow=5'));
                $slideshow_link_full = '&nbsp;[<a href="' . $slideshow_link . '">' . $lang['Slideshow'] . '</a>]&nbsp;';
            } else {
                $slideshow_link_full = '';
            }
            // Mighty Gorgon - Slideshow - END
            if (is_array($cats)) {
                // is a personal category that the picture belongs to AND
                // is it the main category in the personal gallery ?
                if ($picrow[$j]['cat_user_id'] != 0 && $picrow[$j]['cat_id'] == album_get_personal_root_id($picrow[$j]['cat_user_id'])) {
                    $album_page_url = 'album.' . PHP_EXT;
                } else {
                    $album_page_url = 'album_cat.' . PHP_EXT;
                }
                $image_cat_url = append_sid(album_append_uid($album_page_url . '?cat_id=' . $picrow[$j]['cat_id'] . '&user_id=' . $picrow[$j]['cat_user_id']));
                $template->assign_block_vars('index_pics_block.picrow.pic_detail.cats', array('CATEGORY' => $picrow[$j]['cat_title'], 'U_PIC_CAT' => $image_cat_url));
            }
        }
    }
    $template->assign_vars(array('PAGINATION' => generate_pagination(append_sid(album_append_uid($album_pagination_page_url . '?cat_id=' . intval($cat_ids) . $sort_append . $viewmode)), $total_pics, $pics_per_page, $start), 'SLIDESHOW' => $slideshow_link_full, $waiting = $tot_unapproved == 0 ? "" : $tot_unapproved . $lang['Waiting'], 'WAITING' => $user->data['user_level'] == ADMIN ? $tot_unapproved == 0 ? '&nbsp;' : '<br /><span class="gensmall"><b>' . $tot_unapproved . $lang['Waiting'] . '</b></span>' : '&nbsp;', 'PAGE_NUMBER' => sprintf($lang['Page_of'], floor($start / $pics_per_page) + 1, ceil($total_pics / $pics_per_page))));
}
 }
 // --------------------------------
 // Delete File
 // --------------------------------
 $pic_filename = $thispic['pic_filename'];
 $pic_base_path = IP_ROOT_PATH . ALBUM_UPLOAD_PATH;
 $pic_extra_path = '';
 $pic_new_filename = $pic_extra_path . $pic_filename;
 $pic_fullpath = $pic_base_path . $pic_new_filename;
 @unlink($pic_fullpath);
 // --------------------------------
 // Delete DB entry
 // --------------------------------
 $sql = "DELETE FROM " . ALBUM_TABLE . "\n\t\t\tWHERE pic_id = '" . $pic_id . "'";
 $result = $db->sql_query($sql);
 $is_personal_gallery = album_get_cat_user_id($cat_id) != false ? true : false;
 if ($is_personal_gallery == true) {
     $sql = "SELECT COUNT(pic_id) AS count\n\t\t\tFROM " . ALBUM_TABLE . "\n\t\t\tWHERE pic_user_id = '" . $user->data['user_id'] . "'\n\t\t\tAND pic_cat_id = '" . $cat_id . "'";
     $result = $db->sql_query($sql);
     $personal_pics_count = $db->sql_fetchrow($result);
     $db->sql_freeresult($result);
     $userpics = $personal_pics_count['count'];
     // Check which users category we are in so we don't update the wrong users pic count
     $sql = 'SELECT cat_user_id FROM ' . ALBUM_CAT_TABLE . ' WHERE cat_id = (' . $cat_id . ') LIMIT 1';
     $result = $db->sql_query($sql);
     $usercat = $db->sql_fetchrow($result);
     $db->sql_freeresult($result);
     $cat_user_id = $usercat['cat_user_id'];
     if (!empty($userpics) || $userpics == 0) {
         $sql = "UPDATE " . USERS_TABLE . "\n\t\t\t\tSET user_personal_pics_count = '" . $userpics . "'\n\t\t\t\tWHERE user_id = '" . $cat_user_id . "'";
         $result = $db->sql_query($sql);