<?php
$days = array (1 => 'Понедельник', 2 => 'Вторник', 3 => 'Среда', 4 => 'Четверг', 5 => 'Пятница', 6 => 'Суббота', 7 => 'Воскресенье');
$day = date("N");


foreach($arResult["CALLS"] as $k=>$less)
{
    ksort($less);

    /*while(!array_key_exists($day, $arResult)) {
        $day = ($day == 7 || $day == 6) ? 1 : $day++ ;
    }*/

    $searchDay = false;
    if(!array_key_exists($day, $less))
    {
        for($i=$day+1; $i<=7; $i++)
        {
            if(array_key_exists($i, $less))
            {
                $day = $i;
                $searchDay = true;
                break;
            }
        }
        
        if($searchDay == false)
        {
            for($i=1; $i<$day; $i++)
            {
                if(array_key_exists($i, $less))
                {
                    $day = $i;
                    $searchDay = true;
                    break;
                }
            }
        }
        
        if($searchDay == false)
        {
            $day = 1;
        }
    }
    
    $arResult["CALLS"][$k] = $less;
    $dayArr[$k] = $day;
}
?>
<li><a href="#" class="test_3"><?php print t('Расписание звонков') ?></a>

<div class="container" style="width: <?=count($arResult["CALLS"]) * 175?>px;">

<?$k = 1;?>

<? foreach($arResult["CALLS"] as $less => $arLesson) { ?>
<div class="daycallwrap">
    <div class="dayCallTitle">Смена: <?=$less?></div>
    <div class="dayCallTitle">Параллели: <?=$arResult["PARALLEL"][$less]["parallelstart"]?> - <?=$arResult["PARALLEL"][$less]["parallelend"]?></div>
<div id="day-left" class="day-left" data-id="<?=$k?>"></div>
<div id="daycall" class="daycall" data-id="<?=$k?>" style="width: <?=count($arLesson) * 175?>px; left: <?=($dayArr[$less]-1)*-175;?>px;">

<? foreach($arLesson as $day => $arDay) { ?>
    <ul id="schedule">
        <li><?=$days[$day]?></li>
        <?/* if(empty($arResult[$day])): ?>
            <li>Сегодня занятий нет</li>
        <? endif; */?>
        <? foreach($arDay as $lesson => $time): ?>
            <li><?=$lesson?>. <?=$time["timebegin"]?> - <?=$time["timeend"]?></li>
        <? endforeach; ?>
    </ul>
<? } ?>

</div>
<div id="day-right" class="day-right" data-id="<?=$k?>"></div>
</div>

<? $k++; } ?>

</div>

</li>
<li id="cur_period">
<? if(!empty($arPeriod)):?>Текущий период обучения: <span><?=date("d.m.Y",$arPeriod["date_start"])?> - <?=date("d.m.Y",$arPeriod["date_end"])?></span><? endif; ?>
</li>