<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
  /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */

  // load viewer library
  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('/home/toledoh/public_html/clubsite/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

  // load records
  list($eventsRecords, $blogMetaData) = getRecords(array(
    'tableName'   => 'events',
  ));

  # team filter must accept records with no teams specified (i.e. a "public record") 
  $team_filter = "membership = ''"; 
   
  # team filter must also accept records with any single team which is also listed in $CURRENT_USER['team'] 
  foreach (explode("\t", trim($CURRENT_USER['membership'])) as $team_num) { 
    $team_filter .= mysql_escapef(" OR membership LIKE ?", "%\t$team_num\t%"); 
  } 
   
  list($eventsRecords, $eventsMetaData) = getRecords(array(
    'tableName'   => 'events',
    'where'         => $team_filter, 
  ));

?>
<?php
  include("_insertGeneralViewerCode.php");
?>    
<?PHP  
  function maxWords($textOrHtml, $maxWords) { 
    $text=str_replace("<p>","*P*",$textOrHtml); 
    $text= str_replace("</p>","*/P*",$text); 
    $text = strip_tags($text);   
    $words = preg_split("/\s+/", $text, $maxWords+1);   
    if (count($words) > $maxWords) { unset($words[$maxWords]); }   
    $output = join(' ', $words);   
    $output=str_replace("*P*","<p>",$output); 
    $output=str_replace("*/P*","</p>",$output); 
    $output.="</p>"; 
 
    return $output;   
  }   
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>ClubsSite Demo</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />


<link rel="stylesheet" href="css/clubs.css" type="text/css" />
<link rel="stylesheet" href="css/zickey_calendar.css" type="text/css" />
<script type="text/javascript" src="js/clubsGlobal.js"></script>
<style type="text/css">
<!--
p {
    font-size: 14px; }
a.tip {
	position: relative;
}

a.tip span {
	display: none;
	position: absolute;
	top: 30px;
	left: 50px;
	width: 400px;
	padding: 5px;
	z-index: 100;
	background: #000;
	color: #fff;
	-moz-border-radius: 5px; /* this works only in camino/firefox */
	-webkit-border-radius: 5px; /* this is just for Safari */
}

a:hover.tip { text-decoration: none;
	font-size: 99%; /* this is just for IE */
}

a:hover.tip span {
	display: block;
}

-->
</style>
</head>
<body>
<div id="pagewidth" >
    <?php
    include("_insertHeader.php");
    ?>    
    <?php
    include("_insertNavigation.php");
    ?>    
<div id="wrapper" class="clearfix" > 
		<div id="twocols" class="clearfix"> 
			<div id="maincol" >
                <div id="container"> 
                <!-- calendar --> 
                <?php
                  
                  // <zickey> - get selected month
                  $monthNames = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); 
                  if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n"); 
                  if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y"); 
                   
                  $cMonth = $_REQUEST["month"]; 
                  $cYear = $_REQUEST["year"]; 
                   
                  $prev_year = $cYear; 
                  $next_year = $cYear; 
                   
                  $prev_month = $cMonth-1; 
                  $next_month = $cMonth+1; 
                   
                  if ($prev_month == 0 ) { 
                  $prev_month = 12; 
                  $prev_year = $cYear - 1; 
                  } 
                  if ($next_month == 13 ) { 
                  $next_month = 1; 
                  $next_year = $cYear + 1; 
                  }
                  // </zickey>
                  
                  // find all events which overlap with this month
                  $this_month_date = sprintf("%04d-%02d-01 00:00:00", $cYear, $cMonth);
                  $next_month_date = sprintf("%04d-%02d-01 00:00:00", $next_year, $next_month);
                  list($eventsRecords, ) = getRecords(array(
                    'tableName' => 'events',
                    'where'     =>  '(' . $team_filter . ')' AND mysql_escapef("start_date < ? AND IF(end_date != '0000-00-00', end_date >= ?, start_date >= ?)", $next_month_date, $this_month_date, $this_month_date),
                  ));
                
                  // go through events and build up a mapping of dates to events, respecting repeated events
                  $eventsByDay = array();
                  $this_month_time = strtotime($this_month_date);
                  $next_month_time = strtotime($next_month_date) - 1;
                  foreach ($eventsRecords as $record) { 
                    if (strtotime($record['end_date'])) { 
                      $last_day     = null; 
                      $current_time = max($this_month_time, strtotime($record['start_date'])); 
                      $end_time     = min($next_month_time, strtotime($record['end_date'])); 
                      while ($current_time < $end_time || date("Y-m-d", $current_time) == date("Y-m-d", $end_time)) { 
                        $skip = false; 
                         
                        $current_day = date("Y-m-d", $current_time); 
                        // skip if we've already seen this day (necessary because we're skipping forward by 23 hours) 
                        if ($current_day == $last_day) { $skip = true; } 
                         
                        // skip if relevant weekday checkbox isn't checked 
                        $current_weekday = strtolower(date('l', $current_time)); 
                        if (!$record[$current_weekday]) { $skip = true; } 
                         
                        // add event to this day
                        if (!$skip) {
                          $day = intval(date('d', $current_time)); 
                          if (!array_key_exists($day, $eventsByDay)) { $eventsByDay[$day] = array(); } 
                          $eventsByDay[$day][] = $record; 
                        } 
                         
                        // advance current_time (but not by 24 hours, lest we skip over a springtime daylight-savings day) 
                        $current_time += 23*60*60; 
                        $last_day = $current_day; 
                      } 
                    } 
                    else { 
                      // no end_date, so this is a single-day event 
                      $day = intval(date('d', strtotime($record['start_date']))); 
                      if (!array_key_exists($day, $eventsByDay)) { $eventsByDay[$day] = array(); } 
                      $eventsByDay[$day][] = $record; 
                    } 
                  }
                ?> 
                
                <div id="calendar_div" name="calendar_div"> 
                <table border="0" cellpadding="1" cellspacing="0" bgcolor="#333333"> 
                <tr align="center"> 
                <td bgcolor="#333333"><table width="100%" border="0" cellspacing="0" cellpadding="0"> 
                <tr> 
                <td width="50%" align="left">&nbsp;&nbsp;<a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $prev_month . "&year=" . $prev_year; ?>" class="CalNextPrevious">Previous</a></td> 
                <td width="50%" align="right"><a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $next_month . "&year=" . $next_year; ?>" class="CalNextPrevious">Next</a>&nbsp;&nbsp;</td> 
                </tr> 
                </table></td> 
                </tr> 
                <tr> 
                <td align="center"><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF"> 
                <tr align="center"> 
                <td colspan="7" class="CalMonth" ><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></td> 
                </tr> 
                <tr> 
                <td class="CalDayofWeek">Monday</td> 
                <td class="CalDayofWeek">Tuesday</td> 
                <td class="CalDayofWeek">Wednesday</td> 
                <td class="CalDayofWeek">Thursday</td> 
                <td class="CalDayofWeek">Friday</td> 
                
                <td class="CalDayofWeek">Saturday</td> 
                <td class="CalDayofWeek">Sunday</td> 
                </tr> 
                <?php 
                $timestamp = mktime(0,0,0,$cMonth,1,$cYear); 
                $maxday = date("t",$timestamp); 
                $thismonth = getdate ($timestamp); 
                $startday = $thismonth['wday'] -1; 
                 
                for ($i=0; $i<($maxday+$startday); $i++) { 
                if(($i % 7) == 0 ) echo "<tr>\n"; 
                if($i < $startday) { 
                  echo "<td></td>\n"; 
                } 
                else { 
                  $day = $i - $startday + 1; 
                  echo "<td class='CalDates'>". $day . "<br />"; 
                  // check if there are any events for this day 
                  if (array_key_exists($day, $eventsByDay)) { 
                    // list all events for this day 
                    foreach ($eventsByDay[$day] as $record) { 
                      ?> <div id="calFonts">
                       <a href="<?php echo $record['_link'] ?>" class="tip"> <h1><?php echo $record['title'] ?></h1> 
                        <span><?php echo $record['title'] ?><br />Time: <?php echo date("g:i a", strtotime($record['start_date'])) ?><br /><br />Where: <?php echo $record['location'] ?><br /><br />What: <?php echo $record['content'] ?></span></a>
                      <?php 
                    } 
                  } 
                  echo "</td>\n"; 
                } 
                if(($i % 7) == 6 ) echo "</tr>\n"; 
                } 
                ?> 
                 
                </table></td> 
                </tr> 
                </table> 
                <br /> 
                <!-- /calendar --></div> 
                <!-- /container --></div> 
            </div>
			<div id="leftcol" > Left Column </div>
		</div> 
</div>
    <?php
    include("_insertFooter.php");
    ?>    
	</div>
</body>
</html>