<?php
  include 'includes/Global.php';
  
	function test_authorized_characters($teststring){
		$teststring=str_replace(" ", "%20", $teststring);
		$teststring=str_replace("á", "%E1", $teststring);
		$teststring=str_replace("í", "%ED", $teststring);
		$teststring=str_replace("ý", "%FD", $teststring);
		$teststring=str_replace("Ý", "%DD", $teststring);
//		$teststring=str_replace("í", "%ED", $teststring);
		
		return mysql_escape_string($teststring);
	}
  

  if (array_key_exists('Album', $_GET)) { //seznam fotek
    $Album = $_GET['Album'];
    echo '<br /><a href="Picture.php">Seznam Albumů</a> > 
    <a href="Picture.php?Album='.$Album.'">Seznam Fotek</a> <br /><br />';
    $sql = "SELECT * FROM pictures WHERE IDAlbum = '$Album'";
 //   echo $sql;
    $ID = $db->SQLCommand($sql);
    while($Line = mysql_fetch_array($ID)) {
      echo '<a href="Picture.php?Picture='.$Line['ID'].'" title="'.$Line['Date'].'">
      <img class="ViewPicture" alt="'.$Line['ViewFileName'].'" src="'.test_authorized_characters($UploadDirPictures.'/'.$Line['ViewFileName']).'"></a> ';
    }
  } else {
    if (array_key_exists('Picture', $_GET)) {  // jednotlivý obrázek
      $Picture = $_GET['Picture'];

      $sql = "SELECT IDAlbum FROM pictures WHERE ID = '$Picture'";
      $ID = $db->SQLCommand($sql);
      $Line = mysql_fetch_array($ID);
      echo '<br /><a href="Picture.php">Seznam Albumů</a> >
       <a href="Picture.php?Album='.$Line['IDAlbum'].'">Seznam Fotek</a> >
        <a href="Picture.php?Picture='.$Picture.'">Fotka</a> <br /> <br />';

      $sql = "SELECT * FROM pictures WHERE ID = '$Picture'";
      $ID = $db->SQLCommand($sql);
      while($Line = mysql_fetch_array($ID)) {
        echo ' <a target="_blank"  href="'.test_authorized_characters($UploadDirPictures.'/'.$Line['FileName']).'">
        <img alt="'.$Line['ViewFileName'].'" class="Picture" src="'.test_authorized_characters($UploadDirPictures.'/'.$Line['FileName']).'"></a><br />';
        //TODO: načítat automaticky zda je předešlý a následujíci, a přidat tam spíš jednotlivé odkazy <1,2,3,4 >
      //  echo '<A href="Picture.php?Picture='.$Last.'">Předešlý</a> <A href="Picture.php?Picture='.$Next.'">následující</a>';
        $Album = $Line['IDAlbum'];
   //     echo $Album;
      }
      
      $sql = "SELECT * FROM pictures WHERE IDAlbum = '$Album'";
      $ID = $db->SQLCommand($sql);
      while($Line = mysql_fetch_array($ID)) {
        if ($Line['ID'] == $Picture) {
          if (isset($Last))
            if ($Last <> '')
              echo '<A href="Picture.php?Picture='.$Last.'">Předešlý</a> ';
        } else {
          if (isset($Last))
            if ($Last == $Picture)
              echo '<A href="Picture.php?Picture='.$Line['ID'].'">Následující</a>';  
        }
          $Last = $Line['ID'];
      }

    } else {
  
    echo '<br /><a href="Picture.php">Seznam Albumů</a> <br /> <br />
    
    <table border="0">';
    
    $sql = "SELECT * FROM picturealbums";
    $ID = $db->SQLCommand($sql);
    while($Line = mysql_fetch_array($ID)) {
      $IDAlbum = $Line['ID'];
      $sql = "SELECT Count(ID) FROM pictures WHERE IDAlbum = '$IDAlbum'";
      $IDPictures = $db->SQLCommand($sql);
      $Pictures = mysql_fetch_row($IDPictures);
      
      echo '
      <tr>
      <td><a href="Picture.php?Album='.$Line['ID'].'" title="'.$Line['Name'].'">
          <img class="ViewAlbum" alt="'.$Line['FileImg'].'" src="'.test_authorized_characters($UploadDirPictures.'/'.$Line['FileImg']).'">
          </a>
      </td>
      <td>Název: <b>'.$Line['Name'].'</b><br />
      Popis: <b>'.$Line['Description'].'</b><br />
      Počet fotek: <b>'.$Pictures[0].'</b><br />
      Datum přidání: <b>'.$Line['Date'].'</b>  
      </td></tr>';
   //   echo $Line['Description'];
      } // konec while
    echo '      </table>';
    }  //konec else picture
  }  //konec else albumu
  
  ShowFooter();
?>
