/// <reference path="../GeneratedArtifacts/viewModel.js" />
/// <reference path="../Scripts/c1ls-4.0.20142.37.js" />

if (typeof c1ls === "undefined") {
    window.location.href = "http://bit.ly/c1ls-nuget";
}

var gameDays = [];

myapp.CalendarDateFilter.created = function (screen) {

    setTimeout(function () {
        setToday(screen);
    }, 100);

    screen.AllGames.oncollectionchange = function () {
        if (Object.getOwnPropertyNames(gameDays).length <= 1) {
            screen.AllGames.data.map(function (g) {
                gameDays[g.StartTime.toDateString()] = true;
            });

            $("#monthcal").wijcalendar({
                customizeDate: function ($dayCell, date, dayType, hover, preview) {
                    if (gameDays[date.toDateString()]) {
                        $dayCell.children("a").attr("style", "color:orange !important");
                        return true;
                    }
                }
            });
        }
    };
};

function nextDay(d) {
    return new Date(d.getTime() + (24 * 60 * 60 * 1000));
}

function setToday(screen) {
    var min = new Date('2014/10/08');
    var max = new Date('2015/04/11');
    var start = new Date();
    start.setHours(0);
    start.setMinutes(0);
    if (start < min) {
        start = min;
    }
    else if (start > max) {
        start = max;
    }
    screen.StartDate = start;
    screen.EndDate = nextDay(start);
}

// http://bitly.com/c1ls-calendar
//
myapp.CalendarDateFilter.StartDate_render = function (element, contentItem) {

    $.wijmo.wijcalendar.prototype.options.wijCSS.stateDefault = "ui-btn-up-a";
    $.wijmo.wijcalendar.prototype.options.wijCSS.content = "ui-body ui-body-a";

    var div = $("<div id='monthcal' data-role='wijcalendar'/>");
    div.appendTo($(element));

    div.wijcalendar({
        selectionMode: {day: true},
        displayDate: contentItem.value,
        allowQuickPick: false,
        minDate: new Date('2014/10/08'),
        maxDate: new Date('2015/04/11'),
        customizeDate: function ($dayCell, date, dayType, hover, preview) {
            if (gameDays[date.toDateString()]) {
                $dayCell.children("a").attr("style", "color:orange !important");
                return true;
            }
        }
    });

    div.wijcalendar("selectDate", contentItem.value);
    var displayDate = true;

    div.wijcalendar({
        selectedDatesChanged: function (e, args) {
            var newValue = div.wijcalendar("getSelectedDate");
            if (contentItem.value != newValue) {
                displayDate = false;
                contentItem.value = newValue;
                contentItem.screen.EndDate = nextDay(newValue);
                displayDate = true;
            }
        }
    });

    contentItem.dataBind("value", function (newValue) {
        div.wijcalendar("unSelectAll");
        if (displayDate) {
            div.wijcalendar({ displayDate: newValue });
        }
        div.wijcalendar("selectDate", newValue);
    });
};

// http://bitly.com/c1ls-grid
//
myapp.CalendarDateFilter.SelectedGames_render = function (element, contentItem) {

    var table = $("<table/>");
    table.appendTo($(element));
    table.attr("style", "width: 400px; margin-top: 10px;");

    contentItem.value.oncollectionchange = function () {
        var grid = c1ls.getGridContent(table, contentItem);

        var time = grid.FindColumn("StartTime");
        time.dataFormatString = "t";

        table.wijgrid({
            // initialization
            columns: grid.Columns,
            data: grid.Rows(),

            // editing
            editingMode: "none",
            afterCellUpdate: grid.OnCellUpdate,

            // sorting
            allowSorting: false,
            sorting: grid.Sorting,

            // filtering
            showFilter: false,
            filtering: grid.Filtering,

            // selection
            selectionMode: "none",
            showRowHeader: grid.ShowRowHeader,
            showSelectionOnRender: false,
            cellClicked: grid.OnCellClicked,
            cellStyleFormatter: grid.OnCellFormat
        });
    };

    c1ls.renderControl(contentItem);
};
