@model NzbDrone.Web.Models.UpcomingEpisodesModel
@{ViewBag.Title = "Upcoming";}
@section HeaderContent
{
    <style>
        .gridControls
        {
            float: right;
            padding-top: 10px;
            visibility: hidden;
        }
        
        .sub-menu
        {
            float: left;
        }
    </style>
}
@section ActionMenu{
    <ul class="sub-menu">
        <li>@Ajax.ActionLink("Start RSS Sync", "RssSync", "Command", null, null, new { Title = "Check for newly released downloads" })</li>
    </ul>
}
<div class="gridControls">
    @Html.CheckBox("showDownloaded", true)
    <label for="showDownloaded">
        Show Downloaded</label>
</div>
<table class="seriesTable">
    <colgroup>
        <col />
        <col style="width: 90px" />
        <col style="width: 300px" />
        <col style="width: 160px" />
        <col style="width: 100px" />
    </colgroup>
    <thead>
        <tr>
            <th>
                Series Title
            </th>
            <th>
                Episode
            </th>
            <th>
                Episode Title
            </th>
            <th>
                Air Time
            </th>
            @*Commands/Status Column*@
            <th>
                Status
            </th>
        </tr>
    </thead>
    <tbody>
        <tr class="title-row">
            <td colspan="6">
                Yesterday
            </td>
        </tr>
        @for (int i = 0; i < Model.Yesterday.Count; i++)
        {
            var episode = Model.Yesterday[i];

            if (i % 2 == 0)
            {
                Html.RenderPartial("UpcomingEpisode", episode);
            }

            else
            {
                Html.RenderPartial("UpcomingEpisode", episode, new ViewDataDictionary { new KeyValuePair<string, object>("AltRow", true) });
            }
        }
        <tr class="title-row">
            <td colspan="6">
                Today
            </td>
        </tr>
        @for (int i = 0; i < Model.Today.Count; i++)
        {
            var episode = Model.Today[i];

            if (i % 2 == 0)
            {
                Html.RenderPartial("UpcomingEpisode", episode);
            }

            else
            {
                Html.RenderPartial("UpcomingEpisode", episode, new ViewDataDictionary { new KeyValuePair<string, object>("AltRow", true) });
            }
        }
        <tr class="title-row">
            <td colspan="6">
                Tomorrow
            </td>
            @for (int i = 0; i < Model.Tomorrow.Count; i++)
            {
                var episode = Model.Tomorrow[i];

                if (i % 2 == 0)
                {
                    Html.RenderPartial("UpcomingEpisode", episode);
                }

                else
                {
                    Html.RenderPartial("UpcomingEpisode", episode, new ViewDataDictionary { new KeyValuePair<string, object>("AltRow", true) });
                }
            }
            <tr class="title-row">
                <td colspan="6">
                    @DateTime.Today.AddDays(2).DayOfWeek
                </td>
            </tr>
            @for (int i = 0; i < Model.TwoDays.Count; i++)
            {
                var episode = Model.TwoDays[i];

                if (i % 2 == 0)
                {
                    Html.RenderPartial("UpcomingEpisode", episode);
                }

                else
                {
                    Html.RenderPartial("UpcomingEpisode", episode, new ViewDataDictionary { new KeyValuePair<string, object>("AltRow", true) });
                }
            }
            <tr class="title-row">
                <td colspan="6">
                    @DateTime.Today.AddDays(3).DayOfWeek
                </td>
            </tr>
            @for (int i = 0; i < Model.ThreeDays.Count; i++)
            {
                var episode = Model.ThreeDays[i];

                if (i % 2 == 0)
                {
                    Html.RenderPartial("UpcomingEpisode", episode);
                }

                else
                {
                    Html.RenderPartial("UpcomingEpisode", episode, new ViewDataDictionary { new KeyValuePair<string, object>("AltRow", true) });
                }
            }
            <tr class="title-row">
                <td colspan="6">
                    @DateTime.Today.AddDays(4).DayOfWeek
                </td>
            </tr>
            @for (int i = 0; i < Model.FourDays.Count; i++)
            {
                var episode = Model.FourDays[i];

                if (i % 2 == 0)
                {
                    Html.RenderPartial("UpcomingEpisode", episode);
                }

                else
                {
                    Html.RenderPartial("UpcomingEpisode", episode, new ViewDataDictionary { new KeyValuePair<string, object>("AltRow", true) });
                }
            }
            <tr class="title-row">
                <td colspan="6">
                    @DateTime.Today.AddDays(5).DayOfWeek
                </td>
            </tr>
            @for (int i = 0; i < Model.FiveDays.Count; i++)
            {
                var episode = Model.FiveDays[i];

                if (i % 2 == 0)
                {
                    Html.RenderPartial("UpcomingEpisode", episode);
                }

                else
                {
                    Html.RenderPartial("UpcomingEpisode", episode, new ViewDataDictionary { new KeyValuePair<string, object>("AltRow", true) });
                }
            }
            <tr class="title-row">
                <td colspan="6">
                    @DateTime.Today.AddDays(6).DayOfWeek
                </td>
            </tr>
            @for (int i = 0; i < Model.SixDays.Count; i++)
            {
                var episode = Model.SixDays[i];

                if (i % 2 == 0)
                {
                    Html.RenderPartial("UpcomingEpisode", episode);
                }

                else
                {
                    Html.RenderPartial("UpcomingEpisode", episode, new ViewDataDictionary { new KeyValuePair<string, object>("AltRow", true) });
                }
            }
            <tr class="title-row">
                <td colspan="6">
                    Later
                </td>
            </tr>
            @for (int i = 0; i < Model.Later.Count; i++)
            {
                var episode = Model.Later[i];

                if (i % 2 == 0)
                {
                    Html.RenderPartial("UpcomingEpisode", episode);
                }

                else
                {
                    Html.RenderPartial("UpcomingEpisode", episode, new ViewDataDictionary { new KeyValuePair<string, object>("AltRow", true) });
                }
            }
    </tbody>
</table>
@section Scripts{
    <script type="text/javascript">
        $(document).ready(function () {
            var cookieValue = $.cookie("showDownloaded");

            if (cookieValue == "false") {
                $('#showDownloaded').attr('checked', false);
                toggleDownloaded(false);
            }

            else {
                $('#showDownloaded').attr('checked', true);
                toggleDownloaded(true);
            }

            $('#showDownloaded').button();
            $('.gridControls').css('visibility', 'visible');
        });

        $('#showDownloaded').on('change', function () {
            var checked = $(this).attr('checked');
            toggleDownloaded(checked);
            toggleHideDownloadedCookie(checked);
        });

        function toggleDownloaded(show) {
            var ready = $('.statusImage[title="Ready"]');

            ready.each(function () {
                if (show) {
                    $(this).parents('tr').show();
                }

                else {
                    $(this).parents('tr').hide();
                }
            });
        }

        function toggleHideDownloadedCookie(show) {
            if (show)
                $.cookie("showDownloaded", true, { expires: 365 });

            else
                $.cookie("showDownloaded", false, { expires: 365 });
        }
    </script>
}
