Index: /trunk/admin/configuration.php
===================================================================
--- /trunk/admin/configuration.php	(revision 17301)
+++ /trunk/admin/configuration.php	(revision 17302)
@@ -107,5 +107,5 @@
     'privacy_level',
   );
-  
+
 // image order management
 $sort_fields = array(
@@ -127,5 +127,5 @@
   'rank ASC'            => l10n('Manual sort order'),
   );
-  
+
 $comments_order = array(
   'ASC' => l10n('Show oldest comments first'),
@@ -142,5 +142,5 @@
   {
     case 'main' :
-    {      
+    {
       if ( !isset($conf['order_by_custom']) and !isset($conf['order_by_inside_category_custom']) )
       {
@@ -159,5 +159,5 @@
             // limit to the number of available parameters
             $order_by = $order_by_inside_category = array_slice($_POST['order_by'], 0, ceil(count($sort_fields)/2));
-            
+
             // there is no rank outside categories
             if ( ($i = array_search('rank ASC', $order_by)) !== false)
@@ -165,5 +165,5 @@
               unset($order_by[$i]);
             }
-            
+
             // must define a default order_by if user want to order by rank only
             if ( count($order_by) == 0 )
@@ -171,5 +171,5 @@
               $order_by = array('id ASC');
             }
-            
+
             $_POST['order_by'] = 'ORDER BY '.implode(', ', $order_by);
             $_POST['order_by_inside_category'] = 'ORDER BY '.implode(', ', $order_by_inside_category);
@@ -181,5 +181,5 @@
         }
       }
-      
+
       foreach( $main_checkboxes as $checkbox)
       {
@@ -190,10 +190,10 @@
     case 'watermark' :
     {
-      include(PHPWG_ROOT_PATH.'admin/include/configuration_watermark_process.inc.php');        
+      include(PHPWG_ROOT_PATH.'admin/include/configuration_watermark_process.inc.php');
       break;
     }
     case 'sizes' :
     {
-      include(PHPWG_ROOT_PATH.'admin/include/configuration_sizes_process.inc.php');        
+      include(PHPWG_ROOT_PATH.'admin/include/configuration_sizes_process.inc.php');
       break;
     }
@@ -303,6 +303,6 @@
 {
   case 'main' :
-  {   
-    
+  {
+
     function order_by_is_local()
     {
@@ -312,13 +312,13 @@
         @include(PHPWG_ROOT_PATH.PWG_LOCAL_DIR. 'config/config.inc.php');
       }
-      
+
       return isset($conf['order_by']) or isset($conf['order_by_inside_category']);
     }
-    
+
     if (order_by_is_local())
     {
       array_push($page['warnings'], l10n('You have specified <i>$conf[\'order_by\']</i> in your local configuration file, this parameter in deprecated, please remove it or rename it into <i>$conf[\'order_by_custom\']</i> !'));
     }
-    
+
     if ( isset($conf['order_by_custom']) or isset($conf['order_by_inside_category_custom']) )
     {
@@ -333,5 +333,5 @@
       $order_by = explode(', ', $order_by);
     }
-  
+
     $template->assign(
       'main',
@@ -441,5 +441,5 @@
           )
         );
-      
+
       foreach ($sizes_checkboxes as $checkbox)
       {
@@ -452,5 +452,5 @@
           );
       }
-      
+
       // derivatives = multiple size
       $enabled = ImageStdParams::get_defined_type_map();
@@ -465,8 +465,8 @@
       {
         $tpl_var = array();
-        
+
         $tpl_var['must_square'] = ($type==IMG_SQUARE ? true : false);
         $tpl_var['must_enable'] = ($type==IMG_SQUARE || $type==IMG_THUMB)? true : false;
-        
+
         if ($params = @$enabled[$type])
         {
@@ -478,5 +478,5 @@
           $params=@$disabled[$type];
         }
-        
+
         if ($params)
         {
@@ -496,4 +496,12 @@
       $template->assign('derivatives', $tpl_vars);
       $template->assign('resize_quality', ImageStdParams::$quality);
+
+      $tpl_vars = array();
+      $now = time();
+      foreach(ImageStdParams::$custom as $custom=>$time)
+      {
+        $tpl_vars[$custom] = ($now-$time<=24*3600) ? l10n('today') : time_since($time, 'day');
+      }
+      $template->assign('custom_derivatives', $tpl_vars);
     }
 
@@ -547,10 +555,10 @@
         $position = 'bottomright';
       }
-      
+
       if ($wm->xrepeat != 0)
       {
         $position = 'custom';
       }
-    
+
       $template->assign(
         'watermark',
@@ -567,5 +575,5 @@
         );
     }
-    
+
     break;
   }
Index: /trunk/admin/include/configuration_sizes_process.inc.php
===================================================================
--- /trunk/admin/include/configuration_sizes_process.inc.php	(revision 17301)
+++ /trunk/admin/include/configuration_sizes_process.inc.php	(revision 17302)
@@ -229,4 +229,13 @@
   }
 
+  foreach( array_keys(ImageStdParams::$custom) as $custom)
+  {
+    if (isset($_POST['delete_custom_derivative_'.$custom]))
+    {
+      $changed_types[] = $custom;
+      unset(ImageStdParams::$custom[$custom]);
+    }
+  }
+
   ImageStdParams::set_and_save($enabled_by);
   if (count($disabled) == 0)
Index: /trunk/admin/include/configuration_watermark_process.inc.php
===================================================================
--- /trunk/admin/include/configuration_watermark_process.inc.php	(revision 17301)
+++ /trunk/admin/include/configuration_watermark_process.inc.php	(revision 17302)
@@ -49,8 +49,8 @@
 
     $new_name = get_filename_wo_extension($_FILES['watermarkImage']['name']).'.png';
-    $file_path = $upload_dir.'/'.$new_name; 
-  
+    $file_path = $upload_dir.'/'.$new_name;
+
     move_uploaded_file($_FILES['watermarkImage']['tmp_name'], $file_path);
-    
+
     $pwatermark['file'] = substr($file_path, strlen(PHPWG_ROOT_PATH));
   }
@@ -135,5 +135,5 @@
   // do we have to regenerate the derivatives (and which types)?
   $changed_types = array();
-  
+
   foreach (ImageStdParams::get_defined_type_map() as $type => $params)
   {
@@ -141,6 +141,17 @@
     ImageStdParams::apply_global($params);
 
-    if ($params->use_watermark != $old_use_watermark
-        or $params->use_watermark and $watermark_changed)
+    $changed = $params->use_watermark != $old_use_watermark;
+    if (!$changed and $params->use_watermark)
+    {
+      $changed = $watermark_changed;
+    }
+    if (!$changed and $params->use_watermark)
+    {
+      // if thresholds change and before/after the threshold is lower than the corresponding derivative side -> some derivatives might switch the watermark
+      $changed |= $watermark->min_size[0]!=$old_watermark->min_size[0] and ($watermark->min_size[0]<$params->max_width() or $old_watermark->min_size[0]<$params->max_width());
+      $changed |= $watermark->min_size[1]!=$old_watermark->min_size[1] and ($watermark->min_size[1]<$params->max_height() or $old_watermark->min_size[1]<$params->max_height());
+    }
+
+    if ($changed)
     {
       $params->last_mod_time = time();
Index: /trunk/admin/include/functions.php
===================================================================
--- /trunk/admin/include/functions.php	(revision 17301)
+++ /trunk/admin/include/functions.php	(revision 17302)
@@ -1570,5 +1570,5 @@
     return false;
   }
-  
+
   // let's first break links with all albums but their "storage album"
   $query = '
@@ -1580,5 +1580,5 @@
 ;';
   pwg_query($query);
-    
+
   if (is_array($categories) and count($categories) > 0)
   {
@@ -2277,6 +2277,5 @@
 function clear_derivative_cache($types='all')
 {
-  $pattern='#.*-';
-  if ($types == 'all')
+  if ($types === 'all')
   {
     $types = ImageStdParams::get_all_types();
@@ -2288,19 +2287,33 @@
   }
 
+  for ($i=0; $i<count($types); $i++)
+  {
+    $type = $types[$i];
+    if ($type == IMG_CUSTOM)
+    {
+      $type = derivative_to_url($type).'[a-zA-Z0-9]+';
+    }
+    elseif (in_array($type, ImageStdParams::get_all_types()))
+    {
+      $type = derivative_to_url($type);
+    }
+    else
+    {//assume a custom type
+      $type = derivative_to_url(IMG_CUSTOM).'_'.$type;
+    }
+    $types[$i] = $type;
+  }
+
+  $pattern='#.*-';
   if (count($types)>1)
   {
-    $type_urls = array();
-    foreach($types as $dtype)
-    {
-      $type_urls[] = derivative_to_url($dtype);
-    }
-    $pattern .= '(' . implode('|',$type_urls) . ')';
+    $pattern .= '(' . implode('|',$types) . ')';
   }
   else
   {
-    $pattern .= derivative_to_url($types[0]);
-  }
-
-  $pattern.='(_[a-zA-Z0-9]+)*\.[a-zA-Z0-9]{3,4}$#';
+    $pattern .= $types[0];
+  }
+  $pattern.='\.[a-zA-Z0-9]{3,4}$#';
+
   if ($contents = @opendir(PHPWG_ROOT_PATH.PWG_DERIVATIVE_DIR))
   {
Index: /trunk/admin/themes/default/template/configuration.tpl
===================================================================
--- /trunk/admin/themes/default/template/configuration.tpl	(revision 17301)
+++ /trunk/admin/themes/default/template/configuration.tpl	(revision 17302)
@@ -455,4 +455,13 @@
   <a href="{$F_ACTION}&action=restore_settings" onclick="return confirm('{'Are you sure?'|@translate|@escape:javascript}');">{'Reset to default values'|@translate}</a>
 </p>
+
+{if !empty($custom_derivatives)}
+<fieldset class="sizeDetails"><legend>{'custom'|@translate}</legend><table style="margin:0">
+{foreach from=$custom_derivatives item=time key=custom}
+<tr><td><label><input type="checkbox" name="delete_custom_derivative_{$custom}"> {'Delete'|@translate} {$custom} ({'Last hit'|@translate}: {$time})</label></td></tr>
+{/foreach}
+</table></fieldset>
+{/if}
+
 </fieldset>
 {/if}
Index: /trunk/themes/default/template/picture.tpl
===================================================================
--- /trunk/themes/default/template/picture.tpl	(revision 17301)
+++ /trunk/themes/default/template/picture.tpl	(revision 17302)
@@ -19,5 +19,5 @@
 {include file='picture_nav_buttons.tpl'|@get_extent:'picture_nav_buttons'}
 <div class="actionButtons">
-{if count($current.unique_derivatives)>1}
+{if isset($current.unique_derivatives) && count($current.unique_derivatives)>1}
 {footer_script require='jquery'}{literal}
 function changeImgSrc(url,typeSave,typeMap)
