/*
     if we still get layout issues then replace the template file with this
     ADM_TPL . 'album_cat_body_debug.tpl', BUT ONLY FOR DEBUGGING PURPOSE, and send me a screenshot of it
     then go back to this template file ADM_TPL . 'album_cat_body.tpl'.
     */
     $template->set_filenames(array('body' => ADM_TPL . 'album_cat_body.tpl'));
     $template->assign_vars(array('S_ALBUM_ACTION' => append_sid('admin_album_cat.' . PHP_EXT), 'L_CREATE_CATEGORY' => $lang['Create_category'], 'L_SYNC_PICS_COUNTER' => $lang['Cat_Pics_Synchronize'], 'L_ALBUM_INDEX' => $lang['Album_Categories_Title']));
     // get the values of level selected
     if (!empty($cat_id)) {
         $parent = $cat_id;
     }
     if (!isset($album_cat_tree['keys'][$parent])) {
         $parent = ALBUM_ROOT_CATEGORY;
     }
     // display the tree
     album_display_admin_index($parent);
     $template->pparse('body');
     include IP_ROOT_PATH . ADM . '/page_footer_admin.' . PHP_EXT;
 } else {
     if ($_GET['action'] == 'edit') {
         $cat_id = intval($_GET['cat_id']);
         $sql = "SELECT cat.*, cat2.cat_title AS cat_parent_title, cat2.cat_id AS cat_parent_id\n\t\t\t\t\tFROM " . ALBUM_CAT_TABLE . " AS cat LEFT OUTER JOIN " . ALBUM_CAT_TABLE . " AS cat2\n\t\t\t\t\tON cat2.cat_id = cat.cat_parent WHERE cat.cat_id = '{$cat_id}'";
         $result = $db->sql_query($sql);
         if ($db->sql_numrows($result) == 0) {
             message_die(GENERAL_ERROR, 'The requested category is not existed');
         }
         $catrow = $db->sql_fetchrow($result);
         album_read_tree();
         $s_album_cat_list = album_get_tree_option($catrow['cat_parent_id'], ALBUM_AUTH_VIEW, ALBUM_SELECTBOX_INCLUDE_ALL | ALBUM_SELECTBOX_INCLUDE_ROOT);
         $template->set_filenames(array('body' => ADM_TPL . 'album_cat_new_body.tpl'));
         $template->assign_block_vars('acp', array('L_ALBUM_CAT_TITLE' => $lang['Album_Categories_Title'], 'L_ALBUM_CAT_EXPLAIN' => $lang['Album_Categories_Explain']));
function album_display_admin_index($cur = ALBUM_ROOT_CATEGORY, $level = 0, $max_level = -1, $column_offset = 1)
{
    global $db, $template, $images, $user, $lang, $user_id, $album_data;
    static $username = '';
    // display 'the' level
    $AH_this = isset($album_data['keys'][$cur]) ? $album_data['keys'][$cur] : ALBUM_ROOT_CATEGORY;
    //-1;
    if (defined('IN_ADMIN')) {
        $admin_url = 'admin_album_cat.' . PHP_EXT;
        $is_root = false;
    } else {
        $admin_url = 'album_personal_cat_admin.' . PHP_EXT;
        $is_root = $AH_this == ALBUM_ROOT_CATEGORY || $AH_this == 0 ? true : false;
    }
    // root level
    if ($AH_this == ALBUM_ROOT_CATEGORY) {
        $level = ALBUM_ROOT_CATEGORY;
        // the the maximum level of categories,counting from the root category
        // this is used in the indentation of the categories
        $keys = array();
        $max_level = album_get_max_depth($keys, $cur, ALBUM_AUTH_VIEW, true);
        $template->assign_vars(array('INC_SPAN_ALL' => $max_level + 4, 'HEADER_INC_SPAN' => $max_level + 1, 'L_ALBUM_TITLE' => $lang['Category_Title'], 'L_ALBUM_ACTION' => $lang['Album_Categories_Title'], 'L_ALBUM_CAT_TITLE' => $lang['Album_Categories_Title'], 'L_ALBUM_CAT_EXPLAIN' => $lang['Album_Categories_Explain']));
        // get user name of the root category
        $username = album_get_user_name($album_data['data'][0]['cat_user_id']);
        //$album_data['data'][0]['username'];
        if (defined('IN_ADMIN')) {
            $template->assign_block_vars('switch_board_footer', array());
        }
    }
    // if we are above the 'root' level, thenadd it to the template (root level is -1)
    if ($AH_this > ALBUM_ROOT_CATEGORY) {
        // display a cat row
        $cat = $album_data['data'][$AH_this];
        $cat_id = $album_data['id'][$AH_this];
        // get the class colors
        $class_catLeft = 'cat';
        $class_catMiddle = 'cat';
        $class_catRight = 'cat';
        // get category title
        $cat_title = $is_root ? htmlspecialchars(sprintf($lang['Personal_Gallery_Of_User'], $username)) : $cat['cat_title'];
        // send to template
        $template->assign_block_vars('catrow', array());
        $template->assign_block_vars('catrow.cathead', array('CAT_ID' => $cat_id, 'CAT_TITLE' => $cat_title, 'CLASS_CATLEFT' => $class_catLeft, 'CLASS_CATRIGHT' => $class_catRight, 'CLASS_CATMIDDLE' => $class_catMiddle, 'WIDTH' => $max_level == $level ? 'width="50%"' : '', 'INC_SPAN' => $max_level - $level + 1, 'U_CAT_EDIT' => append_sid(album_append_uid($admin_url . '?action=edit&cat_id=' . $cat_id)), 'U_CAT_DELETE' => $is_root && $user->data['user_level'] != ADMIN ? '' : append_sid(album_append_uid("{$admin_url}?action=delete&cat_id={$cat_id}")), 'U_CAT_MOVE_UP' => $is_root ? '' : append_sid(album_append_uid($admin_url . '?action=move&move=-15&cat_id=' . $cat_id)), 'U_CAT_MOVE_DOWN' => $is_root ? '' : append_sid(album_append_uid($admin_url . '?action=move&move=15&cat_id=' . $cat_id)), 'U_VIEWCAT' => append_sid(album_append_uid($admin_url . '?action=edit&cat_id=' . $cat_id)), 'L_MOVE_UP' => $is_root ? '' : $lang['MOVE_UP'], 'L_MOVE_DOWN' => $is_root ? '' : $lang['MOVE_DOWN'], 'L_EDIT' => $lang['Edit'], 'L_DELETE' => $is_root && $user->data['user_level'] != ADMIN ? '' : $lang['Delete']));
        // add expandsion of the row height in order to have room for the description
        $rowspan = empty($cat['cat_desc']) ? 1 : 2;
        // creat a table data for each level down to the level where the title (and maybe description) is
        // in other words put 'X' times '<td class="row2" rowspan="1" width="46">&nbsp;</td>' string in
        // or incase there is a desciption use this text '<td class="row2" rowspan="2" width="46">&nbsp;'1'</td>' instead
        for ($k = 1; $k <= $level; $k++) {
            $template->assign_block_vars('catrow.cathead.inc', array('ROWSPAN' => $rowspan));
        }
        // send the category description to template... if its specified
        if (!empty($cat['cat_desc'])) {
            $cat_desc = $cat['cat_desc'];
            $template->assign_block_vars('catrow', array());
            $template->assign_block_vars('catrow.cattitle', array('CAT_DESCRIPTION' => $cat_desc, 'INC_SPAN_ALL' => $max_level - $level + 4));
        }
    }
    // if we are above the root level
    // display the sub-level
    for ($i = 0; $i < sizeof($album_data['sub'][$cur]); $i++) {
        $column_offset = album_display_admin_index($album_data['sub'][$cur][$i], $level + 1, $max_level, $column_offset);
    }
    // if we are 'above' the root level then add the add category 'footer'
    if ($AH_this > ALBUM_ROOT_CATEGORY) {
        // cat footer, add the footer
        $template->assign_block_vars('catrow', array());
        $template->assign_block_vars('catrow.catfoot', array('S_ADD_NAME' => "name[{$cat_id}]", 'INC_SPAN' => $max_level - $level + 1, 'INC_SPAN_ALL' => $max_level - $level + 4, 'S_ADD_CAT_SUBMIT' => "addcategory[{$cat_id}]"));
    }
    // add indentation to the display
    for ($k = 1; $k <= $level; $k++) {
        $template->assign_block_vars('catrow.catfoot.inc', array());
    }
    // at the moment is isn't used, so it might be removed, but not until
    // this patch has been approved by other ppl then me.
    return $column_offset;
}