/// class PD.Web.InvalidFieldHighlighter

PD.Web.InvalidFieldHighlighter = Class.extend({

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

    CSS_VALIDATION_FAILURE: "validation-failure-label",
    ATTR_DATA: "warnings",

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

    init: function (options, $root) {
        this.apply(options, $root);
    },

    //===================================
    // METHODS
    //===================================

    apply: function (data, $root) {

        var that = this,
            label = null,
            labelless = [],
            $labels = null;

        $root = $root || $(document);

        this.clear($root);

        _.each(data, function (item) {

            if (item.key.length === 0) {
                labelless[labelless.length] = item;
            }
            else {

                label = that.getFieldLabel($root, item.key);

                if (label != null) {
                    that.addWarningToFieldLabel(label, item.message);
                }

            }

        });

        $labels = $root.find("." + this.CSS_VALIDATION_FAILURE);

        $labels.each(function (index, el) {
            that.addTooltip($(el));
        });

        $labels.on("click", $.proxy(this._toolTip, this));

        
        if ($labels.length > 0) {
            this.showToasterMessage($labels.length);
        }

        if (labelless.length > 0) {
            this.showValidationDialog(labelless);
        }
        
    },

    clear: function ($root) {

        var that = this;

        $root = $root || $(document);

        $root.find("." + this.CSS_VALIDATION_FAILURE).each(function (idx, element) {
            $(element).removeData(that.ATTR_DATA);
            Tipped.remove($(element));
        });

        $root.find("." + this.CSS_VALIDATION_FAILURE).off("click");
        $root.find("." + this.CSS_VALIDATION_FAILURE).removeClass(this.CSS_VALIDATION_FAILURE);

    },

    addWarningToFieldLabel: function ($label, message) {

        var data = null;

        if (!$.hasData($label) && $label.data(this.ATTR_DATA) === undefined) {
            $label.data(this.ATTR_DATA, []);
        }

        data = $label.data(this.ATTR_DATA);

        data[data.length] = message;

        $label.addClass(this.CSS_VALIDATION_FAILURE);

    },

    addTooltip: function (element, message) {
    
        var messages = element.data(this.ATTR_DATA),
            message = "";

        if (messages.length > 1) {
            message = "<ul style=\"padding:0px 10px 0px 10px;\"><li>" + messages.join("</li><li>") + "</li></ul>";
        }
        else {
            message = messages[0];
        }

        Tipped.create(element, message, {
            skin: "white",
            border: {
                color: '#660000',
                size: 1,
                opacity: 1
            },
            fontSize: "12px",
            hook: {
                target: 'topleft',
                tooltip: 'bottomleft'
            },
            maxWidth: 300
        });

    },

    getFieldLabel: function ($container, key) {

        var forId = "label[for='" + key + "']",
            result = $container.find(forId);

        if (result.length === 0) {
            forId = "label[for='" + key.replace(/\[/g, "_").replace(/\]/g, "_").replace(/\./g, "_") + "']";
            result = $container.find(forId);
        }

        return result.length > 0 ? result : null;

    },

    showToasterMessage: function (validationErrorCount) {

        var icon = PD.Path.resolve("~/application/icons/24x24/SIGN_WARNING.PNG"),
            html = "<span style=\"font-size:12px;display:inline-block;height:100%;background-image:url(" + icon + ");background-repeat:no-repeat;padding-left:30px;\"><b>Validation Errors</b><br>" + validationErrorCount + " validation errors were found.</span>"

        PDJS.Shared.MessageStack.instance.error(html);

    }

});