--- template.php_original	2007-01-17 18:40:25.000000000 +0100
+++ template.php	2008-02-29 18:48:46.000000000 +0100
@@ -73,3 +73,53 @@ function phptemplate_menu_local_tasks() 
 
   return $output;
 }
+
+
+/**
+ * Returns CSS code that sets a random header image.
+ * The function searches the images/headers directory for 
+ * jpg, png or gif images and returns CSS to display this 
+ * image in the header region.
+ */
+function randomheaderimage() {
+  $debug = true;
+
+  $image = base_path() . path_to_theme() . '/images/headers/';
+
+  // Open the images/headers dir and get a random image
+  $path = path_to_theme() . '/images/headers/';
+  if(($dir = @dir($path)) == false) {
+    return $debug ? '<!-- k2 random header image: Cannot open dir "'. $path .'". -->' : '';
+  }
+  $ext_match = 'jpg|png|gif';
+  $files = array();
+  while(($file = $dir->read()) !== false) {
+    if(preg_match('/\.(' . $ext_match . ')$/i', $file)) {
+      $files[] = $file;
+    }
+  }
+  $dir->close();
+
+  // Choose a random image
+  $size = count($files);
+  if ($size > 1) {
+    $image .= $files[rand(0, $size - 1)];
+  } elseif($size==1) { 
+    $image .= $files[0];
+  } else {
+    return $debug ? '<!-- k2 random header image: Found no image in dir "'. $path .'". -->' : '';
+  }
+
+  // return css that changes the header image
+  $output = '';
+  $output .= '    <style type="text/css">'."\n";
+  $output .= '        #header {'."\n";
+  $output .= '            background: transparent url(\'' . $image . '\') repeat scroll 0%;'."\n";
+  $output .= '        }'."\n";
+  $output .= '    </style>'."\n";
+
+  return $output;
+}
+
+
+?>
