function album_move_tree($cat_id, $move, $user_id = ALBUM_PUBLIC_GALLERY)
{
    global $db, $album_data;
    // if the album_tree is NOT filled then reload the data
    // this will ensure that the album IS populated with data
    if (sizeof($album_data['data']) == 0) {
        album_read_tree($user_id);
    }
    // 'search' the object
    $AH_this = isset($album_data['keys'][$cat_id]) ? $album_data['keys'][$cat_id] : ALBUM_ROOT_CATEGORY;
    // get the root or parent cat id
    $parent = $AH_this < 0 ? ALBUM_ROOT_CATEGORY : $album_data['parent'][$AH_this];
    // renum objects of the same level and regenerate all
    $order = 0;
    $cats = array();
    $parents = array();
    // for the nuber of rows read/categories do this loop
    for ($i = 0; $i < sizeof($album_data['data']); $i++) {
        // ------------------------------------------------------------------------
        // if the current itetorated parent id is equal to the selected category's parent id then
        // reorder the cat_order, the way that, if the found category is the selected category
        // then move the category by the sequentual order number + 'move direction value'
        // else give it the sequentual order number...this will ensure that the selected category
        // always is moved up or down compared to its siblings
        // ------------------------------------------------------------------------
        if ($album_data['parent'][$i] == $parent) {
            $order = $order + 10;
            $neworder = $i == $AH_this ? $order + $move : $order;
            $album_data['data'][$i]['cat_order'] = $neworder;
        }
        // ------------------------------------------------------------------------
        // fill these arrays which are going to be need in building the tree
        // (see album_read_tree for similiar code)
        // ------------------------------------------------------------------------
        $idx = sizeof($cats);
        $cats[$idx] = $album_data['data'][$i];
        $parents[$album_data['parent'][$i]][] = $idx;
    }
    // rebuild the tree
    $album_data = array();
    album_build_tree($cats, $parents);
    // ------------------------------------------------------------------------
    // re-order all categories...in the database acording to the album_tree
    // is really the same things as the reorder_cat in admin/album_cat.php
    // ------------------------------------------------------------------------
    $order = 0;
    for ($i = 0; $i < sizeof($album_data['data']); $i++) {
        $order = $order + 10;
        $sql = "UPDATE " . ALBUM_CAT_TABLE . " SET cat_order={$order} WHERE cat_id=" . $album_data['id'][$i];
        $db->sql_query($sql);
    }
}
function album_build_tree(&$cats, &$parents, $level = ALBUM_ROOT_CATEGORY, $parent = ALBUM_ROOT_CATEGORY)
{
    global $db, $album_data, $album_config;
    $album_data_level = array();
    // add the categories of this level
    for ($i = 0; $i < sizeof($parents[$parent]); $i++) {
        $idx = $parents[$parent][$i];
        $album_data_level['id'][] = $cats[$idx]['cat_id'];
        $album_data_level['sort'][] = $cats[$idx][$album_config['album_category_sorting']];
        $album_data_level['data'][] = $cats[$idx];
        $album_data_level['personal'][] = $cats[$idx]['cat_user_id'] == 0 ? 0 : 1;
    }
    // sort the tree level acordingly to the desired category sort
    if (!empty($album_data_level['data'])) {
        if ($album_config['album_category_sorting'] != 'cat_order') {
            if ($album_config['album_category_sorting_direction'] == 'ASC') {
                array_multisort($album_data_level['sort'], SORT_ASC, $album_data_level['id'], $album_data_level['data']);
            } else {
                array_multisort($album_data_level['sort'], SORT_DESC, $album_data_level['id'], $album_data_level['data']);
            }
        } else {
            array_multisort($album_data_level['sort'], SORT_ASC, $album_data_level['id'], $album_data_level['data']);
        }
    }
    // add the tree_level to the tree
    $level++;
    for ($i = 0; $i < sizeof($album_data_level['data']); $i++) {
        $AH_this = sizeof($album_data['data']);
        $key = $album_data_level['id'][$i];
        $album_data['sub'][$parent][] = $key;
        $album_data['keys'][$key] = $AH_this;
        $album_data['parent'][] = $parent;
        $album_data['id'][] = $album_data_level['id'][$i];
        $album_data['data'][] = $album_data_level['data'][$i];
        $album_data['personal'][$key] = $album_data_level['personal'][$i];
        // add sub levels
        album_build_tree($cats, $parents, $level, $key);
    }
    return;
}