/// <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";
}

// http://bit.ly/c1ls-eventscalendar
//
myapp.CalendarTapAction.Games_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/>");
    div.appendTo($(element));
    div.attr("style", "width: 800px; height: 660px");

    var min = new Date('2014/10/08');
    var max = new Date('2015/04/11');
    var today = new Date();
    today.setHours(0);
    today.setMinutes(0);

    if (today < min) {
        today = min;
    }
    else if (today > max) {
        today = max;
    }

    contentItem.dataBind("value.isLoaded", function (newValue) {
        if (newValue) {
            div.wijevcal({
                timeInterval: 60,
                timeIntervalHeight: 20,
                viewType: "day",
                selectedDate: today
            });

            $(".wijmo-wijev-headerbar .wijmo-wijev-tools").controlgroup("refresh");

            contentItem.value.oncollectionchange = function (a) {
                var calendar = c1ls.getCalendarContent(div, contentItem), timer;

                function refresh() {
                    if (div.is(":visible")) {
                        clearInterval(timer);
                        div.wijevcal("option", "beforeEditEventDialogShow", calendar.Select);
                        div.wijevcal("option", "eventsData", calendar.Events);
                    }
                }

                if (a.detail.action === "refresh") {
                    timer = setInterval(refresh, 100);
                }
            };

            c1ls.renderControl(contentItem);
        }
    });
};
