Index: /trunk/admin/element_set_ranks.php
===================================================================
--- /trunk/admin/element_set_ranks.php	(revision 5758)
+++ /trunk/admin/element_set_ranks.php	(revision 5759)
@@ -85,4 +85,7 @@
 // +-----------------------------------------------------------------------+
 
+$image_order_choices = array('default', 'rank', 'user_define');
+$image_order_choice = 'default';
+
 if (isset($_POST['submit']))
 {
@@ -98,4 +101,38 @@
     l10n('Images manual order was saved')
     );
+
+  $image_order = null;
+  if (!empty($_POST['image_order_choice']) 
+      && in_array($_POST['image_order_choice'], $image_order_choices))
+  {
+    $image_order_choice = $_POST['image_order_choice'];
+  }
+      
+  if ($image_order_choice=='user_define')
+  {
+    for ($i=1; $i<=3; $i++)
+    {
+      if ( !empty($_POST['order_field_'.$i]) )
+      {
+        if (! empty($image_order) )
+        {
+          $image_order .= ',';
+        }
+        $image_order .= $_POST['order_field_'.$i];
+        if ($_POST['order_direction_'.$i]=='DESC')
+        {
+          $image_order .= ' DESC';
+        }
+      }
+    }
+  }
+  elseif ($image_order_choice=='rank')
+  {
+    $image_order = 'rank';
+  }
+  $query = '
+UPDATE '.CATEGORIES_TABLE.' SET image_order=\''.$image_order.'\'
+  WHERE id='.$page['category_id'];
+  pwg_query($query);
 }
 
@@ -109,12 +146,19 @@
 $base_url = get_root_url().'admin.php';
 
-// $form_action = $base_url.'?page=element_set_global';
-
 $query = '
-SELECT uppercats
+SELECT *
   FROM '.CATEGORIES_TABLE.'
   WHERE id = '.$page['category_id'].'
 ;';
 $category = pwg_db_fetch_assoc(pwg_query($query));
+
+if ($category['image_order']=='rank')
+{
+  $image_order_choice = 'rank';
+}
+elseif ($category['image_order']!='') 
+{
+  $image_order_choice = 'user_define';
+}
 
 // Navigation path
@@ -175,4 +219,54 @@
 }
 
+// image order management
+$sort_fields = array(
+  '' => '',
+  'date_creation' => l10n('Creation date'),
+  'date_available' => l10n('Post date'),
+  'average_rate' => l10n('Average rate'),
+  'hit' => l10n('Most visited'),
+  'file' => l10n('File name'),
+  'id' => 'Id',
+  'rank' => l10n('Rank'),
+  );
+
+$sort_directions = array(
+  'ASC' => l10n('ascending'),
+  'DESC' => l10n('descending'),
+  );
+
+$template->assign('image_order_field_options', $sort_fields);
+$template->assign('image_order_direction_options', $sort_directions);
+
+$matches = array();
+if ( !empty( $category['image_order'] ) )
+{
+  preg_match_all('/([a-z_]+) *(?:(asc|desc)(?:ending)?)? *(?:, *|$)/i',
+    $category['image_order'], $matches);
+}
+
+for ($i=0; $i<3; $i++) // 3 fields
+{
+  $tpl_image_order_select = array(
+      'ID' => $i+1,
+      'FIELD' => array(''),
+      'DIRECTION' => array('ASC'),
+    );
+
+  if ( isset($matches[1][$i]) )
+  {
+    $tpl_image_order_select['FIELD'] = array($matches[1][$i]);
+  }
+
+  if (isset($matches[2][$i]) and strcasecmp($matches[2][$i],'DESC')==0)
+  {
+    $tpl_image_order_select['DIRECTION'] = array('DESC');
+  }
+  $template->append( 'image_orders', $tpl_image_order_select);
+}
+
+$template->assign('image_order_choice', $image_order_choice);
+
+
 // +-----------------------------------------------------------------------+
 // |                          sending html code                            |
Index: /trunk/admin/themes/default/default-layout.css
===================================================================
--- /trunk/admin/themes/default/default-layout.css	(revision 5758)
+++ /trunk/admin/themes/default/default-layout.css	(revision 5759)
@@ -292,4 +292,8 @@
 	margin-top: 2em;
 	margin-bottom: 2em;
+}
+
+form p.field {
+  text-align: left;
 }
 
Index: /trunk/admin/themes/default/template/element_set_ranks.tpl
===================================================================
--- /trunk/admin/themes/default/template/element_set_ranks.tpl	(revision 5758)
+++ /trunk/admin/themes/default/template/element_set_ranks.tpl	(revision 5759)
@@ -2,5 +2,5 @@
 {known_script id="jquery.ui" src=$ROOT_URL|@cat:"themes/default/js/ui/packed/ui.core.packed.js" }
 {known_script id="jquery.ui.sortable" src=$ROOT_URL|@cat:"themes/default/js/ui/packed/ui.sortable.packed.js" }
-
+{html_head}
 {literal}
 <script type="text/javascript">
@@ -16,4 +16,5 @@
   .each(function() { $(this).attr('value', (i+1)*10)});
   });
+  $('#image_order_rank').attr('checked', true);
   }
   });
@@ -22,4 +23,5 @@
 </script>
 {/literal}
+{/html_head}
 
 <h2>{'Manage image ranks'|@translate}</h2>
@@ -47,6 +49,32 @@
     {/if}
   </fieldset>
+
+  <fieldset>
+    <legend>{'Sort order'|@translate}</legend>
+    <p class="field">
+      <input type="radio" name="image_order_choice" id="image_order_default" value="default"{if $image_order_choice=='default'} checked="checked"{/if}>
+      <label for="image_order_default">{'Use the default image sort order (defined in the configuration file)'|@translate}</label>
+    </p>
+    <p class="field">
+      <input type="radio" name="image_order_choice" id="image_order_rank" value="rank"{if $image_order_choice=='rank'} checked="checked"{/if}>
+      <label for="image_order_rank">{'By rank'|@translate}</label>
+    </p>
+    <p class="field">
+      <input type="radio" name="image_order_choice" id="image_order_user_define" value="user_define"{if $image_order_choice=='user_define'} checked="checked"{/if}>
+      <label for="image_order_user_define">{'Manual order'|@translate}</label>
+      {foreach from=$image_orders item=order}
+      <p class="field">
+        <select name="order_field_{$order.ID}">
+          {html_options options=$image_order_field_options selected=$order.FIELD }
+        </select>
+        <select name="order_direction_{$order.ID}">
+          {html_options options=$image_order_direction_options selected=$order.DIRECTION }
+        </select>      
+      </p>
+      {/foreach}
+  </fieldset>
   <p><input class="submit" type="submit" value="{'Submit'|@translate}" name="submit" {$TAG_INPUT_ENABLED}></p>
 </form>
+
 
 {else}
