<?php
  // Define order of weekdays
    $Weekdays = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
  $UserFirstDayOfWeek = getCurrentUserVar('UserFirstDayOfWeek');
  $Index = array_search ( $UserFirstDayOfWeek, $Weekdays );
  if ($UserFirstDayOfWeek!='Monday')
    for ($k=1; $k<=$Index; $k++) array_push ( $Weekdays, array_shift ( $Weekdays ) );
  $String = '<table summary="Monatsuebersicht">
              <tr>
                <th colspan="7">'.getMonthNameFromMonthNumber($Month).' \''.substr($Year,-2).'</th>
              </tr>
              <tr>';
  for ($l=0; $l<7; $l++)
    $String .= '<td class="WeekdayName">'.${'L_'.substr($Weekdays[$l], 0, 3)}.'</td>';
  $String .= '</tr>
              <tr>';
  ob_start();
  $ErsterWochentag = getWeekdayFromDate (1, $monat, $Year)+($Index);
  $TimestampFromToday = strtotime('today');
  $Tag = 1;
  $Zelle = 1;
  $NumberOfDaysInMonth = getNumberOfDaysInMonth ($monat, $Year);
  for ($z=1; $z<=6; $z++)
  {
    for ($s=1; $s<=7; $s++)
    {
      if (($Zelle < $ErsterWochentag) OR ($Tag > $NumberOfDaysInMonth)) {$String .= "<td>\n</td>";}
      else
      {
        $Timestamp = strtotime($Year.'-'.$monat.'-'.$Tag);
        //$GigStatusDesTages = getGigTypeOfTimestamp($Timestamp);
        $ToolTip = GetTooltip($Timestamp);
        $GigID = getGigVarFromGigDate ($Timestamp, 'GigID');
        if ( getNumerOfEventsFromTimestamp( $Timestamp )>1 ) $GigStatusDesTages = 'DoubleDates';
          else $GigStatusDesTages = 'Normal';
        $String .= '<td';
        $String .= ' class="'.$GigStatusDesTages.' '.getWeekdayNameFromDate ($Tag, $monat, $Year);
        if ($Timestamp == $TimestampFromToday) $String .= ' Today';
        $String .= '"';
        if ( $GigStatusDesTages!="DoubleDates" ) $String .= ' title="'.$ToolTip.'"';
        $String .= '>';
        // Wochentags-Zahl mit Link
          if ( IsUserUser() AND $GigStatusDesTages == "Normal")
          {
            $String .= $Tag;
          }
          else
          {
            $String .= '<a href="index.php?submenu=calendar&amp;page=';
            if ( $GigStatusDesTages=="DoubleDates" ) { $String .= 'backend_selectGig&amp;timestamp='.$Timestamp.'"'; }
            else
            {
              if ( IsUserUser() ) { $String .= 'backend_showGig'; }
              else
              {
                if ( $GigStatusDesTages == "Normal" ) { $String .= 'backend_addGig&amp;job=add&amp;timestamp='.$Timestamp.''; }
                else $String .= 'backend_addGig&amp;job=edit';
              }
              $String .= '&amp;id='.$GigID.'" title="'.$ToolTip.'"';
            }
            $String .= '>'.$Tag.'</a>';
          }
        $String .= "</td>\n";
        $Tag++;
      }
      if ($s == 7)
      {
        if ($z < 6)
        {
          $String .= "</tr><tr>";
        }
        else
        {
          $String .= "</tr>";
        }
      }
      $Zelle++;
    }
  }
  ob_end_flush();
  $String .= "</table>";
?>
