/// <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-piechart
//
myapp.ChartDrillDown.WijmoChart_render = function (element, contentItem) {

    var div = $("<div/>");
    div.appendTo($(element));
    div.attr("style", "width: 400px; height: 400px");

    contentItem.value.oncollectionchange = function (args) {
        if (args.detail.action !== "refresh") return;

        if (div.children().length) {
            div.wijpiechart("destroy");
        }

        var chart = c1ls.getPieChartContent(contentItem);
        var color = "#999999";

        chart.Hint.content = function () {
            return this.label + ": " + Globalize.format(this.y, "c0");
        };

        div.wijpiechart({
            textStyle: {
                "font-family": c1ls.fontFamily
            },
            footer: {
                visible: false,
                text: "Chart Footer",
                textStyle: { fill: color, "font-size": 18 }
            },
            legend: {
                visible: true,
                style: { stroke: color },
                textStyle: { fill: color, "font-size": 10, "font-weight": "bold" }
            },
            showChartLabels: false,
            seriesList: chart.Series,
            click: chart.OnClick,
            hint: chart.Hint
        });

        contentItem.value.selectedItem = contentItem.value.data[1];
    };

    c1ls.renderControl(contentItem);
};

// http://bit.ly/c1ls-grid
//
myapp.ChartDrillDown.DetailItems_render = function (element, contentItem) {

    var table = $("<table/>");
    table.appendTo($(element));
    table.attr("style", "height: 413px");

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

        var value = grid.FindColumn("Value");
        value.dataFormatString = "c0";

        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);
};
