/// class PD.Web.UI.Controls.GridMessagingModule
/// using PD.Web.UI.Controls.GridModule

PD.Web.UI.Controls.GridMessagingModule =
PD.Web.UI.Controls.GridModule.extend({

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

    start: function () {
        this.addGridMethod("addMessage", this.addMessage);
        this.addGridMethod("clearMessage", this.clearMessage);
    },

    addMessage: function (text, callback) {
        var that = this;
        var $message = this._getMessage(text);
        this._addMessageCallback($message, callback);
        this.get_kendo().content.prepend($message);
        
        setTimeout(_.bind(this._setContentHeight, this), 250);
        
    },

    clearMessage: function () {
        this.get_kendo().content.find("[data-role=message-container]").remove();
    },

    //===================================
    // PRIVATE METHODS
    //===================================

    _addMessageCallback: function ($message, callback) {

        if ($.isFunction(callback)) {

            $message.find("[data-role=message]").css({
                textDecoration: "underline",
                color: "#0000FF",
                cursor: "pointer"
            });

            this.get_kendo().content.on("click", "SPAN[data-role=message]", $.proxy(callback, this));

        }

    },

    _getMessage: function (text) {
        
        var result = [
            "<table style='height:100%;background-color:#FFFFFF;border:0 none;' data-role='message-container'>",
                "<tr>",
                    "<td style='text-align:center;vertical-align:middle;background-color:#FFFFFF;cursor:default;border: 0 none;'>",
                        "<span style='font-size:13px;' data-role='message'>",
                            text,
                        "</span>",
                    "</td>",
                "</tr>",
            "</table>"
        ];

        return $(result.join(""));

    },

    _setContentHeight: function () {

        if (this.get_kendo() == null) {
            return;
        }

        var $grid = this.get_kendo().element;
        var gridHeight = $grid.outerHeight();
        var headerHeight = $grid.find(".k-grid-toolbar").outerHeight();
        var columnHeaderHeight = $grid.find(".k-grid-header").outerHeight();
        var footerHeight = $grid.find(".k-grid-pager").outerHeight();
        var $content = $grid.find(".k-grid-content");
        var calculatedHeight = gridHeight - (headerHeight + footerHeight + columnHeaderHeight);

        $content.css({
            height: calculatedHeight != null ? calculatedHeight + "px" : null
        });

    }

});