ProspectDirector.Types.KendoExtensions.Grid = {};

//---------------------------
//--- STATIC METHODS
//---------------------------

ProspectDirector.Types.KendoExtensions.Grid.addMessage = function (grid, text, callback) {

    var container = $("<table style=\"height:100%;background-color:#FFFFFF;\" data-role=\"message-container\"><tr><td style=\"text-align:center;vertical-align:middle;\"></td></tr></table>"),
        message = $("<span style=\"font-size:13px;\" data-role=\"message\">" + text + "</span>");

    ProspectDirector.Types.KendoExtensions.Grid.removeMessage(grid);

    if ($.isFunction(callback)) {

        message.css({
            textDecoration: "underline",
            color: "#0000FF",
            cursor: "pointer"
        });

        grid.on("click.gridmessage", "[data-role=message-container] [data-role=message]", callback);

    }

    message.appendTo(container.find("TR > TD"));

    grid.find(".k-grid-content").prepend(container);

}

ProspectDirector.Types.KendoExtensions.Grid.removeMessage = function (grid) {
    grid.find(".k-grid-content [data-role=message-container]").remove();
    grid.off("click.gridmessage");
};

ProspectDirector.Types.KendoExtensions.GridFill = Class.extend({

    init: function (options) {

        this.options = $.extend({
            selector: null,
            useAvailableSpace: false
        }, options);

    },

    get_grid: function () {
        return $(this.options.selector);
    },

    get_content: function () {
        return this.get_grid().find(".k-grid-content");
    },

    get_nonContent: function () {
        return this.get_grid().children().not(".k-grid-content");
    },

    size: function () {

        var height = this.getAvailableSpace(),
            nonContentHeight = 0;

        this.get_nonContent().each(function () {
            nonContentHeight += $(this).outerHeight(true);
        });

        this.get_content().height(height - nonContentHeight);

    },

    getAvailableSpace: function () {

        var height = 0,
            grid = this.get_grid();

        if (this.options.useAvailableSpace) {

            height = this.get_grid().parent().outerHeight(true);

            this.get_grid().parent().children().each(function (index, element) {
                if (!$(element).is(grid)) {
                    height = height - $(element).outerHeight(true);
                }
            });

        }
        else {
            height = this.get_grid().innerHeight();
        }

        return (height < 0) ? 0 : height;

    }

});