/// class ProspectDirector.Web.UI.Canvas.Charts.FieldFoundInFeature
/// using ProspectDirector.Web.UI.FeatureSets.MacroEditorBase

/**
@class FieldFoundInFeature
@constructor
@namespace Web.UI.Canvas.Charts
@extends Web.UI.FeatureSets.MacroEditorBase
**/
ProspectDirector.Web.UI.Canvas.Charts.FieldFoundInFeature = ProspectDirector.Web.UI.FeatureSets.MacroEditorBase.extend({

    //==============================
    // PROPERTIES
    //==============================

    get_grid: function () {
        return this.get_gridDomNode().data("kendoGrid");
    },

    get_gridDomNode: function () {
        return this.get_container().find("[data-id=grid]");
    },

    get_gridWrapperDomNode: function () {
        return this.get_container().find("[data-id=grid-wrapper]");
    },

    //==============================
    // INITIALIZERS
    //==============================

    _init_controls: function () {
        this._init_grid();
    },

    _init_grid: function () {

        var that = this,
            data = new ProspectDirector.Web.List(this.getValue("forms"));

        data = data.transform(function (item) {
            return { form: item };
        });

        data = data.toArray();

        this.get_gridDomNode().kendoGrid({
            dataSource: data,
            height: "100%",
            resizable: true,
            columns: [
                {
                    title: "&nbsp;",
                    field: "icon",
                    width: 25,
                    attributes: {
                        style: "vertical-align:top;"
                    },
                    template: function (e) {
                        return "<img src=\"" + ProspectDirector.RUNTIME.resolveUrl("~/application/icons/16x16/form_blue.png") + "\"/>";
                    }
                },
                {
                    title: "Found In",
                    field: "form",
                    attributes: {
                        style: "vertical-align:top;"
                    }
                }
            ],
            dataBound: function () {

                if (that.get_grid().dataSource.data().length > 0) {
                    ProspectDirector.Web.UI.Utilities.Kendo.Grid.removeMessage(that.get_gridDomNode());
                }
                else {
                    ProspectDirector.Web.UI.Utilities.Kendo.Grid.addMessage(that.get_gridDomNode(),
                        "This field is not found within any search groups.");
                }

            }
        });

    },

    //==============================
    // PUBLIC METHODS
    //==============================

    activate: function () {
        this._init_controls();
    },

    render: function () {

        var result = "" +

        "<div id=\"" + this.getId() + "\" style=\"padding:10px;\">" +
            "<div style=\"height:48px;overflow:hidden;font-size:12px;margin:0px 0px 3px 0px;line-height:16px;\">" +
            "The list below contains the search groups within which this field is used for data display. This list helps you in identifying the source of the data contained within this field." +
            "</div>" +
            "<div data-id=\"grid-wrapper\" style=\"border:1px solid #AAAAAA;\">" +
                "<div data-id=\"grid\" style=\"border:0px;\"></div>" +
            "</div>" +
        "<div>";

        return result;

    },

    size: function (e) {

        var wrapper = this.get_element().find("DIV[data-id=grid-wrapper]"),
            grid = this.get_element().find("DIV[data-id=grid]");

        this.get_element().height(e.height);
        this.get_element().width(e.width);
        wrapper.width(e.width - 2);
        wrapper.height(e.height - 53);

        var filler = new ProspectDirector.Web.UI.Utilities.Kendo.Grid.Fill({
            selector: this.get_element().find("DIV[data-role=grid]").selector,
            useAvailableSpace: false
        });

        filler.size();

    }

});