<?php
  $thisPage="baum";
  include __DIR__ . "./../layout/header.php";
  include __DIR__ . "./../layout/navigation.php";

  include __DIR__ . "./../layout/css/navi.css";
	include __DIR__ . "./../layout/css/baum.css"; ?>
</head>
<?php
  global $anz_gen, $partner; $ladezeit;
  error_reporting(0);
  $root_fam = array();  // Stammbaum alle (nur männliche Familien)
?>
<body>
  <div class="container-fluid"><?php
// Anzeige gemäß Anzahl generationen - möglich sind 3, 4 oder 5 (alle)
    if ($anz_gen == 5) { // alle Stammväter mit parent_id = NULL?>
      <table>
        <tr>
          <td style="width:50px;"></td><?php
            $last_name = array();  // leeres array für Stammväter
            $last_name = ['alle'];   // erster Eintrag

            $array_stamm = $array_stamm['id'] = $array_stamm['name'] = array();
            $array_stamm['id'][0] = 0;
            $array_stamm['name'][0] = 'alle';

            $i = 0;
            foreach ($roots as $row)  {           // Jede Person mit parent = NULL (Stammvater)
              $i++;
              $last_name[] = $row->last_name;  // in array $last_name eintragen
              $array_stamm->id[$i] = $row->member_id;
              $array_stamm->name[$i] = $row->last_name;
            }
            $len = count($array_stamm->id);  // anzahl stammväter?>
          <td>
            <form name="stammvater" action="baum"  method="get">
              <strong>Stammvater:</strong><?php
              $i = 0;
              for ($i=0; $i<$len; $i++) {
                echo "&emsp;".$array_stamm->name[$i]." ";?>
                <input type="radio" name="baum" value="<?php echo $array_stamm->id[$i]; ?>" onchange="submit()"
                <?php if ($array_stamm->id[$i] == $baum) { echo "checked=checked"; } ?> /><?php
                } ?>
            </form>
          </td>
          <td>
            <form  action="<?php echo "?id=".$person->member_id; ?>">
              <select name="baum" onchange="submit()"  style="background-color:lightyellow;">
                <option value="null">Stammvater wählen<?php
                foreach ($person_man as $pers) {
                  if ($pers->member_id == $_SESSION['baum']) { ?>">
                    <option selected value=" <?php echo $pers->member_id; ?>" > <?php
                  } else { ?>
                    <option value="<?php echo $pers->member_id;?>"> <?php
                  }
                    echo $pers->last_name." ".$pers->first_name;?>
                  </option><?php
                } ?>
              </select>
            </form>
          </td>
          <td><strong>&nbsp;(Kinder nur bei männlichen Nachfahren)&nbsp; </strong></td>
          <!-- Checkbox zur Wahl der Anzahl Generationen -->
          <td style="width:30px;"> </td>
          <td>
            <form name="Generationen" action="baum"  method="get">
              <strong>Anzahl Generationen:</strong>
              &nbsp;&nbsp;<strong> 3 </strong>
              <input type="radio" name="anz" value="3" onchange="submit()"<?php
              if ($anz_gen == 3) { echo "checked=checked"; } ?>/>
              &nbsp;&nbsp;<strong> 4 </strong>
              <input type="radio" name="anz" value="4" onchange="submit()"<?php
              if ($anz_gen == 4) { echo "checked=checked"; } ?>/>
              &nbsp;&nbsp;<strong> alle </strong>
              <input type="radio" name="anz" value="5" onchange="submit()"<?php
              if ($anz_gen == 5) { echo "checked=checked"; } ?>/>
            </form>
          </td>
        </tr>
      </table>
    </div> <?php
  } // Ende für Generation (>= 5; alle)
//****************************** Baum mit allen Namen (root) *******************
//****************************** Anzahl = 5 ************************************
  if ($anz_gen >= 5) { ?>
    <style>
        ul { list-style-type: disc; color: red; font-size:16px; font-weight: bold; margin-left: 40px; }
/*        ol { list-style-type: upper-roman; font-weight: bold; }
        ol > li:first-child { color: blue; }
        ol > li:nth-child(2) { color: maroon; }
        ol > li:nth-child(3) { color: olive; }
        dl { font-size: 0.7em ; font-family: Verdana,Arial,sans-serif; }
*/
    </style>

    <script type="text/javascript">
      ddsmoothmenu.init({
        mainmenuid: "smoothmenu1", //menu DIV id
        orientation: 'h', //Horizontal or vertical menu: Set to "h" or "v"
        classname: 'ddsmoothmenu', //class added to menu's outer DIV
        customtheme: ["#1c5a80", "#482400"],
        contentsource: "markup" //"markup" or ["container_id", "path_to_menu_file"]
      })
    </script>

    <?php
    function submenu_h($persons, $member_id) {
      foreach ($persons as $pers) {
        if (($pers->father_id  == $member_id)) {
          $root_fam[] = $pers;      // Familienmitglied
        }
      }
      if (count($root_fam >= 0)) { ?>
        <ul><?php
          foreach ($root_fam as $row_2){
            $day = jahr($row_2->day_of_birth); ?>
            <li>
              <a href="familienblatt?id=<?php echo $row_2->member_id; ?>"><?php
                echo $day.": ".$row_2->first_name ?></a><?php
                if(submenu_h($persons,$row_2->member_id)=="")
                 { ?>
              </li>	<?php
                 }
          }?>
        </ul><?php
       }
    } // End submenu_h
    //*********************************************************************?>
    </br>

    <div id="smoothmenu1" class="ddsmoothmenu">
      <ul><?php
        $first = true;
        foreach ($root as $row)  {
          $day = jahr($row_2->day_of_birth);
          if ($first == true) { ?>
            <li>
              <a href="familienblatt?id=<?php echo $row->member_id; ?>"><?php
              echo $day.": ".$row->first_name." ".$row->last_name?></a><?php
            } else { ?>
              <a href="familienblatt?id=<?php echo $row->member_id; ?>"><?php
              echo $row->first_name."-".$day ?></a><?php
            }
            if(submenu_h($persons,$row->member_id)=="") {
              ?></li>	            	            <hr><?php
              }
        } ?>
      </ul>
      <br style="clear: left" />
    </div>
  </div><?php
} else {  // Ende anzahl generationen 5 - jetzt 3 oder 4 ?>

<!-- ***************************** Anzahl Generationen <5 **********************-->
  <?php

  function fill($pers, $gen, $p) { // fill tho boxes
    global $anz_gen, $partner, $ladezeit;  // Anzahl der Generationen pro Blatt
    if (!empty($pers)) {
      $id = $pers->member_id;
      if ($pers->gender == 1){$gen = "m";$img ="male.jpg";} else{$gen="f";$img ="female.jpg";}
      $birth = DateTime::createFromFormat('Y-m-d', $pers->day_of_birth);
      $death = DateTime::createFromFormat('Y-m-d', $pers->day_of_death);
      if ($pers->member_img != "") { $img = $pers->member_img;}

      $alter = alter($pers);
      if ($death->format('Y') >= 2) {
        $alter = alter($pers);
        $period = "<b>* </b>".$birth->format('Y')."<b>&nbsp;&nbsp;&nbsp;  † </b>".$death->format('Y')."";
      } else {
        $period = "<b>* </b>".$birth->format('Y')."  &nbsp;" ;
      }
      #$period = $period."&nbsp;&nbsp;&nbsp;&nbsp; (".alter($pers)." Jahre)";
      $period = $period."&nbsp;&nbsp;&nbsp;&nbsp;(".$alter ." Jahre)";

  // ************************* 4 Generationen anzeigen ***************************
      if ($anz_gen == 4 ) {?>
        <div class="box">
        <table>
          <tr style="width:100%;">
            <td colspan=3 style='text-align:left; '><?php
              echo "ID ".$pers->member_id." <b><img src='../../img/".$gen.".jpg'>";
              echo "<a href='baum?anz=4&id=".$pers->member_id."'>"." "
              .$pers->first_name." ".$pers->last_name."</a></b>";?>
            </td>
          </tr>
  <!-- ************************ hover *****************************************-->
          <tr>
            <td style="padding-left:2px; text-align:left; width:20%;" >
              <a href="familienblatt?id=<?php echo $id;?>" title="Familienblatt">
              <img src="../../photos/<?php echo $img;?>" border="0" alt="Bild" width="44" height="55" /></a>
            </td>

            <td class="hover" id="<?php echo "mehr_".$p;?>" style='text-align:left; width:100px;'><?php
              if ($pers->birth_name != "") {
                echo "geb. ".$pers->birth_name."</br> ";
              } else { echo "&nbsp;</br>";}
              if ($anz_gen == 4) { echo $period;} ?>
            </td>
          </tr>
        </table>
        <table class="mask">    <!-- zusätzlicher Block zeigt bei mouseover diese Details-->
          <tr>
            <td colspan="3" id="<?php echo "popup_".$p;?>">
              <?php echo "<strong>* </strong>".date_german($pers->day_of_birth)."
                in ".$pers->place_of_birth."</br>";

                if ($pers->day_of_death != "0000-00-00") {
                  echo "<strong>† </strong>".date_german($pers->day_of_death)."
                  in ".$pers->place_of_death."</br>";
                }?>
            </td>
          </tr>
        </table></div><?php
      } else {
// ****************** Anzeige 4 Generationen Ende ******************************
// ****************** Anzeige 3 Generationen Begin ***************************** ?>
      <table>
        <tr>
          <td colspan='2'><?php
            echo "<b><img src='../../img/".$gen.".jpg'>";
            echo "<a href='baum?anz=4&id=".$pers->member_id."'>"." "
            .$pers->first_name." ".$pers->last_name."</a></b>
          </td>
        </tr>
        <tr>
          <td colspan='2'>";
            if ($pers->birth_name != "") {
              echo "<p>geb. ".$pers->birth_name."</p>";} else {echo "<p> - </p>";
            }
            echo "<p><b>* </b>".$pers->day_of_birth." in ".$pers->place_of_birth."</br>";
            if ($pers->day_of_death != 0) {
              echo "<b>† </b>".$pers->day_of_death." in ".$pers->place_of_death."</p>";
            } else { echo "<b> † </b> - "; }?>
          </td>
        </tr>
      </table><?php
      }
// ************************ Ende Anzeige 3 Generationen ************************
// ************************ keine Daten eingetragen ****************************
    } else { // emty $pers?>
      <table>
        <tr>
          <td colspan='2'><?php
            echo "<b><img src='../../img/'u.jpg'>";
            echo "<b>no data</b>";?>
          </td>
        </tr>
      </table><?php
    }
  } // ende function fill for generationen alle (5)
//***************************** End functions ******************************** ?>

<!-- ********************** Kopfzeilen *****************************************-->
  <body>
    <div class="container-fluid">
      <table>
        <tr>
          <td><h1><?php echo $person->first_name." ".$person->last_name;?>
            &nbsp;&nbsp;&nbsp;&nbsp; - &nbsp;&nbsp;&nbsp;&nbsp;Partner: </b> <?php
            foreach ($partner as $part): ?>
              <a href="baum?anz=4&id=<?php echo $part->member_id; ?>">
              <?php echo $part->first_name." ".$part->last_name." | "; ?></a><?php
            endforeach;?><h1>
          </td>
          <td style="width:30px;"> </td>
          <td>
            <form  action="<?php echo "?id=".$person->member_id; ?>">
              <select name="id" onchange="submit()"  style="background-color:lightyellow;">
                <option value="null">Andere Person wählen<?php
                  foreach ($persons as $pers) { ?><?php
                    if ($pers->member_id == $_SESSION['id']) { ?>">
                      <option selected value=" <?php echo $pers->member_id; ?>" > <?php
                    } else { ?>
                      <option value="<?php echo $pers->member_id;?>"> <?php
                    }?><?php
                    echo $pers->last_name.", ".$pers->first_name;?>
                </option><?php
                  } ?>
              </select>
            </form>
          </td>
  <!-- Checkbox zur Wahl der Anzahl Generationen -->
          <td style="width:30px;"> </td>
          <td>
            <form name="Generationen" action="baum"  method="get">
              <strong>Anzahl Generationen &nbsp;3 </strong><input type="radio" name="anz" value="3" onchange="submit()"
              <?php if ($anz_gen == 3) { echo "checked=checked"; } ?>/>
              &nbsp;&nbsp;<strong> 4 </strong><input type="radio" name="anz" value="4" onchange="submit()"
              <?php if ($anz_gen == 4) { echo "checked=checked"; } ?>/>
              &nbsp;&nbsp;<strong> alle </strong><input type="radio" name="anz" value="5" onchange="submit()"
              <?php if ($anz_gen == 5) { echo "checked=checked"; } ?>/>
            </form>
          </td>
        </tr>
      </table>

      <p><b>Kinder: </b><?php
        foreach ($children as $child): ?>
          <a href="baum?anz=4&id=<?php echo $child->member_id; ?>">
          <?php echo $child->first_name." ".$child->last_name." | "; ?></a><?php
        endforeach;?></p>

      <p><b>Geschwister: </b><?php
        foreach ($siblings as $sibling): ?>
          <a href="baum?id=<?php echo $sibling->member_id; ?>">
          <?php echo $sibling->first_name." ".$sibling->last_name." | "; ?></a><?php
        endforeach;?>
      </p></br><?php
      if ($anz_gen ==  4 ){?>
        <p><strong><font color=blue>Hinweise zu dieser Webseite <br></font></strong></p>
        <ul>
          <li><b>Neuer Stammbaum:</b> Click auf den Namen der jeweiligen Person</li>
          <li><b>zum Familienblatt:</b> Click auf das Bild der jeweiligen Person</li>
          <li><b>Geburtsdaten:</b> Maus over Jahr zeigt das Datum</li>
        </ul><?php
      } ?>
    </div> <?php
//*************************** Ende Überschriften ********************************-->

    $boxheight = 70;                // in css fambox
    $boxwidth = 255;                // Weite der Box
    $gap = 30;                      // Abstand zwischen Boxen
    $distance_h = 80;               // Abstands-Höhe der Box
    $distance_w = $boxwidth + $gap; // Weite zur nächsten Box

    $len_hor = $gap/2;              // Länge horizentale Verbindung
    $top_0 = 170;                   // 1. Box von oben
    $left_0 = 55;                   // 1. Box von Links

    $left = $left_0;                // Variable für linke Position
    $left = $left + $distance_w;    // neue Position für Box links
    $left1 = $left - $len_hor;      // neue Position für Verbingung zur Box rechtss
    $left10 = $left1 - $len_hor;    // neue Position für Verbingung zur Box links

    $left2 = $left1 + $distance_w;
    $left20 = $left2 - $len_hor;

    $left3 = $left2 + $distance_w;
    $left30 = $left3 - $len_hor;

    $t1 = $top_0 + 2*$distance_h;
    $t11 = $top_0 + 4*$distance_h;

    $t2 = $top_0 + $distance_h;
    $t21 = $top_0 + $distance_h;   // aktuelle Höhe

    $t3 = $top_0 + $distance_h/2;

//************************** 4 Generationen anzeigen ****************************-->?>
<?php
  if ($anz_gen ==  4 ){ ?>
    <div class="container-fluid">
      // senkrechte Linien: man to woman
      <div class="line_h1" style="top:<?php echo $t1;?>px;left:<?php echo $left1;?>px;"></div><?php
      $t20 = $t2;
      for ($i=0; $i < 2; $i++) {?>
        <div class="line_h2" style="top:<?php echo $t20;?>px;left:<?php echo $left2;?>px;"></div><?php
        $t20 = $t20 + 4*$distance_h;
      }
      $t30 = $t3;
      for ($i=0; $i < 4; $i++) {?>
        <div class="line_h3" style="top:<?php echo $t30;?>px;left:<?php echo $left3;?>px;"></div><?php
        $t30 = $t30 + 2*$distance_h;
      }

      // Waagerechte Verbindungen nach rechts (to next Generation)
      $t10 = $t1;
      for ($i=0; $i < 2; $i++) {?>
        <div class="line_v1" style="top:<?php echo $t10;?>px;left:<?php echo $left1;?>px;"></div><?php
        $t10 = $t10 + 4*$distance_h;
      } ?>
      <!-- 1 waagerechter nach links-->
      <div class="line_v1" style="top:<?php echo $t11;?>px;left:<?php echo $left10;?>px;"></div>
      <!-- Verbindungen zur Generation 2 -> 4 waagerechte nach rechts--><?php
      $left21 = $left2;
      for ($i=0; $i < 4; $i++) {?>
        <div class="line_v1" style="top:<?php echo $t21;?>px;left:<?php echo $left21;?>px;"></div><?php
        $t21 = $t21 + 2*$distance_h;
      } ?>
      <!-- 2 waagerechte nach links --><?php
      $t10 = $t1;
      for ($i=0; $i < 2; $i++) {?>
        <div class="line_v1" style="top:<?php echo $t10;?>px;left:<?php echo $left20;?>px;"></div><?php
        $t10 = $t10 + 4*$distance_h;
      } ?>
      <!-- Verbindungen zur Generation 3 *** 4 waagerechte nach links --><?php
      $t21 = $t2;
      for ($i=0; $i < 4; $i++) {?>
        <div class="line_v1" style="top:<?php echo $t21;?>px;left:<?php echo $left30;?>px;"></div><?php
        $t21 = $t21 + 2*$distance_h;
      } ?>
      <!-- 8 waagerechte nach rechts-->
      <?php
      $t31 = $t3;
      $left31 = $left3;
      for ($i=0; $i < 8; $i++) {?>
        <div class="line_v1" style="top:<?php echo $t31;?>px;left:<?php echo $left31;?>px;"></div><?php
        $t31 = $t31 + $distance_h;
      }

  // *********************** first box ****************************
      $p=0; // Person
      $top = $top_0 + 4*$distance_h - $distance_h/2;?>
      <div class= "fambox" style="left:<?php echo $left_0;?>px;top:<?php echo $top;?>px;"><?php
        if ($person->gender == 1) {$g = "m";} else {$g = "f";}
        $p++;
        fill($person,$g,$p);
        $top = $top_0 + 125;?>
      </div>
      <div class= "fambox" style="left:<?php echo $left;?>px;top:<?php echo $top;?>px;"><?php
        $g = "m";
        $p++;
        fill($father,$g,$p);
        $top = $top + 4* $distance_h;?>
      </div>
      <div class= "fambox" style="left:<?php echo $left;?>px;top:<?php echo $top;?>px;"><?php
        $g = "f";
        $p++;
        fill($mother,$g,$p);
        $top = $top_0 + $distance_h/2;
        $left = $left + $distance_w;?>
      </div>
      <div class= "fambox" style="left:<?php echo $left;?>px;top:<?php echo $top;?>px;"><?php
        $g = "m";
        $p++;
        fill($gffather,$g,$p);
        $top = $top + 2*$distance_h;?>
      </div>
      <div class= "fambox" style="left:<?php echo $left;?>px;top:<?php echo $top;?>px;"><?php
        $g = "f";
        $p++;
        fill($gmfather,$g,$p);
        $top = $top + 2*$distance_h;?>
      </div>
      <div class= "fambox" style="left:<?php echo $left;?>px;top:<?php echo $top;?>px;"><?php
        $g = "m";
        $p++;
        fill($gfmother,$g,$p);
        $top = $top + 2*$distance_h;?>
      </div>

      <div class= "fambox" style="left:<?php echo $left;?>px;top:<?php echo $top;?>px;"><?php
        $g = "f";
        $p++;
        fill($gmmother,$g,$p);
        $top = $top_0;
        $left = $left + $distance_w;?>
      </div>
      <div class= "fambox" style="left:<?php echo $left;?>px;top:<?php echo $top;?>px;"><?php
        $g = "m";
        $p++;
        fill($f_gffather,$g,$p);
        $top = $top + $distance_h;?>
      </div>
      <div class= "fambox" style="left:<?php echo $left;?>px;top:<?php echo $top;?>px;"><?php
        $g = "f";
        $p++;
        fill($m_gffather,$g,$p);
        $top = $top + $distance_h;?>
      </div>

      <div class= "fambox" style="left:<?php echo $left;?>px;top:<?php echo $top;?>px;"><?php
        $g = "m";
        $p++;
        fill($f_gmfather,$g,$p);
        $top = $top + $distance_h;?>
      </div>
      <div class= "fambox" style="left:<?php echo $left;?>px;top:<?php echo $top;?>px;"><?php
        $g = "f";
        $p++;
        fill($m_gmfather,$g,$p);
        $top = $top + $distance_h;?>
      </div>

      <div class= "fambox" style="left:<?php echo $left;?>px;top:<?php echo $top;?>px;"><?php
        $g = "m";
        $p++;
        fill($f_gfmother,$g,$p);
        $top = $top + $distance_h;?>
      </div>
      <div class= "fambox" style="left:<?php echo $left;?>px;top:<?php echo $top;?>px;"><?php
        $g = "f";
        $p++;
        fill($m_gfmother,$g,$p);
        $top = $top + $distance_h;?>
      </div>

      <div class= "fambox" style="left:<?php echo $left;?>px;top:<?php echo $top;?>px;"><?php
        $g = "m";
        $p++;
        fill($f_gmmother,$g,$p);
        $top = $top + $distance_h;?>
      </div>
      <div class= "fambox" style="left:<?php echo $left;?>px;top:<?php echo $top;?>px;"><?php
        $g = "f";
        $p++;
        //var_dump($m_gmmother);die();
        fill($m_gmmother,$g,$p);
        $top = $top + $distance_h;
        $left = $left_0;?>
      </div>
    </div>
    <!-- Ende div container --><?php
    $t_footer = $top;
  }
  //********************** Ende 4 Generationen anzeigen ***************************-->?>

<!--**************************footer ***************************************************-->

    <?php
//************************** 3 Generationen anzeigen ****************************-->
  if ($anz_gen <= 3) { ?>
    <div class="container">
      <table border='0'>
        <tr><?php
          $p = 0;
          $tr_gpar = "<td width='120'><center><div class='gparents'>";
          $td_div = "</div></td>";
            echo $tr_gpar;          // </td></div> wird in fill gestzt
            $g = "m";
            $p++;
            fill($gffather,$g,$p);
            echo $td_div;

            echo $tr_gpar;
            $g = "f";
            $p++;
            fill($gmfather,$g,$p);
            echo $td_div; ?>
            <td>&nbsp;&nbsp;</td><?php

            echo $tr_gpar;
            $g = "m";
            $p++;
            fill($gfmother,$g,$p);
            echo $td_div;

            echo $tr_gpar;
            $g = "f";
            $p++;
            fill($gmmother,$g,$p);
            echo $td_div; ?>
        </tr>
        <tr><td colspan='5'>&nbsp;</td></tr>
  <!-- line parents -->
        <tr><?php $trpar = "<td colspan='2'><center><div class='parents'>";
            $g = "m";
            $p++;
            echo $trpar;
            fill($father,$g,$p);
            echo $td_div; ?>
            <td>&nbsp;</td><?php
            $g = "f";
            $p++;
            echo $trpar;
            fill($mother,$g,$p);
            echo $td_div; ?>
        </tr>
        <tr><td colspan='5'>&nbsp;</td></tr>
  <!-- line main person -->
        <tr><?php $trper = "<td colspan='5'><center><div class='person'>";
          echo $trper;
          if ($person->gender == 1) {$g = "m";} else {$g = "f";}
          $p++;
          fill($person,$g,$p);
          echo $td_div; ?>
        </tr>
        <tr><td colspan='5'>&nbsp;</td></tr>
      </table>
    </div> <?php
  }?>


<?php
} // ende Generation<5

  include __DIR__ . "/../js/hochschieben.js";   // Navigation nach oben schieben
  include __DIR__ . "/../layout/footer_e.php";
?>

</body>
</html>