<?php


require_once('scripts/sb_functions.php');
global $logged_in;
$logged_in = logged_in( false, true );
read_config();
sb_language( 'index' );

html_header('',false);



function page_content() {

  //    dir .... subdir with images
  //    album .. photo album (use gallery extension)

  $dir = 'images';
  if (isset($_REQUEST['dir'])) $dir=$_REQUEST['dir'];

  // album specified ... read picture list from album

  if (isset($_REQUEST['album'])){
    $album = $_REQUEST['album'];
    if (file_exists('scripts/sb_gallery.php')){
      include_once('scripts/sb_gallery.php');
      include_once "scripts/sb_gallery_config.php";
      $files = getPictureList($album);
      $dir = makeAlbumPath($album);
    }
  }

  // otherwise: read files from dir

  elseif (is_dir($dir)){
    if ($dh = opendir($dir)){
      while (false !== ($filename = readdir($dh))) {
	$tempArray = explode(".", $filename);
	/* Add image file types in the code below */
	if($tempArray[1] == "jpg" || $tempArray[1] == "JPG" ||
	   $tempArray[1] == "jpeg" ||  $tempArray[1] == "JPEG" ||
	   $tempArray[1] == "gif" || $tempArray[1] == "GIF" ||
	   $tempArray[1] == "png" ||
	   $tempArray[1] == "tif" || 
	   $tempArray[1] == "bmp"){
	  $files[] = $filename;
	}
      }
    }
  }


  /* Generate a random number */
  $nooffildi = count($files);
  $nooffiles = ($nooffildi-1);
  srand((double)microtime()*1000000);
  $randnum = rand(0,$nooffiles);

  $entry_array = array();
  if (isset($_REQUEST['album'])){
    $album = $_REQUEST['album'];
    $pic = getPictureURL($album,$files[$randnum]);
    if (isset($_REQUEST['thumb'])){
      include_once "gallery/sb_thumbnails.php";
      if (validThumbnailExists($album.'/'.$files[$randnum])){
	$pic=getThumbnailURL($album.'/'.$files[$randnum]);
      }
    }
    elseif (validScaledExists($pic)){
      $pic=getScaledURL($pic);
    }
    $files[$randnum]=basename($pic);
    $dir=dirname($pic);

    $entry_array['subject'] = '<a href="gallery.php?page=1&album='.$album.'">';
    $entry_array['subject'] .= "Random picture from <i>'$album'</i></a>";
  }
  else{
    $entry_array['subject'] = "Random picture from <i>'$dir'</i>";
  }
  $entry_array['subject'] .= ' [<a href="">reload</a>]';


  if (function_exists('getimagesize')){
    $elements = getimagesize("$dir/$files[$randnum]");
    $width = $elements[0];
    $height = $elements[1];
  }
  if ($width>500){
    $entry_array['entry'] = "<div align='center'><a href=''><IMG SRC='$dir/$files[$randnum]' ALT='$files[$randnum]' BORDER='0' width='500'></a></div>";
  }
  else{
    $entry_array['entry'] = "<div align='center'><a href=''><IMG SRC='$dir/$files[$randnum]' ALT='$files[$randnum]' BORDER='0'></a></div>";
  }
  echo( theme_staticentry( $entry_array ) );

}
		
// Generate Page
// -----------
theme_pagelayout();
html_footer();

?>
