function foxyshop_custom_sort()
{
    global $wpdb, $product;
    $parentID = 0;
    $success = "";
    //Reset Order
    if (isset($_GET['upgrade_menu_order'])) {
        foxyshop_upgrade_menu_order();
    }
    if (isset($_POST['submit_new_product_order'])) {
        if (check_admin_referer('update-foxyshop-sorting-options')) {
            $success = foxyshop_update_order();
        }
    } elseif (isset($_POST['revert_product_order'])) {
        if (check_admin_referer('update-foxyshop-sorting-options')) {
            $success = foxyshop_revert_order();
        }
    }
    ?>

	<div class="wrap">
	<div class="icon32" id="icon-tools"><br></div>
	<h2><?php 
    echo sprintf(__('Custom %s Order'), FOXYSHOP_PRODUCT_NAME_SINGULAR);
    ?>
</h2>
	<?php 
    if ($success) {
        echo $success;
    }
    ?>

	<?php 
    $product_categories = get_terms('foxyshop_categories', 'hide_empty=0&hierarchical=0&orderby=name&order=ASC');
    $categoryID = 0;
    if (isset($_REQUEST['categoryID'])) {
        $categoryID = $_REQUEST['categoryID'];
    }
    if ($product_categories) {
        echo '<p>' . sprintf(__("Select a category from the drop down to order the %s in that category.", 'foxyshop'), strtolower(FOXYSHOP_PRODUCT_NAME_PLURAL)) . "</p>\n";
        echo '<form name="form_product_category_order" method="post" action="edit.php?post_type=foxyshop_product&page=foxyshop_custom_sort">';
        echo '<select name="categoryID" id="categoryID">' . "\n";
        echo '<option value="0"' . ($categoryID == 0 ? ' selected="selected"' : '') . '>' . __('All', 'foxyshop') . ' ' . FOXYSHOP_PRODUCT_NAME_PLURAL . '</option>' . "\n";
        foreach ($product_categories as $cat) {
            echo '<option value="' . esc_attr($cat->term_id) . '"' . ($categoryID == $cat->term_id ? ' selected="selected"' : '') . '>' . esc_html($cat->name) . ' (' . $cat->count . ')' . '</option>' . "\n";
        }
        echo '</select>' . "\n";
        echo '<input type="submit" name="btnSubPages" class="button" id="btnSubPages" value="' . __('Select Category', 'foxyshop') . '" /></form>';
    } else {
        $categoryID = 0;
    }
    if ($categoryID >= 0) {
        if ($categoryID > 0) {
            $term = get_term_by('id', $categoryID, "foxyshop_categories");
            $current_category_name = $term->name;
            $args = array('post_type' => 'foxyshop_product', 'foxyshop_categories' => $term->slug, 'posts_per_page' => -1, 'orderby' => "meta_value_num", "meta_key" => "_foxyshop_menu_order_" . $categoryID, 'order' => "ASC", 'post__not_in' => foxyshop_hide_children_array($categoryID));
        } else {
            $current_category_name = __("All", 'foxyshop') . ' ' . FOXYSHOP_PRODUCT_NAME_PLURAL;
            $args = array('post_type' => 'foxyshop_product', 'numberposts' => -1, 'orderby' => "menu_order", 'order' => "ASC");
        }
        $product_list = get_posts($args);
        if ($product_list) {
            echo '<h3>' . $current_category_name . '</h3>' . "\n";
            echo '<p>' . sprintf(__('Drag %s to the preferred order and then click the Save button at the bottom of the page.', 'foxyshop'), strtolower(FOXYSHOP_PRODUCT_NAME_PLURAL)) . '</p>';
            echo '<form name="form_product_order" method="post" action="">' . "\n";
            echo '<ul id="foxyshop_product_order_list" class="foxyshop_sort_list">' . "\n";
            foreach ($product_list as $prod) {
                $product = foxyshop_setup_product($prod);
                if ($categoryID == 0) {
                    $current_count = $prod->menu_order;
                } else {
                    $current_count = (int) get_post_meta($prod->ID, "_foxyshop_menu_order_" . $categoryID, true);
                }
                echo '<li id="id_' . $prod->ID . '" class="lineitem">';
                echo '<img src="' . foxyshop_get_main_image() . '" />';
                echo '<h4>' . $prod->post_title . '</h4>' . "\n";
                echo foxyshop_price();
                echo '<div class="counter">' . ($current_count + 1) . '</div>';
                echo '<div style="clear: both; height: 1px;"></div>' . "\n";
                echo '</li>' . "\n";
            }
            echo '</ul>' . "\n";
            ?>
			<div style="width: 90%; height: 100px;">
				<input type="submit" name="submit_new_product_order" id="submit_new_product_order" class="button-primary" value="<?php 
            _e('Save Custom Order', 'foxyshop');
            ?>
" onclick="javascript:orderPages(); return true;" />&nbsp;&nbsp;<strong id="updateText"></strong>
				<input type="submit" name="revert_product_order" id="revert_product_order" class="button" style="float: right;" value="<?php 
            _e('Revert To Original', 'foxyshop');
            ?>
" onclick="javascript:orderPages(); return true;" />
			</div>
			<input type="hidden" id="foxyshop_product_order_value" name="foxyshop_product_order_value" />
			<input type="hidden" id="hdnParentID" name="hdnParentID" value="<?php 
            echo $parentID;
            ?>
" />
			<input type="hidden" id="categoryID" name="categoryID" value="<?php 
            echo $categoryID;
            ?>
" />
			<?php 
            wp_nonce_field('update-foxyshop-sorting-options');
            ?>
			</form>
			<?php 
        } else {
            echo '<p><em>' . sprintf(__('No %s Found For This Category.', 'foxyshop'), FOXYSHOP_PRODUCT_NAME_PLURAL) . '</em></p>';
            echo '<p><a href="edit.php?post_type=foxyshop_product&amp;page=foxyshop_custom_sort&amp;upgrade_menu_order=1&amp;categoryID=' . $categoryID . '">Missing products? Click here.</a></p>';
        }
    }
    ?>

</div>

<script type="text/javascript">
function foxyshop_custom_order_load_event(){
	jQuery("#foxyshop_product_order_list").sortable({
		placeholder: "sortable-placeholder",
		revert: false,
		tolerance: "pointer",
		update: function() {
			var counter = 1;
			jQuery("#foxyshop_product_order_list li").each(function() {
				jQuery(this).find('.counter').html(counter);
				counter++;
			});
		}
	});
};
addLoadEvent(foxyshop_custom_order_load_event);
function orderPages() {
	jQuery("#updateText").html("<?php 
    echo sprintf(__('Updating %s Order...', 'foxyshop'), FOXYSHOP_PRODUCT_NAME_SINGULAR);
    ?>
");
	jQuery("#foxyshop_product_order_value").val(jQuery("#foxyshop_product_order_list").sortable("toArray"));
}
</script>
<?php 
}
function foxyshop_featured_category($categoryName, $showAddToCart = false, $showMoreDetails = false, $showMax = -1, $simpleList = false)
{
    global $product;
    $term = get_term_by('slug', $categoryName, "foxyshop_categories");
    $currentCategorySlug = $term->slug;
    $currentCategoryID = $term->term_id;
    $args = array('post_type' => 'foxyshop_product', "foxyshop_categories" => $currentCategorySlug, 'numberposts' => $showMax);
    $args = array_merge($args, foxyshop_sort_order_array());
    $args = array_merge($args, foxyshop_hide_children_array($currentCategoryID));
    echo '<ul class="foxyshop_featured_product_list' . ($simpleList ? "_simple" : "") . '">';
    $featuredlist = get_posts($args);
    foreach ($featuredlist as $featuredprod) {
        $product = foxyshop_setup_product($featuredprod);
        if ($product['hide_product']) {
            continue;
        }
        if ($simpleList) {
            $simplewrite = '<li><a href="' . $product['url'] . '">' . apply_filters('the_title', $product['name']) . '</a></li>' . "\n";
            echo apply_filters("foxyshop_featured_category_simple", $simplewrite, $product);
        } else {
            $thumbnailSRC = foxyshop_get_main_image("thumbnail");
            $write = '<li class="foxyshop_product_box">' . "\n";
            $write .= '<div class="foxyshop_product_image">';
            $write .= '<a href="' . $product['url'] . '"><img src="' . $thumbnailSRC . '" alt="' . esc_attr($product['name']) . '" class="foxyshop_main_image" /></a>';
            $write .= "</div>\n";
            $write .= '<div class="foxyshop_product_info">';
            $write .= '<h2><a href="' . $product['url'] . '">' . apply_filters('the_title', $product['name']) . '</a></h2>';
            $write .= foxyshop_price(0, 0);
            if ($showMoreDetails) {
                $write .= '<a href="' . $product['url'] . '" class="foxyshop_button">' . __('More Details') . '</a>';
            }
            if ($showAddToCart) {
                $write .= '<a href="' . foxyshop_product_link("", true) . '" class="foxyshop_button">' . __('Add To Cart') . '</a>';
            }
            $write .= "</div>\n";
            $write .= '<div class="clr"></div>';
            $write .= "</li>\n";
            echo apply_filters("foxyshop_featured_category_html", $write, $product);
        }
    }
    echo "</ul><div class=\"clr\"></div>\n";
}
$term = get_term_by('slug', $foxyshop_category_slug, "foxyshop_categories");
$currentCategoryName = $term->name;
$currentCategoryDescription = $term->description;
$currentCategorySlug = $term->slug;
$currentCategoryID = $term->term_id;
//Write Category Title (if you want the title in, just uncomment line below)
//echo '<h1 id="foxyshop_category_title">' . str_replace("_","",$currentCategoryName) . '</h1>'."\n";
//If there's a category description, write it here
if ($currentCategoryDescription) {
    echo '<p>' . $currentCategoryDescription . '</p>' . "\n";
}
//Run the query for all products in this category
//Note that the widget displays ALL products since pagination isn't possible
$args = array('post_type' => 'foxyshop_product', "foxyshop_categories" => $currentCategorySlug, 'post_status' => 'publish', 'posts_per_page' => -1);
$args = array_merge($args, foxyshop_sort_order_array());
$args = array_merge($args, foxyshop_hide_children_array($currentCategoryID));
$category_contents = get_posts($args);
echo '<ul class="foxyshop_product_list">';
foreach ($category_contents as $post) {
    setup_postdata($post);
    //Product Display
    foxyshop_include('product-loop');
}
echo '</ul>';
?>
</div>
<?php 
foxyshop_include('footer');
?>

<script type="text/javascript">