<?
   $M = Array('01' => 'янв', '02' => 'фев', '03' => 'мар', '04' => 'апр', '05' => 'май', '06' => 'июн', '07' => 'июл', '08' => 'авг', '09' => 'сен', '10' => 'окт', '11' => 'ноя', '12' => 'дек');
   
   $M_FULL = Array('01' => 'Январь', '02' => 'Февраль', '03' => 'Март', '04' => 'Апрель', '05' => 'Май', '06' => 'Июнь', '07' => 'Июль', '08' => 'Август', '09' => 'Сентябрь', '10' => 'Октябрь', '11' => 'Ноябрь', '12' => 'Декабрь');
   
   $W = Array('1' => 'ПН', '2' => 'ВТ', '3' => 'СР', '4' => 'ЧТ', '5' => 'ПТ', '6' => 'СБ', '7' => 'ВС');
   
   $l = 46;
   
   $width = array(
		0 => $l,
		1 => $l,
		2 => $l,
		3 => $l
   );
   
   for($i=4; $i<=10; $i++) {
	$l -= 2;
	$width[$i] = $l;
   }
?>
                                <div class="month page">
									<? if(count($arResult["NAVIGATION"]["MONTHS"]) > 7) { ?>
									<div id="month-left"></div>
									<? } ?>
									<div class="months">
										<ul id="months-list" style="width: <? echo (count($arResult["NAVIGATION"]["MONTHS"])-1) * 63 + 126; ?>px;">
									<?
									foreach($arResult["NAVIGATION"]["MONTHS"] as $timestamp) { ?>
											<li<?=(date("m-Y",$arResult["ACTIVE"]) == date("m-Y", $timestamp)) ? ' class="curent"' : '' ;?>><a href="/diary/<?=date("Y-m-d", $timestamp)?>"><?=$M_FULL[date("m", $timestamp)]?></a></li>
									<? }	?>
										</ul>
									</div>
									<? if(count($arResult["NAVIGATION"]["MONTHS"]) > 7) { ?>
									<div id="month-right"></div>
									<? } ?>

									<div class="days">
									<?
									foreach($arResult["NAVIGATION"]["WEEKS"] as $arWeek) { ?>
										<a href="/diary/<?=date("Y-m-d", $arWeek["START"])?>"<?=($arResult["ACTIVE"] >= $arWeek["START"] && $arResult["ACTIVE"] <= $arWeek["END"]) ? ' class="curent_day"' : '';?>><?=date("j", $arWeek["START"])?>-<?=date("j", $arWeek["END"])?></a>
									<?}?>
									</div>
									<div class="curent_year">
										<? if(!empty($arResult["NAVIGATION"]["PREV_SCHOOLYEAR"])) { ?>
											<a id="year-left" title="<?=$arResult["NAVIGATION"]["PREV_SCHOOLYEAR"]["NAME"]?>" href="/diary/<?=date("Y-m-d",$arResult["NAVIGATION"]["PREV_SCHOOLYEAR"]["DATE"])?>"></a>
										<? } ?>
										<?=$arResult["NAVIGATION"]["SCHOOLYEAR"]?>
										<? if(!empty($arResult["NAVIGATION"]["NEXT_SCHOOLYEAR"])) { ?>
											<a id="year-right" title="<?=$arResult["NAVIGATION"]["NEXT_SCHOOLYEAR"]["NAME"]?>" href="/diary/<?=date("Y-m-d",$arResult["NAVIGATION"]["NEXT_SCHOOLYEAR"]["DATE"])?>"></a>
										<? } ?>
									</div>
                                </div>
<? if(!empty($arResult)):?>
<table class="diary type_3">
	<tbody>
<? //print_r($arResult); ?>
<?
$days = array_keys($arResult["EVENTS"]);
if(count( $arResult["EVENTS"]) % 3 == 0) {
	$to = ceil( count( $arResult["EVENTS"]) / 3 );
} else {
	$to = ceil( count( $arResult["EVENTS"]) / 3 ) - 1;
}
for( $i=0; $i <= $to; $i -= 2) {
	$day 	  	= explode("-", $days[$i]);
	$mktime	= mktime(0, 0, 0, $day[1], $day[0], $day[2]);
	$is_active = ($days[$i] == date("j-m-Y")) ? " active" : "";
?>
		<tr>
			<td class="left">
				<div class="day_week<? echo ($i == 0) ? " first" . $is_active : $is_active; ?>">
					<span class="week"><?=$W[date("N",$mktime)]?></span>
					<span class="day"><?=date("d",$mktime)?></span>
					<span class="mons"><?=$M[date("m",$mktime)]?></span>
				</div>
				<table> 
				<? if ($i == 0): ?>
					<thead>
						<tr>
							<td class="number_leson">&nbsp;</td>
							<td class="leson"><span>Предмет</span></td>
							<td><span>Оценка</span></td>
							<td class="homework">Домашнее задание / <span>комментарии учителя</span></td>
						</tr>
					</thead>
				<? endif; ?>
				
					<tbody>
						<?
						if(!empty($arResult["EVENTS"][$days[$i]])) {
							$lessons = array_keys($arResult["EVENTS"][$days[$i]]);
							$max = (max($lessons) > 5) ? max($lessons) : 5;
							for($j = 1; $j <= $max; $j++) {
								if(!empty($arResult["EVENTS"][$days[$i]][$j])) {
								foreach($arResult["EVENTS"][$days[$i]][$j] as $arLesson) {
								$mkcnt = count($arLesson["MARKS"]);
								$cls = ($mkcnt > 4) ? " width_1" : "";
								?>
							<tr>
								<td class="number_leson"><?=$j ?>. </td>
								<td class="leson"><span title="<? echo $arLesson["TEACHER"]; echo (!empty($arLesson["CABINET"])) ? ', ' . $arLesson["CABINET"] : ''?>"><?=$arLesson["SUBJECT"]?></span></td>
								<td class="description"  colspan="2">
									<?
									if($arLesson["MISS"] === true) { ?>
										<div class="mark out "><span title="Отсутствие на уроке">Н</span></div>
									<? } elseif(!empty($arLesson["MARKS"])) { ?>
										<div class="mark count_<?=$mkcnt?>">
										<? foreach($arLesson["MARKS"] as $mark) { ?>
											<span class="<? echo ($mark["VALUE"] <=5) ? "orange_".$mark["VALUE"] : "grey"; echo $cls; ?>" title="<? echo (is_string($mark["VALUE"]) && mb_strlen($mark["VALUE"]) > 3) ? htmlspecialchars($mark["VALUE"]) . ", " : '' ;?><? echo (empty($mark["COMMENT"])) ? '' : $mark["COMMENT"]; ?>"><? echo (is_string($mark["VALUE"]) && mb_strlen($mark["VALUE"]) > 3) ? htmlspecialchars(mb_substr($mark["VALUE"], 0, 3)) : htmlspecialchars($mark["VALUE"]);?></span>
										<? } ?>
										</div>
									<? } else { ?>
										<div class="mark"></div>
									<? }	?>
									<div class="task"><font title="<?=htmlspecialchars($arLesson["HOMEWORK"])?>"><?=htmlspecialchars(mb_substr($arLesson["HOMEWORK"], 0, $width[$mkcnt]))?><? echo (mb_strlen($arLesson["HOMEWORK"]) > $width[$mkcnt]) ? "&hellip;" : ""; ?></font><?if(!empty($arLesson["COMMENT"]))?><br/><span title="<?=htmlspecialchars($arLesson["COMMENT"])?>"><?=htmlspecialchars(mb_substr($arLesson["COMMENT"], 0, $width[$mkcnt]))?><? echo (mb_strlen($arLesson["COMMENT"]) > $width[$mkcnt]) ? "&hellip;" : ""; ?></span></div>
								</td>                                       
							</tr>
								<?
								}
								} else {
								?>
							<tr>
								<td class="number_leson"><?=$j ?>. </td>
								<td class="leson no"></td>
								<td class="description"  colspan="2"><div class="mark count_1"></div></td>                                       
							</tr>
								<?
								}
							}
						} else {
							for($j=1; $j<=5; $j++) {?>
							<tr>
								<td class="number_leson"><?=$j?>. </td>
								<td class="leson no"></td>
								<td class="description"  colspan="2"><div class="mark count_1"></div></td>                                       
							</tr>
						<?
							}
						}
						?>
					</tbody>
				</table>
			</td>
			
			<td class="center_line"></td>
			<?
			$i += 3;
			if(isset($arResult["EVENTS"][$days[$i]])) {
			$day 	  	= explode("-", $days[$i]);
			$mktime	= mktime(0, 0, 0, $day[1], $day[0], $day[2]);
			$is_active = ($days[$i] == date("j-m-Y")) ? " active" : "";
			?>
			<td class="right">
				<div class="day_week<? echo ($i == 3) ? " first" . $is_active : $is_active; ?>">
					<span class="week"><?=$W[date("N",$mktime)]?></span>
					<span class="day"><?=date("d",$mktime)?></span>
					<span class="mons"><?=$M[date("m",$mktime)]?></span>
				</div>
				<table> 
				<? if ($i == 3): ?>
					<thead>
						<tr>
							<td class="number_leson">&nbsp;</td>
							<td class="leson"><span>Предмет</span></td>
							<td><span>Оценка</span></td>
							<td class="homework">Домашнее задание / <span>комментарии учителя</span></td>
						</tr>
					</thead>
				<? endif; ?>
				
					<tbody>
						<?
						if(!empty($arResult["EVENTS"][$days[$i]])) {
							$lessons = array_keys($arResult["EVENTS"][$days[$i]]);
							$max = (max($lessons) > 5) ? max($lessons) : 5;
							for($j = 1; $j <= $max; $j++) {
								if(!empty($arResult["EVENTS"][$days[$i]][$j])) {
								foreach($arResult["EVENTS"][$days[$i]][$j] as $arLesson) {
								$mkcnt = count($arLesson["MARKS"]);
								$cls = ($mkcnt > 4) ? " width_1" : "";
								?>
							<tr>
								<td class="number_leson"><?=$j ?>. </td>
								<td class="leson"><span title="<? echo $arLesson["TEACHER"]; echo (!empty($arLesson["CABINET"])) ? ', ' . $arLesson["CABINET"] : ''?>"><?=$arLesson["SUBJECT"]?></span></td>
								<td class="description"  colspan="2">
									<?
									if($arLesson["MISS"] === true) { ?>
										<div class="mark out "><span title="Отсутствие на уроке">Н</span></div>
									<? } elseif(!empty($arLesson["MARKS"])) { ?>
										<div class="mark count_<?=$mkcnt?>">
										<? foreach($arLesson["MARKS"] as $mark) { ?>
											<span class="<? echo ($mark["VALUE"] <=5) ? "orange_".$mark["VALUE"] : "grey"; echo $cls; ?>" title="<? echo (is_string($mark["VALUE"]) && mb_strlen($mark["VALUE"]) > 3) ? htmlspecialchars($mark["VALUE"]) . ", " : '' ;?><? echo (empty($mark["COMMENT"])) ? '' : $mark["COMMENT"]; ?>"><? echo (is_string($mark["VALUE"]) && mb_strlen($mark["VALUE"]) > 3) ? htmlspecialchars(mb_substr($mark["VALUE"], 0, 3)) : htmlspecialchars($mark["VALUE"]);?></span>
										<? } ?>
										</div>
									<? } else { ?>
										<div class="mark"></div>
									<? }	?>
									<div class="task"><font title="<?=htmlspecialchars($arLesson["HOMEWORK"])?>"><?=htmlspecialchars(mb_substr($arLesson["HOMEWORK"], 0, $width[$mkcnt]))?><? echo (mb_strlen($arLesson["HOMEWORK"]) > $width[$mkcnt]) ? "&hellip;" : ""; ?></font><?if(!empty($arLesson["COMMENT"]))?><br/><span title="<?=htmlspecialchars($arLesson["COMMENT"])?>"><?=htmlspecialchars(mb_substr($arLesson["COMMENT"], 0, $width[$mkcnt]))?><? echo (mb_strlen($arLesson["COMMENT"]) > $width[$mkcnt]) ? "&hellip;" : ""; ?></span></div>
								</td>                                       
							</tr>
								<?
								}
								} else {
								?>
							<tr>
								<td class="number_leson"><?=$j ?>. </td>
								<td class="leson no"></td>
								<td class="description"  colspan="2"><div class="mark count_1"></div></td>                                       
							</tr>
								<?
								}
							}
						} else {
							for($j=1; $j<=5; $j++) {?>
							<tr>
								<td class="number_leson"><?=$j?>. </td>
								<td class="leson no"></td>
								<td class="description"  colspan="2"><div class="mark count_1"></div></td>                                       
							</tr>
						<?
							}
						}
						?>
					</tbody>
				</table>
			</td>
			<? } else { ?>
			<td></td>
			<? }	?>
		</tr>
		
		<tr class="null_line"> 
			<td class="left"></td>
			<td class="center_line"></td>
			<td class="right"></td>
		</tr>
<? }?>
	</tbody>
</table>
<? endif; ?>