Index: wp-includes/template-functions-category.php
===================================================================
--- wp-includes/template-functions-category.php	(revision 3568)
+++ wp-includes/template-functions-category.php	(working copy)
@@ -410,4 +410,60 @@
 		return false;
 }
 
+function &get_categories($args = '') {
+	global $wpdb, $category_links;
+
+	parse_str($args, $r);
+
+	if ( !isset($r['type']) )  // 'post' or 'link'
+		$r['type'] = 'post';
+	if ( !isset($r['child_of']) )
+		$r['child_of'] = 0;
+	if ( !isset($r['orderby']) )
+		$r['orderby'] = 'name';
+	if ( !isset($r['order']) )
+		$r['order'] = 'ASC';
+	if ( !isset($r['hide_empty']) )
+		$r['hide_empty'] = true;
+
+	$r['orderby'] = "cat_" . $r['orderby'];
+
+	$exclusions = '';
+	if ( !empty($r['exclude']) ) {
+		$excategories = preg_split('/[\s,]+/',$r['exclude']);
+		if ( count($excategories) ) {
+			foreach ( $excategories as $excat ) {
+				$exclusions .= ' AND cat_ID <> ' . intval($excat) . ' ';
+			}
+		}
+	}
+
+	$categories = $wpdb->get_results("SELECT * " .
+		"FROM $wpdb->categories " .
+		"$exclusions " .
+		"ORDER BY " . $r['orderby'] . " " . $r['order']);
+
+	if ( empty($categories) )
+		return array();
+
+	if ( $r['hide_empty'] ) {
+		foreach ( $categories as $category ) {
+			$count = 0;
+			if ( 'link' == $r['type'] ) {
+				$count = $category->link_count;
+			} else {
+				$count = $category->category_count;	
+			}
+			if ( $count )
+				$the_categories[] = $category; 
+		}
+		$categories = $the_categories;
+	}
+
+	/* if ( $r['child_of'] )
+		$categories = & get_category_children($r['child_of'], $categories); */
+
+	return $categories;
+}
+
 ?>
Index: wp-includes/links.php
===================================================================
--- wp-includes/links.php	(revision 3568)
+++ wp-includes/links.php	(working copy)
@@ -28,21 +28,16 @@
                          $limit = -1, $show_updated = 0) {
     global $wpdb;
     $cat_id = -1;
-    $results = $wpdb->get_results("SELECT cat_id FROM $wpdb->linkcategories WHERE cat_name='$cat_name'");
+    $results = $wpdb->get_results("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$cat_name'");
     if ($results) {
         foreach ($results as $result) {
-            $cat_id = $result->cat_id;
+            $cat_id = $result->cat_ID;
         }
     }
     get_links($cat_id, $before, $after, $between, $show_images, $orderby,
               $show_description, $show_rating, $limit, $show_updated);
 }
 
-function bool_from_yn($yn) {
-    if ($yn == 'Y') return 1;
-    return 0;
-}
-
 /** function wp_get_linksbyname()
  ** Gets the links associated with the named category.
  ** Parameters:
@@ -51,26 +46,13 @@
 function wp_get_linksbyname($category, $args = '') {
 	global $wpdb;
 
-	$cat = $wpdb->get_row("SELECT cat_id, cat_name, auto_toggle, show_images, show_description, "
-												. " show_rating, show_updated, sort_order, sort_desc, text_before_link, text_after_link, "
-												. " text_after_all, list_limit FROM $wpdb->linkcategories WHERE cat_name='$category'");
+	$cat_id = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$category' LIMIT 1");
 
-	if (! $cat) {
+	if (! $cat_id)
 		return;
-	}
 
-	if (empty($args)) {
-		if ($cat->sort_desc == 'Y') {
-			$cat->sort_order = '_'.$cat->sort_order;
-		}
-		get_links($cat->cat_id, $cat->text_before_link, $cat->text_after_all,
-							$cat->text_after_link, bool_from_yn($cat->show_images), $cat->sort_order,
-							bool_from_yn($cat->show_description), bool_from_yn($cat->show_rating),
-							$cat->list_limit, bool_from_yn($cat->show_updated));
-	} else {
-		$args = add_query_arg('category', $cat->cat_id, $args);
-		wp_get_links($args);
-	}
+	$args = add_query_arg('category', $cat_id, $args);
+	wp_get_links($args);
 } // end wp_get_linksbyname
 
 /** function wp_get_links()
@@ -83,38 +65,29 @@
 function wp_get_links($args = '') {
 	global $wpdb;
 
-	if (!empty($args) && false === strpos($args, '=')) {
-		// If args is not a query string, it's a category id.
-		$category = $args;
-		$cat = $wpdb->get_row("SELECT cat_id, cat_name, auto_toggle, show_images, show_description, "
-													. " show_rating, show_updated, sort_order, sort_desc, text_before_link, text_after_link, "
-													. " text_after_all, list_limit FROM $wpdb->linkcategories WHERE cat_id=$category");
-		if ($cat) {
-			if ($cat->sort_desc == 'Y') {
-				$cat->sort_order = '_'.$cat->sort_order;
-			}
-			get_links($cat->cat_id, $cat->text_before_link, $cat->text_after_all,
-								$cat->text_after_link, bool_from_yn($cat->show_images), $cat->sort_order,
-								bool_from_yn($cat->show_description), bool_from_yn($cat->show_rating),
-								$cat->list_limit, bool_from_yn($cat->show_updated));
-		}
-	} else {
-		parse_str($args);
+	if ( empty($args) )
+		return;
 
-		if (! isset($category))	$category = -1;
-		if (! isset($before)) $before = '';
-		if (! isset($after)) $after = '<br />';
-		if (! isset($between))	$between = ' ';
-		if (! isset($show_images)) $show_images = true;
-		if (! isset($orderby)) $orderby = 'name';
-		if (! isset($show_description)) $show_description = true;
-		if (! isset($show_rating)) $show_rating = false;
-		if (! isset($limit)) $limit = -1;
-		if (! isset($show_updated)) $show_updated = 1;
-		if (! isset($echo)) $echo = true;
-
-		return get_links($category, $before, $after, $between, $show_images, $orderby, $show_description, $show_rating, $limit, $show_updated, $echo);
+	if ( false === strpos($args, '=') ) {
+		$cat_id = $args;
+		$args = add_query_arg('category', $cat_id, $args);
 	}
+
+	parse_str($args);
+
+	if (! isset($category))	$category = -1;
+	if (! isset($before)) $before = '';
+	if (! isset($after)) $after = '<br />';
+	if (! isset($between))	$between = ' ';
+	if (! isset($show_images)) $show_images = true;
+	if (! isset($orderby)) $orderby = 'name';
+	if (! isset($show_description)) $show_description = true;
+	if (! isset($show_rating)) $show_rating = false;
+	if (! isset($limit)) $limit = -1;
+	if (! isset($show_updated)) $show_updated = 1;
+	if (! isset($echo)) $echo = true;
+
+	return get_links($category, $before, $after, $between, $show_images, $orderby, $show_description, $show_rating, $limit, $show_updated, $echo);
 } // end wp_get_links
 
 /** function get_links()
@@ -154,53 +127,13 @@
 
 	global $wpdb;
 
-	$direction = ' ASC';
-	$category_query = '';
-	if ($category != -1) {
-		$category_query = " AND link_category = $category ";
-	}
-	if (get_settings('links_recently_updated_time')) {
-		$recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL " . get_settings('links_recently_updated_time') . " MINUTE) >= NOW(), 1,0) as recently_updated ";
-	} else {
-		$recently_updated_test = '';
-	}
-	if ($show_updated) {
-		$get_updated = ", UNIX_TIMESTAMP(link_updated) AS link_updated_f ";
-	}
+	$results = get_linkz("orderby=$orderby&show_updated=$show_updated&limit=$limit");
 
-	$orderby = strtolower($orderby);
-	if ($orderby == '')
-		$orderby = 'id';
-	if (substr($orderby, 0, 1) == '_') {
-		$direction = ' DESC';
-		$orderby = substr($orderby, 1);
-	}
-
-	switch($orderby) {
-		case 'length':
-		$length = ", CHAR_LENGTH(link_name) AS length";
-		break;
-		case 'rand':
-			$orderby = 'rand()';
-			break;
-		default:
-			$orderby = " link_" . $orderby;
-	}
-
-	if (!isset($length)) {
-		$length = '';
-	}
-
-	$sql = "SELECT link_url, link_name, link_image, link_target, link_description, link_rating, link_rel $length $recently_updated_test $get_updated FROM $wpdb->links WHERE link_visible = 'Y' " . $category_query;
-	$sql .= ' ORDER BY ' . $orderby . $direction;
-	/* The next 2 lines implement LIMIT TO processing */
-	if ($limit != -1)
-		$sql .= " LIMIT $limit";
-	$results = $wpdb->get_results($sql);
 	if (!$results) {
 		return;
 	}
 
+
 	$output = '';
 
 	foreach ($results as $row) {
@@ -290,10 +223,12 @@
  **   echo '<li>'.$link->link_name.'</li>';
  ** }
  **/
+// Deprecate in favor of get_linkz().
 function get_linkobjectsbyname($cat_name = "noname" , $orderby = 'name', $limit = -1) {
     global $wpdb;
     $cat_id = -1;
-    $results = $wpdb->get_results("SELECT cat_id FROM $wpdb->linkcategories WHERE cat_name='$cat_name'");
+    //$results = $wpdb->get_results("SELECT cat_id FROM $wpdb->linkcategories WHERE cat_name='$cat_name'");
+    // TODO: Fix me.
     if ($results) {
         foreach ($results as $result) {
             $cat_id = $result->cat_id;
@@ -337,6 +272,7 @@
  ** link_rel
  ** link_notes
  **/
+// Deprecate in favor of get_linkz().
 function get_linkobjects($category = -1, $orderby = 'name', $limit = -1) {
     global $wpdb;
 
@@ -445,12 +381,18 @@
  **                uses 0
  */
 function get_linkcatname($id = 0) {
-    global $wpdb;
-    $cat_name = '';
-    if ('' != $id) {
-        $cat_name = $wpdb->get_var("SELECT cat_name FROM $wpdb->linkcategories WHERE cat_id=$id");
-    }
-    return $cat_name;
+    if ( empty($id) )
+    	return '';
+  
+	$cats = wp_get_link_cats($id);
+
+	if ( empty($cats) || ! is_array($cats) )
+		return '';
+
+	$cat_id = $cats[0]; // Take the first cat.
+
+	$cat = get_category($cat_id);
+	return $cat->cat_name;
 }
 
 /** function get_get_autotoggle()
@@ -459,11 +401,7 @@
  **                uses 0
  */
 function get_autotoggle($id = 0) {
-    global $wpdb;
-    $auto_toggle = $wpdb->get_var("SELECT auto_toggle FROM $wpdb->linkcategories WHERE cat_id=$id");
-    if ('' == $auto_toggle)
-        $auto_toggle = 'N';
-    return $auto_toggle;
+	return 0;  
 }
 
 /** function links_popup_script()
@@ -511,50 +449,29 @@
  *   hide_if_empty (default true)  - Supress listing empty link categories
  */
 function get_links_list($order = 'name', $hide_if_empty = 'obsolete') {
-	global $wpdb;
-
 	$order = strtolower($order);
 
 	// Handle link category sorting
+	$direction = 'ASC';
 	if (substr($order,0,1) == '_') {
-		$direction = ' DESC';
+		$direction = 'DESC';
 		$order = substr($order,1);
 	}
 
-	// if 'name' wasn't specified, assume 'id':
-	$cat_order = ('name' == $order) ? 'cat_name' : 'cat_id';
-
 	if (!isset($direction)) $direction = '';
-	// Fetch the link category data as an array of hashesa
-	$cats = $wpdb->get_results("
-		SELECT DISTINCT link_category, cat_name, show_images, 
-			show_description, show_rating, show_updated, sort_order, 
-			sort_desc, list_limit
-		FROM `$wpdb->links` 
-		LEFT JOIN `$wpdb->linkcategories` ON (link_category = cat_id)
-		WHERE link_visible =  'Y'
-			AND list_limit <> 0
-		ORDER BY $cat_order $direction ", ARRAY_A);
 
+	$cats = get_categories("type=link&orderby=$order&order=$direction");
+
 	// Display each category
 	if ($cats) {
 		foreach ($cats as $cat) {
 			// Handle each category.
-			// First, fix the sort_order info
-			$orderby = $cat['sort_order'];
-			$orderby = (bool_from_yn($cat['sort_desc'])?'_':'') . $orderby;
 
 			// Display the category name
-			echo '	<li id="linkcat-' . $cat['link_category'] . '"><h2>' . $cat['cat_name'] . "</h2>\n\t<ul>\n";
+			echo '	<li id="linkcat-' . $cat->cat_ID . '"><h2>' . $cat->cat_name . "</h2>\n\t<ul>\n";
 			// Call get_links() with all the appropriate params
-			get_links($cat['link_category'],
-				'<li>',"</li>","\n",
-				bool_from_yn($cat['show_images']),
-				$orderby,
-				bool_from_yn($cat['show_description']),
-				bool_from_yn($cat['show_rating']),
-				$cat['list_limit'],
-				bool_from_yn($cat['show_updated']));
+			get_links($cat->cat_ID,
+				'<li>',"</li>","\n");
 
 			// Close the last category
 			echo "\n\t</ul>\n</li>\n";
@@ -562,4 +479,83 @@
 	}
 }
 
+function get_linkz($args = '') {
+	global $wpdb;
+
+	parse_str($args, $r);
+
+	if ( !isset($r['orderby']) )
+		$r['orderby'] = 'name';
+	if ( !isset($r['order']) )
+		$r['order'] = 'ASC';
+	if ( !isset($r['limit']) )
+		$r['limit'] = -1;
+	if ( !isset($r['category']) )
+		$r['category'] = -1;
+	if ( !isset($r['category_name']) )
+		$r['category_name'] = '';
+	if ( !isset($r['hide_invisible']) )
+		$r['hide_invisible'] = 1;
+	if ( !isset($r['show_updates']) )
+		$r['show_updated'] = 0;
+
+	$exclusions = '';
+	if ( !empty($r['exclude']) ) {
+		$exlinks = preg_split('/[\s,]+/',$r['exclude']);
+		if ( count($exlinks) ) {
+			foreach ( $exlinks as $exlink ) {
+				$exclusions .= ' AND link_id <> ' . intval($exlink) . ' ';
+			}
+		}
+	}
+
+	extract($r);
+
+	if ( ! empty($category_name) ) {
+		if ( $cat_id = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$category_name' LIMIT 1") )
+			$category = $cat_id;
+	}
+
+	$category_query = '';
+	$join = '';
+	if ( $category != -1 && !empty($category) ) {
+		$join = " LEFT JOIN $wpdb->link2cat ON ($wpdb->links.link_id = $wpdb->link2cat.link_id) ";
+
+      	$category_query = " AND category_id = $category ";
+	}
+
+	if (get_settings('links_recently_updated_time')) {
+		$recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL " . get_settings('links_recently_updated_time') . " MINUTE) >= NOW(), 1,0) as recently_updated ";
+	} else {
+		$recently_updated_test = '';
+	}
+
+	if ($show_updated) {
+		$get_updated = ", UNIX_TIMESTAMP(link_updated) AS link_updated_f ";
+	}
+
+	$orderby = strtolower($r['orderby']);
+	$length = '';
+	switch ($orderby) {
+		case 'length':
+			$length = ", CHAR_LENGTH(link_name) AS length";
+			break;
+		case 'rand':
+			$orderby = 'rand()';
+			break;
+		default:
+			$orderby = "link_" . $orderby;
+	}
+
+	$visible = '';
+	if ( $hide_invisible )
+		$visible = "AND link_visible = 'Y'";
+
+	$query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
+	$query .= " ORDER BY $orderby $order";
+	if ($limit != -1)
+		$query .= " LIMIT $limit";
+
+	return $wpdb->get_results($query);
+}
 ?>
\ No newline at end of file
Index: wp-includes/version.php
===================================================================
--- wp-includes/version.php	(revision 3568)
+++ wp-includes/version.php	(working copy)
@@ -3,6 +3,6 @@
 // This just holds the version number, in a separate file so we can bump it without cluttering the SVN
 
 $wp_version = '2.1-alpha1';
-$wp_db_version = 3548;
+$wp_db_version = 3569;
 
 ?>
\ No newline at end of file
Index: wp-includes/functions.php
===================================================================
--- wp-includes/functions.php	(revision 3568)
+++ wp-includes/functions.php	(working copy)
@@ -2409,4 +2409,9 @@
 	else
 		return '';
 }
+
+function bool_from_yn($yn) {
+    if ($yn == 'Y') return 1;
+    return 0;
+}
 ?>
Index: wp-settings.php
===================================================================
--- wp-settings.php	(revision 3568)
+++ wp-settings.php	(working copy)
@@ -78,6 +78,7 @@
 $wpdb->categories       = $table_prefix . 'categories';
 $wpdb->post2cat         = $table_prefix . 'post2cat';
 $wpdb->comments         = $table_prefix . 'comments';
+$wpdb->link2cat         = $table_prefix . 'link2cat';
 $wpdb->links            = $table_prefix . 'links';
 $wpdb->linkcategories   = $table_prefix . 'linkcategories';
 $wpdb->options          = $table_prefix . 'options';
@@ -98,6 +99,7 @@
 $tablecategories = $wpdb->categories;
 $tablepost2cat = $wpdb->post2cat;
 $tablecomments = $wpdb->comments;
+$tablelink2cat = $wpdb->link2cat;
 $tablelinks = $wpdb->links;
 $tablelinkcategories = $wpdb->linkcategories;
 $tableoptions = $wpdb->options;
Index: wp-admin/upgrade-functions.php
===================================================================
--- wp-admin/upgrade-functions.php	(revision 3568)
+++ wp-admin/upgrade-functions.php	(working copy)
@@ -1,6 +1,7 @@
 <?php
 
 require_once(ABSPATH . '/wp-admin/admin-functions.php');
+require_once(ABSPATH . '/wp-admin/admin-db.php');
 require_once(ABSPATH . '/wp-admin/upgrade-schema.php');
 // Functions to be called in install and upgrade scripts
 function upgrade_all() {
@@ -33,7 +34,7 @@
 	if ( $wp_current_db_version < 3308 )
 		upgrade_160();
 
-	if ( $wp_current_db_version < 3548 )
+	if ( $wp_current_db_version < 3569 )
 		upgrade_210();
 
 	$wp_rewrite->flush_rules();
@@ -366,6 +367,43 @@
 			foreach ( $posts as $post )
 				wp_schedule_event(mysql2date('U', $post->post_date), 'once', 'publish_future_post', $post->ID);
 	}
+	if ( $wp_current_db_version < 3569 ) {
+		// Create categories for link categories if a category with the same
+		// name doesn't exist.  Create a map of link cat IDs to cat IDs.
+		$link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM $wpdb->linkcategories");	
+		foreach ( $link_cats as $link_cat) {
+			if ( $cat_id = category_exists($link_cat->cat_name) ) {
+				$link_cat_id_map[$link_cat->cat_id] = $cat_id;
+				$default_link_cat = $cat_id;
+			} else {
+				$link_cat_id_map[$link_cat->cat_id] = wp_create_category($link_cat->cat_name);
+				$default_link_cat = $link_cat_id_map[$link_cat->cat_id];
+			}
+		}
+
+		// Associate links to cats.
+		$links = $wpdb->get_results("SELECT link_id, link_category FROM $wpdb->links");
+		foreach ( $links as $link ) {
+			$link_cat = $link_cat_id_map[$link->link_category];
+			$cat = $wpdb->get_row("SELECT * FROM $wpdb->link2cat WHERE link_id = $link->link_id AND category_id = $link_cat");
+			if (!$cat && 0 != $link->link_category) {
+				$wpdb->query("INSERT INTO $wpdb->link2cat (link_id, category_id)
+					VALUES ('$link->link_id', '$link_cat')");
+			}			
+		}
+		
+		// Set default to the last category we grabbed during the upgrade loop.
+		update_option('default_link_category', $default_link_cat);
+
+		// Count links per category.
+		if ( 0 == $wpdb->get_var("SELECT SUM(link_count) FROM $wpdb->categories") ) {
+			$categories = $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories");
+			foreach ( $categories as $cat_id ) {
+				$count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->link2cat, $wpdb->links WHERE $wpdb->links.link_id = $wpdb->link2cat.link_id AND category_id = '$cat_id'");
+				$wpdb->query("UPDATE $wpdb->categories SET link_count = '$count' WHERE cat_ID = '$cat_id'");
+			}
+		}
+	}
 }
 
 // The functions we use to actually do stuff
Index: wp-admin/admin-functions.php
===================================================================
--- wp-admin/admin-functions.php	(revision 3568)
+++ wp-admin/admin-functions.php	(working copy)
@@ -476,6 +476,7 @@
 	$link->link_description = wp_specialchars($link->link_description);
 	$link->link_notes = wp_specialchars($link->link_notes);
 	$link->link_rss = wp_specialchars($link->link_rss);
+	$link->post_category = $link->link_category;
 
 	return $link;
 }
@@ -507,15 +508,7 @@
 	$_POST['link_name'] = wp_specialchars($_POST['link_name']);
 	$_POST['link_image'] = wp_specialchars($_POST['link_image']);
 	$_POST['link_rss'] = wp_specialchars($_POST['link_rss']);
-	$auto_toggle = get_autotoggle($_POST['link_category']);
 
-	// if we are in an auto toggle category and this one is visible then we
-	// need to make the others invisible before we add this new one.
-	// FIXME Add category toggle func.
-	//if (($auto_toggle == 'Y') && ($link_visible == 'Y')) {
-	//	$wpdb->query("UPDATE $wpdb->links set link_visible = 'N' WHERE link_category = $link_category");
-	//}
-
 	if ( !empty($link_id) ) {
 		$_POST['link_id'] = $link_id;
 		return wp_update_link($_POST);
@@ -643,6 +636,44 @@
 	}
 }
 
+// Dandy new recursive multiple category stuff.
+function link_cat_rows($parent = 0, $level = 0, $categories = 0) {
+	global $wpdb, $class;
+
+	if (!$categories)
+		$categories = $wpdb->get_results("SELECT * FROM $wpdb->categories ORDER BY cat_name");
+
+	if ($categories) {
+		foreach ($categories as $category) {
+			if ($category->category_parent == $parent) {
+				$category->cat_name = wp_specialchars($category->cat_name);
+				$pad = str_repeat('&#8212; ', $level);
+				if ( current_user_can('manage_categories') ) {
+					$edit = "<a href='link-categories.php?action=edit&amp;cat_ID=$category->cat_ID' class='edit'>".__('Edit')."</a></td>";
+					$default_cat_id = get_option('default_link_category');
+
+					if ($category->cat_ID != $default_cat_id)
+						$edit .= "<td><a href='link-categories.php?action=delete&amp;cat_ID=$category->cat_ID' onclick=\"return deleteSomething( 'cat', $category->cat_ID, '".sprintf(__("You are about to delete the category &quot;%s&quot;.  All of its posts will go to the default category.\\n&quot;OK&quot; to delete, &quot;Cancel&quot; to stop."), wp_specialchars($category->cat_name, 1))."' );\" class='delete'>".__('Delete')."</a>";
+					else
+						$edit .= "<td style='text-align:center'>".__("Default");
+				}
+				else
+					$edit = '';
+
+				$class = ('alternate' == $class) ? '' : 'alternate';
+				echo "<tr id='cat-$category->cat_ID' class='$class'><th scope='row'>$category->cat_ID</th><td>$pad $category->cat_name</td>
+								<td>$category->category_description</td>
+								<td>$category->link_count</td>
+								<td>$edit</td>
+								</tr>";
+				link_cat_rows($category->cat_ID, $level +1, $categories);
+			}
+		}
+	} else {
+		return false;
+	}
+}
+
 function page_rows($parent = 0, $level = 0, $pages = 0, $hierarchy = true) {
 	global $wpdb, $class, $post;
 
@@ -702,23 +733,63 @@
 	}
 }
 
-function link_category_dropdown($fieldname, $selected = 0) {
+function return_link_categories_list($parent = 0) {
 	global $wpdb;
+	return $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent ORDER BY link_count DESC LIMIT 100");
+}
 
-	$results = $wpdb->get_results("SELECT cat_id, cat_name, auto_toggle FROM $wpdb->linkcategories ORDER BY cat_id");
-	echo "\n<select name='$fieldname' size='1'>\n";
-	foreach ($results as $row) {
-		echo "\n\t<option value='$row->cat_id'";
-		if ($row->cat_id == $selected)
-			echo " selected='selected'";
-		echo ">$row->cat_id : " . wp_specialchars($row->cat_name);
-		if ($row->auto_toggle == 'Y')
-			echo ' (auto toggle)';
-		echo "</option>";
+function get_nested_link_categories($default = 0, $parent = 0) {
+	global $link_id, $mode, $wpdb;
+
+	if ($link_id) {
+		$checked_categories = $wpdb->get_col("
+		     SELECT category_id
+		     FROM $wpdb->categories, $wpdb->link2cat
+		     WHERE $wpdb->link2cat.category_id = cat_ID AND $wpdb->link2cat.link_id = '$link_id'
+		     ");
+
+		if (count($checked_categories) == 0) {
+			// No selected categories, strange
+			$checked_categories[] = $default;
+		}
+
+	} else {
+		$checked_categories[] = $default;
 	}
-	echo "\n</select>\n";
+
+	$cats = return_link_categories_list($parent);
+	$result = array ();
+
+	if (is_array($cats)) {
+		foreach ($cats as $cat) {
+			$result[$cat]['children'] = get_nested_link_categories($default, $cat);
+			$result[$cat]['cat_ID'] = $cat;
+			$result[$cat]['checked'] = in_array($cat, $checked_categories);
+			$result[$cat]['cat_name'] = get_the_category_by_ID($cat);
+		}
+	}
+
+	usort($result, 'sort_cats');
+
+	return $result;
 }
 
+function write_nested_link_categories($categories) {
+	foreach ($categories as $category) {
+		echo '<label for="category-', $category['cat_ID'], '" class="selectit"><input value="', $category['cat_ID'], '" type="checkbox" name="link_category[]" id="category-', $category['cat_ID'], '"', ($category['checked'] ? ' checked="checked"' : ""), '/> ', wp_specialchars($category['cat_name']), "</label>\n";
+
+		if (isset ($category['children'])) {
+			echo "\n<span class='cat-nest'>\n";
+			write_nested_link_categories($category['children']);
+			echo "</span>\n";
+		}
+	}
+}
+
+function dropdown_link_categories($default = 0) {
+	write_nested_link_categories(get_nested_link_categories($default));
+}
+
 function wp_create_thumbnail($file, $max_side, $effect = '') {
 
 		// 1 = GIF, 2 = JPEG, 3 = PNG
Index: wp-admin/admin-db.php
===================================================================
--- wp-admin/admin-db.php	(revision 3568)
+++ wp-admin/admin-db.php	(working copy)
@@ -105,11 +105,14 @@
 	if (empty ($category_parent))
 		$category_parent = 0;
 
+	if ( ! isset($links_visible) )
+		$links_visible = 1;
+
 	if (!$update) {
-		$wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_description, category_parent) VALUES ('0', '$cat_name', '$category_nicename', '$category_description', '$category_parent')");
+		$wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_description, category_parent, links_visible) VALUES ('0', '$cat_name', '$category_nicename', '$category_description', '$category_parent', '$links_visible')");
 		$cat_ID = $wpdb->insert_id;
 	} else {
-		$wpdb->query ("UPDATE $wpdb->categories SET cat_name = '$cat_name', category_nicename = '$category_nicename', category_description = '$category_description', category_parent = '$category_parent' WHERE cat_ID = '$cat_ID'");
+		$wpdb->query ("UPDATE $wpdb->categories SET cat_name = '$cat_name', category_nicename = '$category_nicename', category_description = '$category_description', category_parent = '$category_parent', links_visible = '$links_visible' WHERE cat_ID = '$cat_ID'");
 	}
 
 	if ( $category_nicename == '' ) {
@@ -168,6 +171,7 @@
 
 	// TODO: Only set categories to general if they're not in another category already
 	$wpdb->query("UPDATE $wpdb->post2cat SET category_id='1' WHERE category_id='$cat_ID'");
+	$wpdb->query("UPDATE $wpdb->link2cat SET category_id='1' WHERE category_id='$cat_ID'");
 
 	wp_cache_delete($cat_ID, 'category');
 	wp_cache_delete('all_category_ids', 'category');
@@ -244,6 +248,7 @@
 	global $wpdb;
 
 	$link = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = '$link_id'");
+	$link->link_category = wp_get_link_cats($link_id);
 
 	if ( $output == OBJECT ) {
 		return $link;
@@ -280,19 +285,26 @@
 	if ( empty($link_notes) )
 		$link_notes = '';
 
+	// Make sure we set a valid category
+	if (0 == count($link_category) || !is_array($link_category)) {
+		$link_category = array(get_option('default_category'));
+	}
+
 	if ( $update ) {
 		$wpdb->query("UPDATE $wpdb->links SET link_url='$link_url',
 			link_name='$link_name', link_image='$link_image',
-			link_target='$link_target', link_category='$link_category',
+			link_target='$link_target',
 			link_visible='$link_visible', link_description='$link_description',
 			link_rating='$link_rating', link_rel='$link_rel',
 			link_notes='$link_notes', link_rss = '$link_rss'
 			WHERE link_id='$link_id'");
 	} else {
-		$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_image, link_target, link_category, link_description, link_visible, link_owner, link_rating, link_rel, link_notes, link_rss) VALUES('$link_url','$link_name', '$link_image', '$link_target', '$link_category', '$link_description', '$link_visible', '$link_owner', '$link_rating', '$link_rel', '$link_notes', '$link_rss')");
+		$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_image, link_target, link_description, link_visible, link_owner, link_rating, link_rel, link_notes, link_rss) VALUES('$link_url','$link_name', '$link_image', '$link_target', '$link_description', '$link_visible', '$link_owner', '$link_rating', '$link_rel', '$link_notes', '$link_rss')");
 		$link_id = $wpdb->insert_id;
 	}
 
+	wp_set_link_cats($link_id, $link_category);
+
 	if ( $update )
 		do_action('edit_link', $link_id);
 	else
@@ -311,8 +323,16 @@
 	// Escape data pulled from DB.
 	$link = add_magic_quotes($link);
 
+	// Passed link category list overwrites existing category list if not empty.
+ 	if ( isset($linkdata['link_category']) && is_array($linkdata['link_category'])
+			 && 0 != count($linkdata['link_category']) )
+ 		$link_cats = $linkdata['link_category'];
+ 	else 
+ 		$link_cats = $link['link_category'];
+
 	// Merge old and new fields with new fields overwriting old ones.
 	$linkdata = array_merge($link, $linkdata);
+ 	$linkdata['link_category'] = $link_cats;
 
 	return wp_insert_link($linkdata);
 }
@@ -321,9 +341,88 @@
 	global $wpdb;
 
 	do_action('delete_link', $link_id);
+	
+	$categories = wp_get_link_cats('', $link_id);
+	if( is_array( $categories ) ) {
+		foreach ( $categories as $cat_id ) {
+			$wpdb->query("UPDATE $wpdb->categories SET link_count = link_count - 1 WHERE cat_ID = '$cat_id'");
+			wp_cache_delete($cat_id, 'category');
+		}
+	}
+
+	$wpdb->query("DELETE FROM $wpdb->link2cat WHERE link_id = $link_id");
 	return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'");
 }
 
+function wp_get_link_cats($link_ID = 0) {
+	global $wpdb;
+
+	$sql = "SELECT category_id 
+		FROM $wpdb->link2cat 
+		WHERE link_id = $link_ID 
+		ORDER BY category_id";
+
+	$result = $wpdb->get_col($sql);
+
+	if ( !$result )
+		$result = array();
+
+	return array_unique($result);
+}
+
+function wp_set_link_cats($link_ID = 0, $link_categories = array()) {
+	global $wpdb;
+	// If $link_categories isn't already an array, make it one:
+	if (!is_array($link_categories) || 0 == count($link_categories))
+		$link_categories = array(get_option('default_category'));
+
+	$link_categories = array_unique($link_categories);
+
+	// First the old categories
+	$old_categories = $wpdb->get_col("
+		SELECT category_id 
+		FROM $wpdb->link2cat 
+		WHERE link_id = $link_ID");
+
+	if (!$old_categories) {
+		$old_categories = array();
+	} else {
+		$old_categories = array_unique($old_categories);
+	}
+
+	// Delete any?
+	$delete_cats = array_diff($old_categories,$link_categories);
+
+	if ($delete_cats) {
+		foreach ($delete_cats as $del) {
+			$wpdb->query("
+				DELETE FROM $wpdb->link2cat 
+				WHERE category_id = $del 
+					AND link_id = $link_ID 
+				");
+		}
+	}
+
+	// Add any?
+	$add_cats = array_diff($link_categories, $old_categories);
+
+	if ($add_cats) {
+		foreach ($add_cats as $new_cat) {
+			$wpdb->query("
+				INSERT INTO $wpdb->link2cat (link_id, category_id) 
+				VALUES ($link_ID, $new_cat)");
+		}
+	}
+	
+	// Update category counts.
+	$all_affected_cats = array_unique(array_merge($link_categories, $old_categories));
+	foreach ( $all_affected_cats as $cat_id ) {
+		$count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->link2cat, $wpdb->links WHERE $wpdb->links.link_id = $wpdb->link2cat.link_id AND category_id = '$cat_id'");
+		$wpdb->query("UPDATE $wpdb->categories SET link_count = '$count' WHERE cat_ID = '$cat_id'");
+		wp_cache_delete($cat_id, 'category');
+	}
+}	// wp_set_link_cats()
+
 function post_exists($title, $content = '', $post_date = '') {
 	global $wpdb;
 
Index: wp-admin/edit-link-form.php
===================================================================
--- wp-admin/edit-link-form.php	(revision 3568)
+++ wp-admin/edit-link-form.php	(working copy)
@@ -1,14 +1,12 @@
 <?php
 if ( ! empty($link_id) ) {
-	$editing = true;
-	$heading = __('Edit a link:');
+	$heading = __('Edit Link');
 	$submit_text = __('Save Changes &raquo;');
-	$form = '<form action="" method="post" name="editlink" id="editlink">'; 
+	$form = '<form name="editlink" id="editlink" method="post" action="link.php">'; 
 } else {
-	$editing = false;
-	$heading = __('<strong>Add</strong> a link:');
+	$heading = __('Create Link');
 	$submit_text = __('Add Link &raquo;');
-	$form = '<form name="addlink" method="post" action="link-manager.php">';
+	$form = '<form name="addlink" id="addlink" method="post" action="link.php">';
 }
 
 function xfn_check($class, $value = '', $type = 'check') {
@@ -28,209 +26,228 @@
 		if ('identity' == $class && in_array('me', $rels) ) echo ' checked="checked"';
 	}
 }
-
 ?>
 
 <div class="wrap"> 
-  <?php echo $form ?>
-  <h2><?php echo $heading ?></h2>
-<fieldset class="options">
-    <legend><?php _e('Basics') ?></legend>
-        <table class="editform" width="100%" cellspacing="2" cellpadding="5">
-         <tr>
-           <th width="33%" scope="row"><?php _e('URI:') ?></th>
-           <td width="67%"><input type="text" name="link_url" value="<?php echo $link->link_url; ?>" style="width: 95%;" /></td>
-         </tr>
-         <tr>
-           <th scope="row"><?php _e('Link Name:') ?></th>
-           <td><input type="text" name="link_name" value="<?php echo $link->link_name; ?>" style="width: 95%" /></td>
-         </tr>
-         <tr>
-            <th scope="row"><?php _e('Short description:') ?></th>
-         	<td><input type="text" name="link_description" value="<?php echo $link->link_description; ?>" style="width: 95%" /></td>
-         	</tr>
-        <tr>
-           <th scope="row"><?php _e('Category:') ?></th>
-           <td><?php link_category_dropdown('link_category', $link->link_category); ?></td>
-         </tr>
-</table>
+<h2><?php echo $heading ?></h2>
+<?php echo $form ?>
+ 
+<div id="poststuff">
+<div id="moremeta">
+<div id="grabit" class="dbx-group">
+
+<fieldset id="categorydiv" class="dbx-box">
+<h3 class="dbx-handle"><?php _e('Categories') ?></h3>
+<div class="dbx-content">
+<p id="jaxcat"></p>
+<div id="categorychecklist"><?php dropdown_link_categories(get_settings('default_link_category')); ?></div></div>
 </fieldset>
-       <p class="submit">
-       <input type="submit" name="submit" value="<?php echo $submit_text ?>" />
-       </p>
-	<fieldset class="options">
-        <legend><?php _e('Link Relationship (XFN)') ?></legend>
-        <table class="editform" width="100%" cellspacing="2" cellpadding="5">
-            <tr>
-                <th width="33%" scope="row"><?php _e('rel:') ?></th>
-            	<td width="67%"><input type="text" name="link_rel" id="link_rel" size="50" value="<?php echo $link->link_rel; ?>" /></td>
-           	</tr>
-            <tr>
-                <th scope="row"><?php _e('<a href="http://gmpg.org/xfn/">XFN</a> Creator:') ?></th>
-            	<td>
-					<table cellpadding="3" cellspacing="5">
-	          <tr>
-              <th scope="row"> <?php _e('identity') ?> </th>
-              <td>
-                <label for="me">
-                <input type="checkbox" name="identity" value="me" id="me" <?php xfn_check('identity', 'me'); ?> />
-          <?php _e('another web address of mine') ?></label>
-              </td>
-            </tr>
-            <tr>
-              <th scope="row"> <?php _e('friendship') ?> </th>
-              <td>
-			    <label for="contact">
-                <input class="valinp" type="radio" name="friendship" value="contact" id="contact" <?php xfn_check('friendship', 'contact', 'radio'); ?> /> <?php _e('contact') ?></label>
-                <label for="acquaintance">
-                <input class="valinp" type="radio" name="friendship" value="acquaintance" id="acquaintance" <?php xfn_check('friendship', 'acquaintance', 'radio'); ?> />  <?php _e('acquaintance') ?></label>
-                <label id="friend">
-                <input class="valinp" type="radio" name="friendship" value="friend" id="friend" <?php xfn_check('friendship', 'friend', 'radio'); ?> /> <?php _e('friend') ?></label>
-                <label for="friendship">
-                <input name="friendship" type="radio" class="valinp" value="" id="friendship" <?php xfn_check('friendship', '', 'radio'); ?> /> <?php _e('none') ?></label>
-              </td>
-            </tr>
-            <tr>
-              <th scope="row"> <?php _e('physical') ?> </th>
-              <td>
-                <label for="met">
-                <input class="valinp" type="checkbox" name="physical" value="met" id="met" <?php xfn_check('physical', 'met'); ?> />
-          <?php _e('met') ?></label>
-              </td>
-            </tr>
-            <tr>
-              <th scope="row"> <?php _e('professional') ?> </th>
-              <td>
-                <label for="co-worker">
-                <input class="valinp" type="checkbox" name="professional" value="co-worker" id="co-worker" <?php xfn_check('professional', 'co-worker'); ?> />
-          <?php _e('co-worker') ?></label>
-                <label for="colleague">
-                <input class="valinp" type="checkbox" name="professional" value="colleague" id="colleague" <?php xfn_check('professional', 'colleague'); ?> />
-          <?php _e('colleague') ?></label>
-              </td>
-            </tr>
-            <tr>
-              <th scope="row"> <?php _e('geographical') ?> </th>
-              <td>
-                <label for="co-resident">
-                <input class="valinp" type="radio" name="geographical" value="co-resident" id="co-resident" <?php xfn_check('geographical', 'co-resident', 'radio'); ?> />
-          <?php _e('co-resident') ?></label>
-                <label for="neighbor">
-                <input class="valinp" type="radio" name="geographical" value="neighbor" id="neighbor" <?php xfn_check('geographical', 'neighbor', 'radio'); ?> />
-          <?php _e('neighbor') ?></label>
-                <label for="geographical">
-                <input class="valinp" type="radio" name="geographical" value="" id="geographical" <?php xfn_check('geographical', '', 'radio'); ?> />
-          <?php _e('none') ?></label>
-              </td>
-            </tr>
-            <tr>
-              <th scope="row"> <?php _e('family') ?> </th>
-              <td>
-                <label for="child">
-                <input class="valinp" type="radio" name="family" value="child" id="child" <?php xfn_check('family', 'child', 'radio'); ?>  />
-          <?php _e('child') ?></label>
-                <label for="kin">
-                <input class="valinp" type="radio" name="family" value="kin" id="kin" <?php xfn_check('family', 'kin', 'radio'); ?>  />
-          <?php _e('kin') ?></label>
-                <label for="parent">
-                <input class="valinp" type="radio" name="family" value="parent" id="parent" <?php xfn_check('family', 'parent', 'radio'); ?> />
-          <?php _e('parent') ?></label>
-                <label for="sibling">
-                <input class="valinp" type="radio" name="family" value="sibling" id="sibling" <?php xfn_check('family', 'sibling', 'radio'); ?> />
-          <?php _e('sibling') ?></label>
-                <label for="spouse">
-                <input class="valinp" type="radio" name="family" value="spouse" id="spouse" <?php xfn_check('family', 'spouse', 'radio'); ?> />
-          <?php _e('spouse') ?></label>
-                <label for="family">
-                <input class="valinp" type="radio" name="family" value="" id="family" <?php xfn_check('family', '', 'radio'); ?> />
-          <?php _e('none') ?></label>
-              </td>
-            </tr>
-            <tr>
-              <th scope="row"> <?php _e('romantic') ?> </th>
-              <td>
-                <label for="muse">
-                <input class="valinp" type="checkbox" name="romantic" value="muse" id="muse" <?php xfn_check('romantic', 'muse'); ?> />
-         <?php _e('muse') ?></label>
-                <label for="crush">
-                <input class="valinp" type="checkbox" name="romantic" value="crush" id="crush" <?php xfn_check('romantic', 'crush'); ?> />
-         <?php _e('crush') ?></label>
-                <label for="date">
-                <input class="valinp" type="checkbox" name="romantic" value="date" id="date" <?php xfn_check('romantic', 'date'); ?> />
-         <?php _e('date') ?></label>
-                <label for="romantic">
-                <input class="valinp" type="checkbox" name="romantic" value="sweetheart" id="romantic" <?php xfn_check('romantic', 'sweetheart'); ?> />
-         <?php _e('sweetheart') ?></label>
-              </td>
-            </tr>
-        </table>
-		  </td>
-           	</tr>
+
+<fieldset class="dbx-box">
+<h3 class="dbx-handle"><?php _e('Target') ?></h3> 
+<div class="dbx-content">
+<label for="link_target_blank" class="selectit">
+<input id="link_target_blank" type="radio" name="link_target" value="_blank" <?php echo(($link->link_target == '_blank') ? 'checked="checked"' : ''); ?> />
+<code>_blank</code></label>
+<label for="link_target_top" class="selectit">
+<input id="link_target_top" type="radio" name="link_target" value="_top" <?php echo(($link->link_target == '_top') ? 'checked="checked"' : ''); ?> />
+<code>_top</code></label>
+<label for="link_target_none" class="selectit">
+<input id="link_target_none" type="radio" name="link_target" value="" <?php echo(($link->link_target == '') ? 'checked="checked"' : ''); ?> />
+<?php _e('none') ?></label>
+</div>
+</fieldset>
+
+<fieldset class="dbx-box">
+<h3 class="dbx-handle"><?php _e('Visible') ?></h3> 
+<div class="dbx-content">
+<label for="link_visible_yes" class="selectit">
+<input id="link_visible_yes" type="radio" name="link_visible" <?php if ($link->link_visible == 'Y') echo "checked='checked'"; ?> value="Y" />
+<?php _e('Yes') ?></label>
+<label for="link_visible_no" class="selectit">
+<input id="link_visible_no" type="radio" name="link_visible" <?php if ($link->link_visible == 'N') echo "checked='checked'"; ?> value="N" />
+<?php _e('No') ?></label>
+</div>
+</fieldset>
+
+</div>
+</div>
+
+<fieldset id="uridiv">
+<legend><?php _e('URI:') ?></legend>
+<div><input type="text" name="link_url" value="<?php echo $link->link_url; ?>" style="width: 95%" /></div>
+</fieldset>
+
+<fieldset id="namediv">
+<legend><?php _e('Name:') ?></legend>
+<div><input type="text" name="link_name" value="<?php echo $link->link_name; ?>" style="width: 95%" /></div>
+</fieldset>
+
+<fieldset id="descdiv">
+<legend><?php _e('Description:') ?></legend>
+<div><input type="text" name="link_description" value="<?php echo $link->link_description; ?>" style="width: 95%" /></div>
+</fieldset>
+
+<p class="submit">
+<input type="submit" name="submit" value="<?php echo $submit_text ?>" />
+</p>
+
+<div id="advancedstuff" class="dbx-group" >
+
+<fieldset id="xfn" class="dbx-box">
+<h3 class="dbx-handle"><?php _e('Link Relationship (XFN)') ?></h3>
+<div class="dbx-content">
+<table class="editform" width="100%" cellspacing="2" cellpadding="5">
+	<tr>
+		<th width="33%" scope="row"><?php _e('rel:') ?></th>
+		<td width="67%"><input type="text" name="link_rel" id="link_rel" size="50" value="<?php echo $link->link_rel; ?>" /></td>
+	</tr>
+	<tr>
+		<th scope="row"><?php _e('<a href="http://gmpg.org/xfn/">XFN</a> Creator:') ?></th>
+		<td>
+			<table cellpadding="3" cellspacing="5">
+				<tr>
+					<th scope="row"> <?php _e('identity') ?> </th>
+					<td>
+						<label for="me">
+						<input type="checkbox" name="identity" value="me" id="me" <?php xfn_check('identity', 'me'); ?> />
+						<?php _e('another web address of mine') ?></label>
+					</td>
+				</tr>
+				<tr>
+					<th scope="row"> <?php _e('friendship') ?> </th>
+					<td>
+						<label for="contact">
+						<input class="valinp" type="radio" name="friendship" value="contact" id="contact" <?php xfn_check('friendship', 'contact', 'radio'); ?> /> <?php _e('contact') ?></label>
+						<label for="acquaintance">
+						<input class="valinp" type="radio" name="friendship" value="acquaintance" id="acquaintance" <?php xfn_check('friendship', 'acquaintance', 'radio'); ?> />  <?php _e('acquaintance') ?></label>
+						<label id="friend">
+						<input class="valinp" type="radio" name="friendship" value="friend" id="friend" <?php xfn_check('friendship', 'friend', 'radio'); ?> /> <?php _e('friend') ?></label>
+						<label for="friendship">
+						<input name="friendship" type="radio" class="valinp" value="" id="friendship" <?php xfn_check('friendship', '', 'radio'); ?> /> <?php _e('none') ?></label>
+					</td>
+				</tr>
+				<tr>
+					<th scope="row"> <?php _e('physical') ?> </th>
+					<td>
+						<label for="met">
+						<input class="valinp" type="checkbox" name="physical" value="met" id="met" <?php xfn_check('physical', 'met'); ?> />
+						<?php _e('met') ?></label>
+					</td>
+				</tr>
+				<tr>
+					<th scope="row"> <?php _e('professional') ?> </th>
+					<td>
+						<label for="co-worker">
+						<input class="valinp" type="checkbox" name="professional" value="co-worker" id="co-worker" <?php xfn_check('professional', 'co-worker'); ?> />
+						<?php _e('co-worker') ?></label>
+						<label for="colleague">
+						<input class="valinp" type="checkbox" name="professional" value="colleague" id="colleague" <?php xfn_check('professional', 'colleague'); ?> />
+						<?php _e('colleague') ?></label>
+					</td>
+				</tr>
+				<tr>
+					<th scope="row"> <?php _e('geographical') ?> </th>
+					<td>
+						<label for="co-resident">
+						<input class="valinp" type="radio" name="geographical" value="co-resident" id="co-resident" <?php xfn_check('geographical', 'co-resident', 'radio'); ?> />
+						<?php _e('co-resident') ?></label>
+						<label for="neighbor">
+						<input class="valinp" type="radio" name="geographical" value="neighbor" id="neighbor" <?php xfn_check('geographical', 'neighbor', 'radio'); ?> />
+						<?php _e('neighbor') ?></label>
+						<label for="geographical">
+						<input class="valinp" type="radio" name="geographical" value="" id="geographical" <?php xfn_check('geographical', '', 'radio'); ?> />
+						<?php _e('none') ?></label>
+					</td>
+				</tr>
+				<tr>
+					<th scope="row"> <?php _e('family') ?> </th>
+					<td>
+						<label for="child">
+						<input class="valinp" type="radio" name="family" value="child" id="child" <?php xfn_check('family', 'child', 'radio'); ?>  />
+						<?php _e('child') ?></label>
+						<label for="kin">
+						<input class="valinp" type="radio" name="family" value="kin" id="kin" <?php xfn_check('family', 'kin', 'radio'); ?>  />
+						<?php _e('kin') ?></label>
+						<label for="parent">
+						<input class="valinp" type="radio" name="family" value="parent" id="parent" <?php xfn_check('family', 'parent', 'radio'); ?> />
+						<?php _e('parent') ?></label>
+						<label for="sibling">
+						<input class="valinp" type="radio" name="family" value="sibling" id="sibling" <?php xfn_check('family', 'sibling', 'radio'); ?> />
+						<?php _e('sibling') ?></label>
+						<label for="spouse">
+						<input class="valinp" type="radio" name="family" value="spouse" id="spouse" <?php xfn_check('family', 'spouse', 'radio'); ?> />
+						<?php _e('spouse') ?></label>
+						<label for="family">
+						<input class="valinp" type="radio" name="family" value="" id="family" <?php xfn_check('family', '', 'radio'); ?> />
+						<?php _e('none') ?></label>
+					</td>
+				</tr>
+				<tr>
+					<th scope="row"> <?php _e('romantic') ?> </th>
+					<td>
+						<label for="muse">
+						<input class="valinp" type="checkbox" name="romantic" value="muse" id="muse" <?php xfn_check('romantic', 'muse'); ?> />
+						<?php _e('muse') ?></label>
+						<label for="crush">
+						<input class="valinp" type="checkbox" name="romantic" value="crush" id="crush" <?php xfn_check('romantic', 'crush'); ?> />
+						<?php _e('crush') ?></label>
+						<label for="date">
+						<input class="valinp" type="checkbox" name="romantic" value="date" id="date" <?php xfn_check('romantic', 'date'); ?> />
+						<?php _e('date') ?></label>
+						<label for="romantic">
+						<input class="valinp" type="checkbox" name="romantic" value="sweetheart" id="romantic" <?php xfn_check('romantic', 'sweetheart'); ?> />
+						<?php _e('sweetheart') ?></label>
+					</td>
+				</tr>
+			</table>
+		</td>
+	</tr>
 </table>
+</div>
 </fieldset>
-       <p class="submit">
-       <input type="submit" name="submit" value="<?php echo $submit_text ?>" />
-       </p>
-<fieldset class="options">
-        <legend><?php _e('Advanced') ?></legend>
-        <table class="editform" width="100%" cellspacing="2" cellpadding="5">
-         <tr>
-           <th width="33%" scope="row"><?php _e('Image URI:') ?></th>
-           <td width="67%"><input type="text" name="link_image" size="50" value="<?php echo $link->link_image; ?>" style="width: 95%" /></td>
-         </tr>
-<tr>
-           <th scope="row"><?php _e('RSS URI:') ?> </th>
-           <td><input name="link_rss" type="text" id="rss_uri" value="<?php echo $link->link_rss; ?>" size="50" style="width: 95%" /></td>
-         </tr>
-         <tr>
-           <th scope="row"><?php _e('Notes:') ?></th>
-           <td><textarea name="link_notes" cols="50" rows="10" style="width: 95%"><?php echo $link->link_notes; ?></textarea></td>
-         </tr>
-         <tr>
-           <th scope="row"><?php _e('Rating:') ?></th>
-           <td><select name="link_rating" size="1">
-<?php
-    for ($r = 0; $r < 10; $r++) {
-      echo('            <option value="'.$r.'" ');
-      if ($link->link_rating == $r)
-        echo 'selected="selected"';
-      echo('>'.$r.'</option>');
-    }
-?>
-           </select>
-         &nbsp;<?php _e('(Leave at 0 for no rating.)') ?> </td>
-         </tr>
-         <tr>
-           <th scope="row"><?php _e('Target') ?></th>
-           <td><label>
-          <input type="radio" name="link_target" value="_blank"   <?php echo(($link->link_target == '_blank') ? 'checked="checked"' : ''); ?> />
-          <code>_blank</code></label><br />
-<label>
-<input type="radio" name="link_target" value="_top" <?php echo(($link->link_target == '_top') ? 'checked="checked"' : ''); ?> />
-<code>_top</code></label><br />
-<label>
-<input type="radio" name="link_target" value=""     <?php echo(($link->link_target == '') ? 'checked="checked"' : ''); ?> />
-<?php _e('none') ?></label><br />
-<?php _e('(Note that the <code>target</code> attribute is illegal in XHTML 1.1 and 1.0 Strict.)') ?></td>
-         </tr>
-         <tr>
-           <th scope="row"><?php _e('Visible:') ?></th>
-           <td><label>
-             <input type="radio" name="link_visible" <?php if ($link->link_visible == 'Y') echo "checked='checked'"; ?> value="Y" />
-<?php _e('Yes') ?></label><br /><label>
-<input type="radio" name="link_visible" <?php if ($link->link_visible == 'N') echo "checked='checked'"; ?> value="N" />
-<?php _e('No') ?></label></td>
-         </tr>
+
+<fieldset id="advanced" class="dbx-box">
+<h3 class="dbx-handle"><?php _e('Advanced') ?></h3>
+<div class="dbx-content">
+<table class="editform" width="100%" cellspacing="2" cellpadding="5">
+	<tr>
+		<th width="33%" scope="row"><?php _e('Image URI:') ?></th>
+		<td width="67%"><input type="text" name="link_image" size="50" value="<?php echo $link->link_image; ?>" style="width: 95%" /></td>
+	</tr>
+	<tr>
+		<th scope="row"><?php _e('RSS URI:') ?> </th>
+		<td><input name="link_rss" type="text" id="rss_uri" value="<?php echo $link->link_rss; ?>" size="50" style="width: 95%" /></td>
+	</tr>
+	<tr>
+		<th scope="row"><?php _e('Notes:') ?></th>
+		<td><textarea name="link_notes" cols="50" rows="10" style="width: 95%"><?php echo $link->link_notes; ?></textarea></td>
+	</tr>
+	<tr>
+		<th scope="row"><?php _e('Rating:') ?></th>
+		<td><select name="link_rating" size="1">
+		<?php
+			for ($r = 0; $r < 10; $r++) {
+				echo('            <option value="'.$r.'" ');
+				if ($link->link_rating == $r)
+					echo 'selected="selected"';
+				echo('>'.$r.'</option>');
+			}
+		?></select>&nbsp;<?php _e('(Leave at 0 for no rating.)') ?>
+		</td>
+	</tr>
 </table>
 </fieldset>
-<p class="submit"><input type="submit" name="submit" value="<?php echo $submit_text ?>" /></p>
-<?php if ( $editing ) : ?>
-          <input type="hidden" name="action" value="editlink" />
-          <input type="hidden" name="link_id" value="<?php echo (int) $link_id; ?>" />
-          <input type="hidden" name="order_by" value="<?php echo wp_specialchars($order_by, 1); ?>" />
-          <input type="hidden" name="cat_id" value="<?php echo (int) $cat_id ?>" />
+</div>
+
+<?php if ( $link_id ) : ?>
+<input type="hidden" name="action" value="save" />
+<input type="hidden" name="link_id" value="<?php echo (int) $link_id; ?>" />
+<input type="hidden" name="order_by" value="<?php echo wp_specialchars($order_by, 1); ?>" />
+<input type="hidden" name="cat_id" value="<?php echo (int) $cat_id ?>" />
 <?php else: ?>
-       	<input type="hidden" name="action" value="Add" />
+<input type="hidden" name="action" value="add" />
 <?php endif; ?>
-</form> 
+</div>
+</form>
 </div>
\ No newline at end of file
Index: wp-admin/admin-header.php
===================================================================
--- wp-admin/admin-header.php	(revision 3568)
+++ wp-admin/admin-header.php	(working copy)
@@ -40,6 +40,8 @@
 var manager = new dbxManager('postmeta');
 <?php break; case 'page.php' : case 'page-new.php' : ?>
 var manager = new dbxManager('pagemeta');
+<?php break; case 'link.php' : case 'link-add.php' : ?>
+var manager = new dbxManager('linkmeta');
 <?php break; endswitch; ?>
 });
 //]]>
Index: wp-admin/link-categories.php
===================================================================
--- wp-admin/link-categories.php	(revision 3568)
+++ wp-admin/link-categories.php	(working copy)
@@ -1,13 +1,11 @@
 <?php
-// Links
-// Copyright (C) 2002, 2003 Mike Little -- mike@zed1.com
 require_once('admin.php');
+
 $title = __('Link Categories');
-$this_file='link-categories.php';
 $parent_file = 'link-manager.php';
 $list_js = true;
 
-$wpvarstoreset = array('action', 'cat', 'auto_toggle');
+$wpvarstoreset = array('action','cat');
 for ($i=0; $i<count($wpvarstoreset); $i += 1) {
     $wpvar = $wpvarstoreset[$i];
     if (!isset($$wpvar)) {
@@ -23,433 +21,149 @@
     }
 }
 
-switch ($action) {
-  case 'addcat':
-  {
-      if ( !current_user_can('manage_links') )
-          die (__("Cheatin' uh ?"));
+switch($action) {
 
-      $cat_name = wp_specialchars($_POST['cat_name']);
-      $auto_toggle = $_POST['auto_toggle'];
-      if ($auto_toggle != 'Y') {
-          $auto_toggle = 'N';
-      }
+case 'addcat':
 
-      $show_images = $_POST['show_images'];
-      if ($show_images != 'Y') {
-          $show_images = 'N';
-      }
+	if ( !current_user_can('manage_categories') )
+		die (__('Cheatin&#8217; uh?'));
 
-      $show_description = $_POST['show_description'];
-      if ($show_description != 'Y') {
-          $show_description = 'N';
-      }
+	wp_insert_category($_POST);
 
-      $show_rating = $_POST['show_rating'];
-      if ($show_rating != 'Y') {
-          $show_rating = 'N';
-      }
+	header('Location: link-categories.php?message=1#addcat');
+break;
 
-      $show_updated = $_POST['show_updated'];
-      if ($show_updated != 'Y') {
-          $show_updated = 'N';
-      }
+case 'delete':
 
-      $sort_order = $_POST['sort_order'];
+	check_admin_referer();
 
-      $sort_desc = $_POST['sort_desc'];
-      if ($sort_desc != 'Y') {
-          $sort_desc = 'N';
-      }
-      $text_before_link = $_POST['text_before_link'];
-      $text_after_link = $_POST['text_after_link'];
-      $text_after_all = $_POST['text_after_all'];
+	if ( !current_user_can('manage_categories') )
+		die (__('Cheatin&#8217; uh?'));
 
-      $list_limit = $_POST['list_limit'];
-      if ($list_limit == '')
-          $list_limit = -1;
+	$cat_ID = (int) $_GET['cat_ID'];
+	$cat_name = get_catname($cat_ID);
 
-      $wpdb->query("INSERT INTO $wpdb->linkcategories (cat_id, cat_name, auto_toggle, show_images, show_description, \n" .
-             " show_rating, show_updated, sort_order, sort_desc, text_before_link, text_after_link, text_after_all, list_limit) \n" .
-             " VALUES ('0', '$cat_name', '$auto_toggle', '$show_images', '$show_description', \n" .
-             " '$show_rating', '$show_updated', '$sort_order', '$sort_desc', '$text_before_link', '$text_after_link', \n" .
-             " '$text_after_all', $list_limit)");
+	if ( 1 == $cat_ID )
+		die(sprintf(__("Can't delete the <strong>%s</strong> category: this is the default one"), $cat_name));
 
-      header('Location: link-categories.php');
-    break;
-  } // end addcat
-  case 'Delete':
-  {
-    $cat_id = (int) $_GET['cat_id'];
-    $cat_name=get_linkcatname($cat_id);
+	wp_delete_category($cat_ID);
 
-    if ($cat_id=="1")
-        die(sprintf(__("Can't delete the <strong>%s</strong> link category: this is the default one"), $cat_name));
+	header('Location: link-categories.php?message=2');
 
-    if ( !current_user_can('manage_links') )
-      die (__("Cheatin' uh ?"));
+break;
 
-    $wpdb->query("DELETE FROM $wpdb->linkcategories WHERE cat_id='$cat_id'");
-    $wpdb->query("UPDATE $wpdb->links SET link_category=1 WHERE link_category='$cat_id'");
+case 'edit':
 
-    header('Location: link-categories.php');
-    break;
-  } // end delete
-  case 'Edit':
-  {
-    include_once ('admin-header.php');
-    $cat_id = (int) $_GET['cat_id'];
-    $row = $wpdb->get_row("SELECT cat_id, cat_name, auto_toggle, show_images, show_description, "
-         . " show_rating, show_updated, sort_order, sort_desc, text_before_link, text_after_link, "
-         . " text_after_all, list_limit FROM $wpdb->linkcategories WHERE cat_id=$cat_id");
-    if ($row) {
-        if ($row->list_limit == -1) {
-            $row->list_limit = '';
-        }
-?>
+    require_once ('admin-header.php');
+    $cat_ID = (int) $_GET['cat_ID'];
+    $category = get_category_to_edit($cat_ID);
+    ?>
 
 <div class="wrap">
-  <h2><?php printf(__('Edit &#8220%s&#8221; Category'), wp_specialchars($row->cat_name)); ?></h2>
-
-  <form name="editcat" method="post">
-      <input type="hidden" name="action" value="editedcat" />
-      <input type="hidden" name="cat_id" value="<?php echo $row->cat_id ?>" />
-<fieldset class="options">
-<legend><?php _e('Category Options') ?></legend>
-<table class="editform" width="100%" cellspacing="2" cellpadding="5">
-<tr>
-	<th width="33%" scope="row"><?php _e('Name:') ?></th>
-	<td width="67%"><input name="cat_name" type="text" value="<?php echo wp_specialchars($row->cat_name)?>" size="30" /></td>
-</tr>
-<tr>
-	<th scope="row"><?php _e('Show:') ?></th>
-        <td>
-            <label>
-            <input type="checkbox" name="show_images"  value="Y" <?php checked('Y', $row->show_images); ?> /> 
-            <?php _e('Image') ?></label> <br />
-            <label>
-            <input type="checkbox" name="show_description" value="Y" <?php checked('Y', $row->show_description); ?> /> 
-            <?php _e('Description') ?></label> 
-            <?php _e('(shown in <code>title</code> regardless)') ?><br />
-            <label>
-            <input type="checkbox" name="show_rating"  value="Y" <?php checked('Y', $row->show_rating); ?> /> 
-            <?php _e('Rating') ?></label> <br />
-            <label>
-            <input type="checkbox" name="show_updated" value="Y" <?php checked('Y', $row->show_updated); ?> /> 
-            <?php _e('Updated') ?></label>
-<?php _e('(shown in <code>title</code> regardless)') ?></td>
-</tr>
-<tr>
-	<th scope="row"><?php _e('Sort order:') ?></th>
-	<td>
-	<select name="sort_order" size="1">
-            <option value="name" <?php echo ($row->sort_order == 'name') ? 'selected="selected"' : ''?>><?php _e('Name') ?></option>
-            <option value="id"      <?php echo ($row->sort_order == 'id') ? 'selected' : ''?>><?php _e('Id') ?></option>
-            <option value="url"     <?php echo ($row->sort_order == 'url') ? 'selected' : ''?>><?php _e('URL') ?></option>
-            <option value="rating"  <?php echo ($row->sort_order == 'rating') ? 'selected' : ''?>><?php _e('Rating') ?></option>
-            <option value="updated" <?php echo ($row->sort_order == 'updated') ? 'selected' : ''?>><?php _e('Updated') ?></option>
-            <option value="rand"  <?php echo ($row->sort_order == 'rand') ? 'selected' : ''?>><?php _e('Random') ?></option>
-            <option value="length"  <?php echo ($row->sort_order == 'length') ? 'selected' : ''?>><?php _e('Name Length') ?></option>
-	</select>
-	<label>
-	<input type="checkbox" name="sort_desc" value="Y" <?php checked('Y', $row->sort_desc); ?> /> 
-	<?php _e('Descending') ?></label>
-	</td>
-</tr>
-<tr>
-	<th scope="row"><?php _e('Limit:') ?></th>
-	<td>
-	<input type="text" name="list_limit" size="5" value="<?php echo $row->list_limit ?>" /> 
-	<?php _e('(Leave empty for no limit to number of links shown)') ?>
-	</td>
-</tr>
-<tr>
-	<th scope="row"><?php _e('Toggle:') ?></th>
-	<td><label>
-		<input type="checkbox" name="auto_toggle"  value="Y" <?php checked('Y', $row->auto_toggle); ?> /> 
-		<?php _e('When new link is added toggle all others to be invisible') ?></label></td>
-</tr>
-
-</table>
-</fieldset>
-<fieldset class="options">
-<legend><?php _e('Formatting') ?></legend>
-<table class="editform" width="100%" cellspacing="2" cellpadding="5">
-<tr>
-	<th width="33%" scope="row"><?php _e('Before Link:') ?></th>
-	<td width="67%"><input type="text" name="text_before_link" size="45" value="<?php echo wp_specialchars($row->text_before_link)?>" /></td>
-</tr>
-<tr>
-<th scope="row"><?php _e('Between Link and Description:') ?></th>
-<td><input type="text" name="text_after_link" size="45" value="<?php echo wp_specialchars($row->text_after_link)?>" /></td>
-</tr>
-<tr>
-<th scope="row"><?php _e('After Link:') ?></th>
-<td><input type="text" name="text_after_all" size="45" value="<?php echo wp_specialchars($row->text_after_all)?>"/></td>
-</tr>
-</table>
-</fieldset>
-<p class="submit"><input type="submit" name="submit" value="<?php _e('Save Category Settings &raquo;') ?>" /></p>
-</form>
-
+ <h2><?php _e('Edit Category') ?></h2>
+ <form name="editcat" action="link-categories.php" method="post">
+	  <table class="editform" width="100%" cellspacing="2" cellpadding="5">
+		<tr>
+		  <th width="33%" scope="row"><?php _e('Category name:') ?></th>
+		  <td width="67%"><input name="cat_name" type="text" value="<?php echo wp_specialchars($category->cat_name); ?>" size="40" /> <input type="hidden" name="action" value="editedcat" />
+<input type="hidden" name="cat_ID" value="<?php echo $category->cat_ID ?>" /></td>
+		</tr>
+		<tr>
+			<th scope="row"><?php _e('Category slug:') ?></th>
+			<td><input name="category_nicename" type="text" value="<?php echo wp_specialchars($category->category_nicename); ?>" size="40" /></td>
+		</tr>
+		<tr>
+			<th scope="row"><?php _e('Category parent:') ?></th>
+			<td>        
+			<select name='category_parent'>
+	  <option value='0' <?php if (!$category->category_parent) echo " selected='selected'"; ?>><?php _e('None') ?></option>
+	  <?php wp_dropdown_cats($category->cat_ID, $category->category_parent); ?>
+	  </select></td>
+		</tr>
+		<tr>
+			<th scope="row"><?php _e('Description:') ?></th>
+			<td><textarea name="category_description" rows="5" cols="50" style="width: 97%;"><?php echo wp_specialchars($category->category_description, 1); ?></textarea></td>
+		</tr>
+		</table>
+	  <p class="submit"><input type="submit" name="submit" value="<?php _e('Edit category &raquo;') ?>" /></p>
+ </form>
+ <p><a href="link-categories.php"><?php _e('&laquo; Return to category list'); ?></a></p>
 </div>
-<?php
-    } // end if row
-    break;
-  } // end Edit
-  case "editedcat":
-  {
-    if ( !current_user_can('manage_links') )
-      die (__("Cheatin' uh ?"));
+    <?php
 
-    $submit=$_POST["submit"];
-    if (isset($submit)) {
+break;
 
-    $cat_id = (int)$_POST["cat_id"];
+case 'editedcat':
+	if ( !current_user_can('manage_categories') )
+		die (__('Cheatin&#8217; uh?'));
 
-    $cat_name= wp_specialchars($_POST["cat_name"]);
-    $auto_toggle = $_POST["auto_toggle"];
-    if ($auto_toggle != 'Y') {
-        $auto_toggle = 'N';
-    }
+	wp_update_category($_POST);
 
-    $show_images = $_POST["show_images"];
-    if ($show_images != 'Y') {
-        $show_images = 'N';
-    }
+	header('Location: link-categories.php?message=3');
+break;
 
-    $show_description = $_POST["show_description"];
-    if ($show_description != 'Y') {
-        $show_description = 'N';
-    }
+default:
 
-    $show_rating = $_POST["show_rating"];
-    if ($show_rating != 'Y') {
-        $show_rating = 'N';
-    }
+require_once ('admin-header.php');
 
-    $show_updated = $_POST["show_updated"];
-    if ($show_updated != 'Y') {
-        $show_updated = 'N';
-    }
-
-    $sort_order = $_POST["sort_order"];
-
-    $sort_desc = $_POST["sort_desc"];
-    if ($sort_desc != 'Y') {
-        $sort_desc = 'N';
-    }
-    $text_before_link = $_POST["text_before_link"];
-    $text_after_link = $_POST["text_after_link"];
-    $text_after_all = $_POST["text_after_all"];
-
-    $list_limit = $_POST["list_limit"];
-    if ($list_limit == '')
-        $list_limit = -1;
-
-    $wpdb->query("UPDATE $wpdb->linkcategories set
-            cat_name='$cat_name',
-            auto_toggle='$auto_toggle',
-            show_images='$show_images',
-            show_description='$show_description',
-            show_rating='$show_rating',
-            show_updated='$show_updated',
-            sort_order='$sort_order',
-            sort_desc='$sort_desc',
-            text_before_link='$text_before_link',
-            text_after_link='$text_after_link',
-            text_after_all='$text_after_all',
-            list_limit=$list_limit
-            WHERE cat_id=$cat_id
-            ");
-    } // end if save
-
-
-    header("Location: link-categories.php");
-    break;
-  } // end editcat
-  default:
-  {
-    include_once ("admin-header.php");
-    if ( !current_user_can('manage_links') )
-      die(__("You have do not have sufficient permissions to edit the link categories for this blog. :)"));
+$messages[1] = __('Category added.');
+$messages[2] = __('Category deleted.');
+$messages[3] = __('Category updated.');
 ?>
 
+<?php if (isset($_GET['message'])) : ?>
+<div id="message" class="updated fade"><p><?php echo $messages[$_GET['message']]; ?></p></div>
+<?php endif; ?>
+
 <div class="wrap">
-            <h2><?php _e('Link Categories:') ?></h2>
-            <table id="the-list" width="100%" cellpadding="5" cellspacing="0" border="0">
-              <tr>
- 	        <th rowspan="2" valign="bottom"><?php _e('Name') ?></th>
-                <th rowspan="2" valign="bottom"><?php _e('ID') ?></th>
-                <th rowspan="2" valign="bottom"><?php _e('Toggle?') ?></th>
-                <th colspan="4" valign="bottom" class="alternate"><?php _e('Show') ?></th>
-                <th rowspan="2" valign="bottom"><?php _e('Sort Order') ?></th>
-                <th rowspan="2" valign="bottom"><?php _e('Desc?') ?></th>
-                <th colspan="3" valign="bottom" class="alternate"><?php _e('Formatting') ?></th>
-                <th rowspan="2" valign="bottom"><?php _e('Limit') ?></th>
-                <th rowspan="2" colspan="2">&nbsp;</th>
-              </tr>
-              <tr>
-                <th valign="top"><?php _e('Images') ?></th>
-                <th valign="top"><?php _e('Description') ?></th>
-                <th valign="top"><?php _e('Rating') ?></th>
-                <th valign="top"><?php _e('Updated') ?></th>
-                <th valign="top"><?php _e('Before') ?></th>
-                <th valign="top"><?php _e('Between') ?></th>
-                <th valign="top"><?php _e('After') ?></th>
-              </tr>
+<?php if ( current_user_can('manage_categories') ) : ?>
+	<h2><?php printf(__('Categories (<a href="%s">add new</a>)'), '#addcat') ?> </h2>
+<?php else : ?>
+	<h2><?php _e('Categories') ?> </h2>
+<?php endif; ?>
+<table id="the-list-x" width="100%" cellpadding="3" cellspacing="3">
+	<tr>
+		<th scope="col"><?php _e('ID') ?></th>
+        <th scope="col"><?php _e('Name') ?></th>
+        <th scope="col"><?php _e('Description') ?></th>
+        <th scope="col"><?php _e('# Links') ?></th>
+        <th colspan="2"><?php _e('Action') ?></th>
+	</tr>
 <?php
-$results = $wpdb->get_results("SELECT cat_id, cat_name, auto_toggle, show_images, show_description, "
-         . " show_rating, show_updated, sort_order, sort_desc, text_before_link, text_after_link, "
-         . " text_after_all, list_limit FROM $wpdb->linkcategories ORDER BY cat_id");
-$i = 1;
-foreach ($results as $row) {
-    if ($row->list_limit == -1) {
-        $row->list_limit = __('none');
-    }
-    $style = ($i % 2) ? ' class="alternate"' : '';
-    /*
-    	Manually internationalize every sort order option.
-    */
-    switch ($row->sort_order) {
-    	case 'name':
-    		$row->sort_order = __('name');
-    		break;
-    	case 'id':
-    		$row->sort_order = __('id');
-    		break;
-    	case 'url':
-    		$row->sort_order = __('url');
-    		break;
-    	case 'rating':
-    		$row->sort_order = __('rating');
-    		break;
-    	case 'updated':
-    		$row->sort_order = __('updated');
-    		break;
-    	case 'rand':
-    		$row->sort_order = __('rand');
-    		break;
-    	case 'length':
-    		$row->sort_order = __('length');
-    		break;
-    }
+link_cat_rows();
 ?>
-              <tr id="link-category-<?php echo $row->cat_id; ?>" valign="middle" align="center" <?php echo $style ?> style="border-bottom: 1px dotted #9C9A9C;">
-                <td><?php echo wp_specialchars($row->cat_name)?></td>
-				<td ><?php echo $row->cat_id?></td>
-                <td><?php echo $row->auto_toggle == 'Y' ? __('Yes') : __('No') ?></td>
-                <td><?php echo $row->show_images == 'Y' ? __('Yes') : __('No') ?></td>
-                <td><?php echo $row->show_description == 'Y' ? __('Yes') : __('No') ?></td>
-                <td><?php echo $row->show_rating == 'Y' ? __('Yes') : __('No') ?></td>
-                <td><?php echo $row->show_updated == 'Y' ? __('Yes') : __('No') ?></td>
-                <td><?php echo $row->sort_order ?></td>
-                <td><?php echo $row->sort_desc == 'Y' ? __('Yes') : __('No') ?></td>
-                <td nowrap="nowrap"><?php echo htmlentities($row->text_before_link)?>&nbsp;</td>
-                <td nowrap="nowrap"><?php echo htmlentities($row->text_after_link)?>&nbsp;</td>
-                <td nowrap="nowrap"><?php echo htmlentities($row->text_after_all)?></td>
-                <td><?php echo $row->list_limit ?></td>
-                <td><a href="link-categories.php?cat_id=<?php echo $row->cat_id?>&amp;action=Edit" class="edit"><?php _e('Edit') ?></a></td>
-                <td><a href="link-categories.php?cat_id=<?php echo $row->cat_id?>&amp;action=Delete" onclick="return deleteSomething( 'link category', <?php echo $row->cat_id . ", '" . sprintf(__("You are about to delete the &quot;%s&quot; link category.\\n&quot;Cancel&quot; to stop, &quot;OK&quot; to delete."), wp_specialchars($row->cat_name,1)); ?>' );" class="delete"><?php _e('Delete') ?></a></td>
-              </tr>
-<?php
-        ++$i;
-    }
-?>
-            </table>
-<p><?php _e('These are the defaults for when you call a link category with no additional arguments. All of these settings may be overwritten.') ?></p>
+</table>
 
 <div id="ajax-response"></div>
 
 </div>
 
+<?php if ( current_user_can('manage_categories') ) : ?>
 <div class="wrap">
-    <form name="addcat" method="post">
-      <input type="hidden" name="action" value="addcat" />
-	  <h2><?php _e('Add a Link Category:') ?></h2>
-<fieldset class="options">
-<legend><?php _e('Category Options') ?></legend>
-<table class="editform" width="100%" cellspacing="2" cellpadding="5">
-<tr>
-	<th width="33%" scope="row"><?php _e('Name:') ?></th>
-	<td width="67%"><input type="text" name="cat_name" size="30" /></td>
-</tr>
-<tr>
-	<th scope="row"><?php _e('Show:') ?></th>
-        <td>
-            <label>
-            <input type="checkbox" name="show_images"  value="Y" /> 
-            <?php _e('Image') ?></label> <br />
-            <label>
-            <input type="checkbox" name="show_description" value="Y" /> 
-            <?php _e('Description') ?></label> 
-            <?php _e('(shown in <code>title</code> regardless)') ?><br />
-            <label>
-            <input type="checkbox" name="show_rating"  value="Y" /> 
-            <?php _e('Rating') ?></label> <br />
-            <label>
-            <input type="checkbox" name="show_updated" value="Y" /> 
-            <?php _e('Updated') ?></label>
-<?php _e('(shown in <code>title</code> regardless)') ?></td>
-</tr>
-<tr>
-	<th scope="row"><?php _e('Sort order:') ?></th>
-	<td>
-	<select name="sort_order" size="1">
-	<option value="name"><?php _e('Name') ?></option>
-	<option value="id"><?php _e('Id') ?></option>
-	<option value="url"><?php _e('URL') ?></option>
-	<option value="rating"><?php _e('Rating') ?></option>
-	<option value="updated"><?php _e('Updated') ?></option>
-	<option value="rand"><?php _e('Random') ?></option>
-	</select>
-	<label>
-	<input type="checkbox" name="sort_desc" value="Y" /> 
-	<?php _e('Descending') ?></label>
-	</td>
-</tr>
-<tr>
-	<th scope="row"><?php _e('Limit:') ?></th>
-	<td>
-	<input type="text" name="list_limit" size="5" value="" /> <?php _e('(Leave empty for no limit to number of links shown)') ?>
-	</td>
-</tr>
-<tr>
-	<th scope="row"><?php _e('Toggle:') ?></th>
-	<td><label>
-		<input type="checkbox" name="auto_toggle"  value="Y" /> 
-		<?php _e('When new link is added toggle all others to be invisible') ?></label></td>
-</tr>
-
-</table>
-</fieldset>
-<fieldset class="options">
-<legend><?php _e('Formatting') ?></legend>
-<table class="editform" width="100%" cellspacing="2" cellpadding="5">
-<tr>
-	<th width="33%" scope="row"><?php _e('Before Link:') ?></th>
-	<td width="67%"><input type="text" name="text_before_link" size="45" value="&lt;li&gt;" /></td>
-</tr>
-<tr>
-<th scope="row"><?php _e('Between Link and Description:') ?></th>
-<td><input type="text" name="text_after_link" size="45" value="&lt;br /&gt;" /></td>
-</tr>
-<tr>
-<th scope="row"><?php _e('After Link:') ?></th>
-<td><input type="text" name="text_after_all" size="45" value="&lt;/li&gt;"/></td>
-</tr>
-</table>
-</fieldset>
-<p class="submit"><input type="submit" name="submit" value="<?php _e('Add Category &raquo;') ?>" /></p>
-  </form>
+<p><?php printf(__('<strong>Note:</strong><br />Deleting a category does not delete posts from that category, it will just set them back to the default category <strong>%s</strong>.'), get_catname(get_option('default_category'))) ?></p>
 </div>
+
 <div class="wrap">
-    <h3><?php _e('Note:') ?></h3>
-	<p><?php printf(__('Deleting a link category does not delete links from that category.<br />It will just set them back to the default category <strong>%s</strong>.'), get_linkcatname(1)) ?></p>
+    <h2><?php _e('Add New Category') ?></h2>
+    <form name="addcat" id="addcat" action="link-categories.php" method="post">
+        
+        <p><?php _e('Name:') ?><br />
+        <input type="text" name="cat_name" value="" /></p>
+        <p><?php _e('Category parent:') ?><br />
+        <select name='category_parent' class='postform'>
+        <option value='0'><?php _e('None') ?></option>
+        <?php wp_dropdown_cats(0); ?>
+        </select></p>
+        <p><?php _e('Description: (optional)') ?> <br />
+        <textarea name="category_description" rows="5" cols="50" style="width: 97%;"></textarea></p>
+        <p class="submit"><input type="hidden" name="action" value="addcat" /><input type="submit" name="submit" value="<?php _e('Add Category &raquo;') ?>" /></p>
+    </form>
 </div>
+<?php endif; ?>
+
 <?php
-    break;
-  } // end default
-} // end case
-?>
-<?php include('admin-footer.php'); ?>
+break;
+}
+
+include('admin-footer.php');
+?>
\ No newline at end of file
Index: wp-admin/link-add.php
===================================================================
--- wp-admin/link-add.php	(revision 3568)
+++ wp-admin/link-add.php	(working copy)
@@ -26,6 +26,7 @@
 }
 
 $xfn_js = true;
+$editing = true;
 require('admin-header.php');
 ?>
 
Index: wp-admin/link.php
===================================================================
--- wp-admin/link.php	(revision 0)
+++ wp-admin/link.php	(revision 0)
@@ -0,0 +1,169 @@
+<?php
+require_once ('admin.php');
+
+$wpvarstoreset = array ('action', 'cat_id', 'linkurl', 'name', 'image', 'description', 'visible', 'target', 'category', 'link_id', 'submit', 'order_by', 'links_show_cat_id', 'rating', 'rel', 'notes', 'linkcheck[]');
+
+for ($i = 0; $i < count($wpvarstoreset); $i += 1) {
+	$wpvar = $wpvarstoreset[$i];
+	if (!isset ($$wpvar)) {
+		if (empty ($_POST["$wpvar"])) {
+			if (empty ($_GET["$wpvar"])) {
+				$$wpvar = '';
+			} else {
+				$$wpvar = $_GET["$wpvar"];
+			}
+		} else {
+			$$wpvar = $_POST["$wpvar"];
+		}
+	}
+}
+
+if ('' != $_POST['assign'])
+	$action = 'assign';
+if ('' != $_POST['visibility'])
+	$action = 'visibility';
+if ('' != $_POST['move'])
+	$action = 'move';
+if ('' != $_POST['linkcheck'])
+	$linkcheck = $_POST[linkcheck];
+
+$this_file = 'link-manager.php';
+
+switch ($action) {
+	case 'assign' :
+		check_admin_referer();
+
+		// check the current user's level first.
+		if (!current_user_can('manage_links'))
+			die(__("Cheatin' uh ?"));
+
+		//for each link id (in $linkcheck[]): if the current user level >= the
+		//userlevel of the owner of the link then we can proceed.
+
+		if (count($linkcheck) == 0) {
+			header('Location: '.$this_file);
+			exit;
+		}
+		$all_links = join(',', $linkcheck);
+		$results = $wpdb->get_results("SELECT link_id, link_owner FROM $wpdb->links LEFT JOIN $wpdb->users ON link_owner = ID WHERE link_id in ($all_links)");
+		foreach ($results as $row) {
+			$ids_to_change[] = $row->link_id;
+		}
+
+		// should now have an array of links we can change
+		$all_links = join(',', $ids_to_change);
+		$q = $wpdb->query("update $wpdb->links SET link_owner='$newowner' WHERE link_id IN ($all_links)");
+
+		header('Location: '.$this_file);
+		break;
+
+	case 'visibility' :
+		check_admin_referer();
+
+		// check the current user's level first.
+		if (!current_user_can('manage_links'))
+			die(__("Cheatin' uh ?"));
+
+		//for each link id (in $linkcheck[]): toggle the visibility
+		if (count($linkcheck) == 0) {
+			header('Location: '.$this_file);
+			exit;
+		}
+		$all_links = join(',', $linkcheck);
+		$results = $wpdb->get_results("SELECT link_id, link_visible FROM $wpdb->links WHERE link_id in ($all_links)");
+		foreach ($results as $row) {
+			if ($row->link_visible == 'Y') { // ok to proceed
+				$ids_to_turnoff[] = $row->link_id;
+			} else {
+				$ids_to_turnon[] = $row->link_id;
+			}
+		}
+
+		// should now have two arrays of links to change
+		if (count($ids_to_turnoff)) {
+			$all_linksoff = join(',', $ids_to_turnoff);
+			$q = $wpdb->query("update $wpdb->links SET link_visible='N' WHERE link_id IN ($all_linksoff)");
+		}
+
+		if (count($ids_to_turnon)) {
+			$all_linkson = join(',', $ids_to_turnon);
+			$q = $wpdb->query("update $wpdb->links SET link_visible='Y' WHERE link_id IN ($all_linkson)");
+		}
+
+		header('Location: '.$this_file);
+		break;
+
+	case 'move' :
+		check_admin_referer();
+
+		// check the current user's level first.
+		if (!current_user_can('manage_links'))
+			die(__("Cheatin' uh ?"));
+
+		//for each link id (in $linkcheck[]) change category to selected value
+		if (count($linkcheck) == 0) {
+			header('Location: '.$this_file);
+			exit;
+		}
+		$all_links = join(',', $linkcheck);
+		// should now have an array of links we can change
+		//$q = $wpdb->query("update $wpdb->links SET link_category='$category' WHERE link_id IN ($all_links)");
+
+		header('Location: '.$this_file);
+		break;
+
+	case 'add' :
+		check_admin_referer();
+
+		add_link();
+
+		header('Location: '.$_SERVER['HTTP_REFERER'].'?added=true');
+		break;
+
+	case 'save' :
+		check_admin_referer();
+
+		$link_id = (int) $_POST['link_id'];
+		edit_link($link_id);
+
+		wp_redirect($this_file);
+		exit;
+		break;
+
+	case 'delete' :
+		check_admin_referer();
+
+		if (!current_user_can('manage_links'))
+			die(__("Cheatin' uh ?"));
+
+		$link_id = (int) $_GET['link_id'];
+
+		wp_delete_link($link_id);
+
+		wp_redirect($this_file);
+		break;
+
+	case 'edit' :
+		$xfn_js = true;
+		$editing = true;
+		$parent_file = 'link-manager.php';
+		$submenu_file = 'link-manager.php';
+		$title = __('Edit Link');
+		include_once ('admin-header.php');
+		if (!current_user_can('manage_links'))
+			die(__('You do not have sufficient permissions to edit the links for this blog.'));
+
+		$link_id = (int) $_GET['link_id'];
+
+		if (!$link = get_link_to_edit($link_id))
+			die(__('Link not found.'));
+
+		include ('edit-link-form.php');
+		break;
+
+	default :
+		break;
+}
+
+include ('admin-footer.php');
+?>
\ No newline at end of file
Index: wp-admin/link-manager.php
===================================================================
--- wp-admin/link-manager.php	(revision 3568)
+++ wp-admin/link-manager.php	(working copy)
@@ -1,253 +1,65 @@
 <?php
+
+
 // Links
 // Copyright (C) 2002, 2003 Mike Little -- mike@zed1.com
 
-require_once('admin.php');
+require_once ('admin.php');
 
 $title = __('Manage Links');
 $this_file = $parent_file = 'link-manager.php';
 $list_js = true;
 
-$wpvarstoreset = array('action','cat_id', 'linkurl', 'name', 'image',
-                       'description', 'visible', 'target', 'category', 'link_id',
-                       'submit', 'order_by', 'links_show_cat_id', 'rating', 'rel',
-                       'notes', 'linkcheck[]');
+$wpvarstoreset = array ('action', 'cat_id', 'linkurl', 'name', 'image', 'description', 'visible', 'target', 'category', 'link_id', 'submit', 'order_by', 'links_show_cat_id', 'rating', 'rel', 'notes', 'linkcheck[]');
 
-for ($i=0; $i<count($wpvarstoreset); $i += 1) {
-    $wpvar = $wpvarstoreset[$i];
-    if (!isset($$wpvar)) {
-        if (empty($_POST["$wpvar"])) {
-            if (empty($_GET["$wpvar"])) {
-                $$wpvar = '';
-            } else {
-                $$wpvar = $_GET["$wpvar"];
-            }
-        } else {
-            $$wpvar = $_POST["$wpvar"];
-        }
-    }
+for ($i = 0; $i < count($wpvarstoreset); $i += 1) {
+	$wpvar = $wpvarstoreset[$i];
+	if (!isset ($$wpvar)) {
+		if (empty ($_POST["$wpvar"])) {
+			if (empty ($_GET["$wpvar"])) {
+				$$wpvar = '';
+			} else {
+				$$wpvar = $_GET["$wpvar"];
+			}
+		} else {
+			$$wpvar = $_POST["$wpvar"];
+		}
+	}
 }
 
-$links_show_cat_id = $_COOKIE['links_show_cat_id_' . COOKIEHASH];
-$links_show_order = $_COOKIE['links_show_order_' . COOKIEHASH];
+if (empty ($cat_id))
+	$cat_id = 'all';
 
-if ('' != $_POST['assign']) $action = 'assign';
-if ('' != $_POST['visibility']) $action = 'visibility';
-if ('' != $_POST['move']) $action = 'move';
-if ('' != $_POST['linkcheck']) $linkcheck = $_POST[linkcheck];
+if (empty ($order_by))
+	$order_by = 'order_name';
 
-switch ($action) {
-  case 'assign':
-  {
-    check_admin_referer();
+$title = __('Manage Links');
+include_once ("./admin-header.php");
 
-    // check the current user's level first.
-    if ( !current_user_can('manage_links') )
-      die (__("Cheatin' uh ?"));
+if (!current_user_can('manage_links'))
+	die(__("You do not have sufficient permissions to edit the links for this blog."));
 
-    //for each link id (in $linkcheck[]): if the current user level >= the
-    //userlevel of the owner of the link then we can proceed.
-
-    if (count($linkcheck) == 0) {
-        header('Location: ' . $this_file);
-        exit;
-    }
-    $all_links = join(',', $linkcheck);
-    $results = $wpdb->get_results("SELECT link_id, link_owner FROM $wpdb->links LEFT JOIN $wpdb->users ON link_owner = ID WHERE link_id in ($all_links)");
-    foreach ($results as $row) {
-       $ids_to_change[] = $row->link_id;
-    }
-
-    // should now have an array of links we can change
-    $all_links = join(',', $ids_to_change);
-    $q = $wpdb->query("update $wpdb->links SET link_owner='$newowner' WHERE link_id IN ($all_links)");
-
-    header('Location: ' . $this_file);
-    break;
-  }
-  case 'visibility':
-  {
-    check_admin_referer();
-
-    // check the current user's level first.
-    if ( !current_user_can('manage_links') )
-      die (__("Cheatin' uh ?"));
-
-    //for each link id (in $linkcheck[]): toggle the visibility
-    if (count($linkcheck) == 0) {
-        header('Location: ' . $this_file);
-        exit;
-    }
-    $all_links = join(',', $linkcheck);
-    $results = $wpdb->get_results("SELECT link_id, link_visible FROM $wpdb->links WHERE link_id in ($all_links)");
-    foreach ($results as $row) {
-        if ($row->link_visible == 'Y') { // ok to proceed
-            $ids_to_turnoff[] = $row->link_id;
-        } else {
-            $ids_to_turnon[] = $row->link_id;
-        }
-    }
-
-    // should now have two arrays of links to change
-    if (count($ids_to_turnoff)) {
-        $all_linksoff = join(',', $ids_to_turnoff);
-        $q = $wpdb->query("update $wpdb->links SET link_visible='N' WHERE link_id IN ($all_linksoff)");
-    }
-
-    if (count($ids_to_turnon)) {
-        $all_linkson = join(',', $ids_to_turnon);
-        $q = $wpdb->query("update $wpdb->links SET link_visible='Y' WHERE link_id IN ($all_linkson)");
-    }
-
-    header('Location: ' . $this_file);
-    break;
-  }
-  case 'move':
-  {
-    check_admin_referer();
-
-    // check the current user's level first.
-    if ( !current_user_can('manage_links') )
-      die (__("Cheatin' uh ?"));
-
-    //for each link id (in $linkcheck[]) change category to selected value
-    if (count($linkcheck) == 0) {
-        header('Location: ' . $this_file);
-        exit;
-    }
-    $all_links = join(',', $linkcheck);
-    // should now have an array of links we can change
-    $q = $wpdb->query("update $wpdb->links SET link_category='$category' WHERE link_id IN ($all_links)");
-
-    header('Location: ' . $this_file);
-    break;
-  }
-
-  case 'Add':
-  {
-    check_admin_referer();
-
-	add_link();
-
-    header('Location: ' . $_SERVER['HTTP_REFERER'] . '?added=true');
-    break;
-  } // end Add
-
-  case 'editlink':
-  {
- 
-	check_admin_referer();
- 
-	if (isset($links_show_cat_id) && ($links_show_cat_id != ''))
-		$cat_id = $links_show_cat_id;
-
-	if (!isset($cat_id) || ($cat_id == '')) {
-		if (!isset($links_show_cat_id) || ($links_show_cat_id == ''))
-			$cat_id = 'All';
-	}
-	$links_show_cat_id = $cat_id;
-
-	$link_id = (int) $_POST['link_id'];
-	edit_link($link_id);
-
-    setcookie('links_show_cat_id_' . COOKIEHASH, $links_show_cat_id, time()+600);
-    wp_redirect($this_file);
-    break;
-  } // end Save
-
-  case 'Delete':
-  {
-    check_admin_referer();
-
-    if ( !current_user_can('manage_links') )
-      die (__("Cheatin' uh ?"));
-
-    $link_id = (int) $_GET['link_id'];
-
-	wp_delete_link($link_id);
-
-    if (isset($links_show_cat_id) && ($links_show_cat_id != ''))
-        $cat_id = $links_show_cat_id;
-
-    if (!isset($cat_id) || ($cat_id == '')) {
-        if (!isset($links_show_cat_id) || ($links_show_cat_id == ''))
-        $cat_id = 'All';
-    }
-    $links_show_cat_id = $cat_id;
-    setcookie('links_show_cat_id_' . COOKIEHASH, $links_show_cat_id, time()+600);
-    wp_redirect($this_file);
-    break;
-  } // end Delete
-
-  case 'linkedit': {
-	$xfn_js = true;
-	include_once ('admin-header.php');
-	if ( !current_user_can('manage_links') )
-		die(__('You do not have sufficient permissions to edit the links for this blog.'));
-
-	$link_id = (int) $_GET['link_id'];
-
-	if ( !$link = get_link_to_edit($link_id) )
-		die( __('Link not found.') );
-
-	include('edit-link-form.php');
-	break;
-  } // end linkedit
-  case __("Show"):
-  {
-    if (!isset($cat_id) || ($cat_id == '')) {
-        if (!isset($links_show_cat_id) || ($links_show_cat_id == ''))
-        $cat_id = 'All';
-    }
-    $links_show_cat_id = $cat_id;
-    if (!isset($order_by) || ($order_by == '')) {
-        if (!isset($links_show_order) || ($links_show_order == ''))
-        $order_by = 'order_name';
-    }
-    $links_show_order = $order_by;
-    //break; fall through
-  } // end Show
-  case "popup":
-  {
-    $link_url = stripslashes($_GET["linkurl"]);
-    $link_name = stripslashes($_GET["name"]);
-    //break; fall through
-  }
-  default:
-  {
-    if (isset($links_show_cat_id) && ($links_show_cat_id != ''))
-        $cat_id = $links_show_cat_id;
-
-    if (!isset($cat_id) || ($cat_id == '')) {
-        if (!isset($links_show_cat_id) || ($links_show_cat_id == ''))
-        $cat_id = 'All';
-    }
-    $links_show_cat_id = $cat_id;
-    if (isset($links_show_order) && ($links_show_order != ''))
-        $order_by = $links_show_order;
-
-    if (!isset($order_by) || ($order_by == ''))
-        $order_by = 'order_name';
-    $links_show_order = $order_by;
-
-    setcookie('links_show_cat_id_' . COOKIEHASH, $links_show_cat_id, time()+600);
-    setcookie('links_show_order_' . COOKIEHASH, $links_show_order, time()+600);
-    include_once ("./admin-header.php");
-    if ( !current_user_can('manage_links') )
-      die(__("You do not have sufficient permissions to edit the links for this blog."));
-
-    switch ($order_by)
-    {
-        case 'order_id':     $sqlorderby = 'id';          break;
-        case 'order_url':    $sqlorderby = 'url';         break;
-        case 'order_desc':   $sqlorderby = 'description'; break;
-        case 'order_owner':  $sqlorderby = 'owner';       break;
-        case 'order_rating': $sqlorderby = 'rating';      break;
-        case 'order_name':
-        default:             $sqlorderby = 'name';        break;
-    }
-
-  if ($action != "popup") {
+switch ($order_by) {
+	case 'order_id' :
+		$sqlorderby = 'id';
+		break;
+	case 'order_url' :
+		$sqlorderby = 'url';
+		break;
+	case 'order_desc' :
+		$sqlorderby = 'description';
+		break;
+	case 'order_owner' :
+		$sqlorderby = 'owner';
+		break;
+	case 'order_rating' :
+		$sqlorderby = 'rating';
+		break;
+	case 'order_name' :
+	default :
+		$sqlorderby = 'name';
+		break;
+}
 ?>
 <script type="text/javascript">
 <!--
@@ -266,131 +78,113 @@
 </script>
 
 <div class="wrap">
-    <form name="cats" method="post" action="">
-    <table width="75%" cellpadding="3" cellspacing="3">
-      <tr>
-        <td>
-        <?php _e('<strong>Show</strong> links in category:'); ?><br />
-        </td>
-        <td>
-          <?php _e('<strong>Order</strong> by:');?>
-        </td>
+<h2><?php _e('Link Management'); ?></h2>
+<form name="cats" method="post" action="">
+<table width="75%" cellpadding="3" cellspacing="3">
+	<tr>
+		<td>
+			<?php _e('<strong>Show</strong> links in category:'); ?><br />
+		</td>
+		<td>
+			<?php _e('<strong>Order</strong> by:');?>
+		</td>
 		<td>&nbsp;</td>
       </tr>
       <tr>
         <td>
-<?php
-    $results = $wpdb->get_results("SELECT cat_id, cat_name, auto_toggle FROM $wpdb->linkcategories ORDER BY cat_id");
-    echo "        <select name=\"cat_id\">\n";
-    echo "          <option value=\"All\"";
-    if ($cat_id == 'All')
-      echo " selected='selected'";
-    echo "> " . __('All') . "</option>\n";
-    foreach ($results as $row) {
-      echo "          <option value=\"".$row->cat_id."\"";
-      if ($row->cat_id == $cat_id)
-        echo " selected='selected'";
-        echo ">".$row->cat_id.": ".wp_specialchars($row->cat_name);
-        if ($row->auto_toggle == 'Y')
-            echo ' '.__('(auto toggle)');
-        echo "</option>\n";
-    }
-    echo "        </select>\n";
-?>
-        </td>
-        <td>
-          <select name="order_by">
-            <option value="order_id"     <?php if ($order_by == 'order_id')     echo " selected='selected'";?>><?php _e('Link ID') ?></option>
-            <option value="order_name"   <?php if ($order_by == 'order_name')   echo " selected='selected'";?>><?php _e('Name') ?></option>
-            <option value="order_url"    <?php if ($order_by == 'order_url')    echo " selected='selected'";?>><?php _e('URI') ?></option>
-            <option value="order_desc"   <?php if ($order_by == 'order_desc')   echo " selected='selected'";?>><?php _e('Description') ?></option>
-            <option value="order_owner"  <?php if ($order_by == 'order_owner')  echo " selected='selected'";?>><?php _e('Owner') ?></option>
-            <option value="order_rating" <?php if ($order_by == 'order_rating') echo " selected='selected'";?>><?php _e('Rating') ?></option>
-          </select>
-        </td>
-        <td>
-          <input type="submit" name="action" value="<?php _e('Show') ?>" />
-        </td>
-      </tr>
-    </table>
-    </form>
+		<?php $categories = get_categories("hide_empty=0"); ?>
+		<select name="cat_id">
+			<option value="all" <?php echo ($cat_id == 'all') ? " selected='selected'" : ''; ?>><?php _e('All') ?></option>
+			<?php foreach ($categories as $cat): ?>
+			<option value="<?php echo $cat->cat_ID; ?>"<?php echo ($cat->cat_ID == $cat_id) ? " selected='selected'" : ''; ?>><?php echo $cat->cat_ID . ": " . wp_specialchars($cat->cat_name); ?>
+			</option>
+			<?php endforeach; ?>
+			</select>
+		</td>
+		<td>
+			<select name="order_by">
+				<option value="order_id"     <?php if ($order_by == 'order_id')     echo " selected='selected'";?>><?php _e('Link ID') ?></option>
+				<option value="order_name"   <?php if ($order_by == 'order_name')   echo " selected='selected'";?>><?php _e('Name') ?></option>
+				<option value="order_url"    <?php if ($order_by == 'order_url')    echo " selected='selected'";?>><?php _e('URI') ?></option>
+				<option value="order_desc"   <?php if ($order_by == 'order_desc')   echo " selected='selected'";?>><?php _e('Description') ?></option>
+				<option value="order_owner"  <?php if ($order_by == 'order_owner')  echo " selected='selected'";?>><?php _e('Owner') ?></option>
+				<option value="order_rating" <?php if ($order_by == 'order_rating') echo " selected='selected'";?>><?php _e('Rating') ?></option>
+			</select>
+		</td>
+		<td>
+			<input type="submit" name="action" value="<?php _e('Show') ?>" />
+		</td>
+	</tr>
+</table>
+</form>
 
-</div>
-
 <form name="links" id="links" method="post" action="">
-<div class="wrap">
-
-    <input type="hidden" name="link_id" value="" />
-    <input type="hidden" name="action" value="" />
-    <input type="hidden" name="order_by" value="<?php echo wp_specialchars($order_by, 1); ?>" />
-    <input type="hidden" name="cat_id" value="<?php echo (int) $cat_id ?>" />
-  <table id="the-list-x" width="100%" cellpadding="3" cellspacing="3">
-    <tr>
-      <th width="15%"><?php _e('Name') ?></th>
-      <th><?php _e('URI') ?></th>
-      <th><?php _e('Category') ?></th>
-      <th><?php _e('rel') ?></th>
-      <th><?php _e('Image') ?></th>
-      <th><?php _e('Visible') ?></th>
-      <th colspan="2"><?php _e('Action') ?></th>
-      <th>&nbsp;</th>
-  </tr>
+<input type="hidden" name="link_id" value="" />
+<input type="hidden" name="action" value="" />
+<input type="hidden" name="order_by" value="<?php echo wp_specialchars($order_by, 1); ?>" />
+<input type="hidden" name="cat_id" value="<?php echo (int) $cat_id ?>" />
+<table id="the-list-x" width="100%" cellpadding="3" cellspacing="3">
+	<tr>
+		<th width="15%"><?php _e('Name') ?></th>
+		<th><?php _e('URI') ?></th>
+		<th><?php _e('Category') ?></th>
+		<th><?php _e('rel') ?></th>
+		<th><?php _e('Image') ?></th>
+		<th><?php _e('Visible') ?></th>
+		<th colspan="2"><?php _e('Action') ?></th>
+		<th>&nbsp;</th>
+	</tr>
 <?php
-    $sql = "SELECT link_url, link_name, link_image, link_description, link_visible,
-            link_category AS cat_id, cat_name AS category, $wpdb->users.user_login, link_id,
-            link_rating, link_rel
-            FROM $wpdb->links
-            LEFT JOIN $wpdb->linkcategories ON $wpdb->links.link_category = $wpdb->linkcategories.cat_id
-            LEFT JOIN $wpdb->users ON $wpdb->users.ID = $wpdb->links.link_owner ";
+if ( 'all' == $cat_id )
+	$cat_id = '';
+$links = get_linkz("category=$cat_id&hide_invisible=0&orderby=$sqlorderby&hide_empty=0");
+if ($links)
+	foreach ($links as $link) {
+		$link->link_name = wp_specialchars($link->link_name);
+		$link->link_description = wp_specialchars($link->link_description);
+		$link->link_url = wp_specialchars($link->link_url);
+		$link->link_category = wp_get_link_cats($link->link_id);
+		$short_url = str_replace('http://', '', $link->link_url);
+		$short_url = str_replace('www.', '', $short_url);
+		if ('/' == substr($short_url, -1))
+			$short_url = substr($short_url, 0, -1);
+		if (strlen($short_url) > 35)
+			$short_url = substr($short_url, 0, 32).'...';
 
-    if (isset($cat_id) && ($cat_id != 'All')) {
-      $sql .= " WHERE link_category = $cat_id ";
-    }
-    $sql .= ' ORDER BY link_' . $sqlorderby;
+		$image = ($link->link_image != null) ? __('Yes') : __('No');
+		$visible = ($link->link_visible == 'Y') ? __('Yes') : __('No');
+		++ $i;
+		$style = ($i % 2) ? '' : ' class="alternate"';
+?>
+	<tr id="link-<?php echo $link->link_id; ?>" valign="middle" <?php echo $style; ?>>
+		<td><strong><?php echo $link->link_name; ?></strong><br />
+		<?php
 
-    // echo "$sql";
-    $links = $wpdb->get_results($sql);
-    if ($links) {
-        foreach ($links as $link) {
-      	    $link->link_name = wp_specialchars($link->link_name);
-      	    $link->link_category = wp_specialchars($link->link_category);
-      	    $link->link_description = wp_specialchars($link->link_description);
-            $link->link_url = wp_specialchars($link->link_url);
-            $short_url = str_replace('http://', '', $link->link_url);
-            $short_url = str_replace('www.', '', $short_url);
-            if ('/' == substr($short_url, -1))
-                $short_url = substr($short_url, 0, -1);
-            if (strlen($short_url) > 35)
-                $short_url =  substr($short_url, 0, 32).'...';
 
-            $image = ($link->link_image != null) ? __('Yes') : __('No');
-            $visible = ($link->link_visible == 'Y') ? __('Yes') : __('No');
-            ++$i;
-            $style = ($i % 2) ? '' : ' class="alternate"';
+		echo sprintf(__('Description: %s'), $link->link_description)."</td>";
+		echo "<td><a href=\"$link->link_url\" title=\"".sprintf(__('Visit %s'), $link->link_name)."\">$short_url</a></td>";
 ?>
-    <tr id="link-<?php echo $link->link_id; ?>" valign="middle" <?php echo $style; ?>>
-		<td><strong><?php echo $link->link_name; ?></strong><br />
+        <td>
+        <?php
+
+
+		foreach ($link->link_category as $category) {
+			$cat_name = get_the_category_by_ID($category);
+			echo wp_specialchars($cat_name);
+			echo " ";
+		}
+?>
+		</td>
+        <td><?php echo $link->link_rel; ?></td>
+        <td align='center'><?php echo $image; ?></td>
+        <td align='center'><?php echo $visible; ?></td>
 <?php
-        echo sprintf(__('Description: %s'), $link->link_description) . "</td>";
-        echo "<td><a href=\"$link->link_url\" title=\"" . sprintf(__('Visit %s'), $link->link_name) . "\">$short_url</a></td>";
-        echo <<<LINKS
-        <td>$link->category</td>
-        <td>$link->link_rel</td>
-        <td align='center'>$image</td>
-        <td align='center'>$visible</td>
-LINKS;
-            $show_buttons = 1; // default
 
-            if ($show_buttons) {
-        echo '<td><a href="link-manager.php?link_id=' . $link->link_id . '&amp;action=linkedit" class="edit">' . __('Edit') . '</a></td>';
-        echo '<td><a href="link-manager.php?link_id=' . $link->link_id . '&amp;action=Delete"' .  " onclick=\"return deleteSomething( 'link', $link->link_id , '" . sprintf(__("You are about to delete the &quot;%s&quot; link to %s.\\n&quot;Cancel&quot; to stop, &quot;OK&quot; to delete."), wp_specialchars($link->link_name,1), wp_specialchars($link->link_url)) . '\' );" class="delete">' . __('Delete') . '</a></td>';
-        echo '<td><input type="checkbox" name="linkcheck[]" value="' . $link->link_id . '" /></td>';
-            } else {
-              echo "<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>\n";
-            }
+		echo '<td><a href="link.php?link_id='.$link->link_id.'&amp;action=edit" class="edit">'.__('Edit').'</a></td>';
+		echo '<td><a href="link.php?link_id='.$link->link_id.'&amp;action=delete"'." onclick=\"return deleteSomething( 'link', $link->link_id , '".sprintf(__("You are about to delete the &quot;%s&quot; link to %s.\\n&quot;Cancel&quot; to stop, &quot;OK&quot; to delete."), wp_specialchars($link->link_name, 1), wp_specialchars($link->link_url)).'\' );" class="delete">'.__('Delete').'</a></td>';
+		echo '<td><input type="checkbox" name="linkcheck[]" value="'.$link->link_id.'" /></td>';
 		echo "\n    </tr>\n";
-        }
-    }
+	}
 ?>
 </table>
 
@@ -406,40 +200,27 @@
         <td>
           <?php _e('Assign ownership to:'); ?>
 <?php
-    $results = $wpdb->get_results("SELECT ID, user_login FROM $wpdb->users ORDER BY ID");
-    echo "          <select name=\"newowner\" size=\"1\">\n";
-    foreach ($results as $row) {
-      echo "            <option value=\"".$row->ID."\"";
-      echo ">".$row->user_login;
-      echo "</option>\n";
-    }
-    echo "          </select>\n";
+
+$results = $wpdb->get_results("SELECT ID, user_login FROM $wpdb->users ORDER BY ID");
+echo "          <select name=\"newowner\" size=\"1\">\n";
+foreach ($results as $row) {
+	echo "            <option value=\"".$row->ID."\"";
+	echo ">".$row->user_login;
+	echo "</option>\n";
+}
+echo "          </select>\n";
 ?>
         <input name="assign" type="submit" id="assign" value="<?php _e('Go') ?>" />
         </td>
         <td>
           <input name="visibility" type="submit" id="visibility" value="<?php _e('Toggle Visibility') ?>" />
         </td>
-        <td>
-          <?php _e('Move to category:'); link_category_dropdown('category'); ?> <input name="move" type="submit" id="move" value="<?php _e('Go') ?>" />
-        </td>
         <td align="right">
           <a href="#" onclick="checkAll(document.getElementById('links')); return false; "><?php _e('Toggle Checkboxes') ?></a>
         </td>
     </tr>
 </table>
-
-<?php
-  } // end if !popup
-?>
 </div>
 </form>
 
-
-<?php
-    break;
-  } // end default
-} // end case
-?>
-
-<?php include('admin-footer.php'); ?>
+<?php include('admin-footer.php'); ?>
\ No newline at end of file
Index: wp-admin/install.php
===================================================================
--- wp-admin/install.php	(revision 3568)
+++ wp-admin/install.php	(working copy)
@@ -154,19 +154,34 @@
 if ( ! $public )
 	update_option('default_pingback_flag', 0);
 
-// Now drop in some default links
-$wpdb->query("INSERT INTO $wpdb->linkcategories (cat_id, cat_name) VALUES (1, '".$wpdb->escape(__('Blogroll'))."')");
-$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://blogs.linux.ie/xeer/', 'Donncha', 1, 'http://blogs.linux.ie/xeer/feed/', '');");
-$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://zengun.org/weblog/', 'Michel', 1, 'http://zengun.org/weblog/feed/', '');");
-$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://boren.nu/', 'Ryan', 1, 'http://boren.nu/feed/', '');");
-$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://photomatt.net/', 'Matt', 1, 'http://xml.photomatt.net/feed/', '');");
-$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://zed1.com/journalized/', 'Mike', 1, 'http://zed1.com/journalized/feed/', '');");
-$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://www.alexking.org/', 'Alex', 1, 'http://www.alexking.org/blog/wp-rss2.php', '');");
-$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://dougal.gunters.org/', 'Dougal', 1, 'http://dougal.gunters.org/feed/', '');");
-
 // Default category
 $wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_count, category_description) VALUES ('0', '".$wpdb->escape(__('Uncategorized'))."', '".sanitize_title(__('Uncategorized'))."', '1', '')");
 
+// Default link category
+$wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_count, category_description) VALUES ('0', '".$wpdb->escape(__('Blogroll'))."', '".sanitize_title(__('Blogroll'))."', '1', '')");
+
+// Now drop in some default links
+$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://blogs.linux.ie/xeer/', 'Donncha', 0, 'http://blogs.linux.ie/xeer/feed/', '');");
+$wpdb->query( "INSERT INTO $wpdb->link2cat (`link_id`, `category_id`) VALUES (1, 2)" );
+
+$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://zengun.org/weblog/', 'Michel', 0, 'http://zengun.org/weblog/feed/', '');");
+$wpdb->query( "INSERT INTO $wpdb->link2cat (`link_id`, `category_id`) VALUES (2, 2)" );
+
+$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://boren.nu/', 'Ryan', 0, 'http://boren.nu/feed/', '');");
+$wpdb->query( "INSERT INTO $wpdb->link2cat (`link_id`, `category_id`) VALUES (3, 2)" );
+
+$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://photomatt.net/', 'Matt', 0, 'http://xml.photomatt.net/feed/', '');");
+$wpdb->query( "INSERT INTO $wpdb->link2cat (`link_id`, `category_id`) VALUES (4, 2)" );
+
+$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://zed1.com/journalized/', 'Mike', 0, 'http://zed1.com/journalized/feed/', '');");
+$wpdb->query( "INSERT INTO $wpdb->link2cat (`link_id`, `category_id`) VALUES (5, 2)" );
+
+$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://www.alexking.org/', 'Alex', 0, 'http://www.alexking.org/blog/wp-rss2.php', '');");
+$wpdb->query( "INSERT INTO $wpdb->link2cat (`link_id`, `category_id`) VALUES (6, 2)" );
+
+$wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://dougal.gunters.org/', 'Dougal', 0, 'http://dougal.gunters.org/feed/', '');");
+$wpdb->query( "INSERT INTO $wpdb->link2cat (`link_id`, `category_id`) VALUES (7, 2)" );
+
 // First post
 $now = date('Y-m-d H:i:s');
 $now_gmt = gmdate('Y-m-d H:i:s');
Index: wp-admin/options-writing.php
===================================================================
--- wp-admin/options-writing.php	(revision 3568)
+++ wp-admin/options-writing.php	(working copy)
@@ -41,6 +41,18 @@
 ?>
 </select></td>
 </tr>
+<tr valign="top">
+<th scope="row"><?php _e('Default link category:') ?></th>
+<td><select name="default_link_category" id="default_link_category">
+<?php
+foreach ($categories as $category) :
+if ($category->cat_ID == get_settings('default_link_category')) $selected = " selected='selected'";
+else $selected = '';
+echo "\n\t<option value='$category->cat_ID' $selected>$category->cat_name</option>";
+endforeach;
+?>
+</select></td>
+</tr>
 </table>
 
 <fieldset class="options">
@@ -90,7 +102,7 @@
 
 <p class="submit">
 <input type="hidden" name="action" value="update" /> 
-<input type="hidden" name="page_options" value="default_post_edit_rows,use_smilies,rich_editing,ping_sites,mailserver_url,mailserver_port,mailserver_login,mailserver_pass,default_category,default_email_category,use_balanceTags" /> 
+<input type="hidden" name="page_options" value="default_post_edit_rows,use_smilies,rich_editing,ping_sites,mailserver_url,mailserver_port,mailserver_login,mailserver_pass,default_category,default_email_category,use_balanceTags,default_link_category" /> 
 <input type="submit" name="Submit" value="<?php _e('Update Options &raquo;') ?>" /> 
 </p>
 </form> 
Index: wp-admin/upgrade-schema.php
===================================================================
--- wp-admin/upgrade-schema.php	(revision 3568)
+++ wp-admin/upgrade-schema.php	(working copy)
@@ -8,6 +8,8 @@
   category_description longtext NOT NULL,
   category_parent bigint(20) NOT NULL default '0',
   category_count bigint(20) NOT NULL default '0',
+  link_count bigint(20) NOT NULL default '0',
+  links_visible tinyint(1) NOT NULL default '1',
   PRIMARY KEY  (cat_ID),
   KEY category_nicename (category_nicename)
 );
@@ -31,6 +33,13 @@
   KEY comment_approved (comment_approved),
   KEY comment_post_ID (comment_post_ID)
 );
+CREATE TABLE $wpdb->link2cat (
+  rel_id bigint(20) NOT NULL auto_increment,
+  link_id bigint(20) NOT NULL default '0',
+  category_id bigint(20) NOT NULL default '0',
+  PRIMARY KEY  (rel_id),
+  KEY link_id (link_id,category_id)
+);
 CREATE TABLE $wpdb->linkcategories (
   cat_id bigint(20) NOT NULL auto_increment,
   cat_name tinytext NOT NULL,
@@ -231,6 +240,7 @@
 	}
 	// 2.1
 	add_option('blog_public', 1);
+	add_option('default_link_category', 2);
 
 	// Delete unused options
 	$unusedoptions = array ('blodotgsping_url', 'bodyterminator', 'emailtestonly', 'phoneemail_separator', 'smilies_directory', 'subjectprefix', 'use_bbcode', 'use_blodotgsping', 'use_phoneemail', 'use_quicktags', 'use_weblogsping', 'weblogs_cache_file', 'use_preview', 'use_htmltrans', 'smilies_directory', 'fileupload_allowedusers', 'use_phoneemail', 'default_post_status', 'default_post_category', 'archive_mode', 'time_difference', 'links_minadminlevel', 'links_use_adminlevels', 'links_rating_type', 'links_rating_char', 'links_rating_ignore_zero', 'links_rating_single_image', 'links_rating_image0', 'links_rating_image1', 'links_rating_image2', 'links_rating_image3', 'links_rating_image4', 'links_rating_image5', 'links_rating_image6', 'links_rating_image7', 'links_rating_image8', 'links_rating_image9', 'weblogs_cacheminutes', 'comment_allowed_tags', 'search_engine_friendly_urls', 'default_geourl_lat', 'default_geourl_lon', 'use_default_geourl', 'weblogs_xml_url', 'new_users_can_blog');
