/// class PD.Web.UI.Controls.ControlMessage

PD.Web.UI.Controls.ControlMessage =
Class.extend({

    //===================================
    // FIELDS
    //===================================

    _options: null,

    //===================================
    // CONSTRUCTOR
    //===================================

    init: function (options) {
        
        this._options = $.extend({}, {
            target: null,
            node: null,
            filter: null
        }, options);

        if (options.target != null) {
            options.target.addMessage = $.proxy(this.addMessage, this);
            options.target.clearMessage = $.proxy(this.clearMessage, this);
        }

    },
    
    //===================================
    // PROPERTIES
    //===================================
    
    get_node: function () {
        return this._options.node;
    },
    
    get_filter: function () {
        return this._options.filter;
    },

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

    addMessage: function (text, callback) {

        var $message = this._buildMessage(text);

        this._addMessageCallback($message, callback);

        this._getTargetNode().prepend($message);

    },

    clearMessage: function () {
        this._getTargetNode().find(".-pd-control-msg").remove();
    },

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

    _getTargetNode: function () {

        var $result = this.get_node(),
            filter = this.get_filter();

        if (filter != null) {
            $result = $result.find(filter);
        }

        return $result;

    },

    _addMessageCallback: function ($message, callback) {

        if ($.isFunction(callback)) {
            $message.find(".-pd-control-msg-text").addClass("-pd-w-callback");
            this._getTargetNode().on("click", ".-pd-control-msg-text", $.proxy(callback, this));
        }

    },

    _buildMessage: function (text) {

        var result = [
            "<table class='-pd-control-msg'>",
                "<tr>",
                    "<td style=''>",
                        "<span class='-pd-control-msg-text'>",
                            text,
                        "</span>",
                    "</td>",
                "</tr>",
            "</table>"
        ];

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

    }

});

PD.Web.UI.Controls.ControlMessage.mixin = function (options) {
    return new PD.Web.UI.Controls.ControlMessage(options);
}