/// class ProspectDirector.Web.UI.FeatureSets.MacroEditorBase
/// using ProspectDirector.Web.UI.FeatureSets.EditorBase

/**
[NEEDS DOCUMENTATION]
@constructor
@class MacroEditorBase
@namespace Web.UI.FeatureSets
@extends Web.UI.FeatureSets.EditorBase
**/
ProspectDirector.Web.UI.FeatureSets.MacroEditorBase =
ProspectDirector.Web.UI.FeatureSets.EditorBase.extend({

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

    get_element: function () {
        return $("#" + this.getId());
    },

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

    render: function () {
        //return "<div id=\"" + this.getId() + "\" style=\"height:100px;\">&nbsp;</div>";
        return "<div id=\"" + this.getId() + "\" style='position:absolute;top:0;right:0;bottom:0;left:0;'></div>";
    },

    size: function () {
        //do nothing on purpose
    },

    calculateDimensions: function (height, width) {
        return {
            height: height - this._getPaddingY(this.get_domNode()),
            width: width - this._getPaddingX(this.get_domNode())
        }
    },

    setValidationErrors: function (validationErrors) {

        var that = this;
        var featureType = this.get_definition().get_type();
        var errors = validationErrors.getByFeatureType(featureType);
        var unmatchedErrors = [];

        this.get_element().find("LABEL[for]").removeClass("-pd-macro-editor-val-error");

        this.get_element().find("LABEL[for]").each(function (index, element) {
            Tipped.remove(element);
        });

        errors.each(function (index, error) {

            var labelDomNode = that.get_element().find("LABEL[for=" + error.get_propertyName() + "]");

            labelDomNode.addClass("-pd-macro-editor-val-error");

            if (labelDomNode.length > 0) { 
                Tipped.create(labelDomNode, error.get_message(), { skin: "validation-error" });
            }
            else {
                unmatchedErrors[unmatchedErrors.length] = error;
            }

        });

        if (unmatchedErrors.length > 0) {
            this.onValidationErrors(unmatchedErrors);
        }

    },

    onValidationErrors: function (validationErrors) {
        //overridable
    },

    setPrimaryValue: function (value) {

        var primaryValueId = this.getPrimaryValueID();

        if (primaryValueId != null) {
            this.get_feature().setValue(primaryValueId, value);
        }
        else {
            throw "The feature \"" + this.get_definition().get_name() + "\" has no primary value specified.";
        }
        
    },

    getPrimaryValue: function (value) {

        var result = null;
        var primaryValueId = this.getPrimaryValueID();

        if (primaryValueId != null) {
            result = this.get_feature().getValue(primaryValueId);
        }
        else {
            throw "The feature \"" + this.get_definition().get_name() + "\" has no primary value specified.";
        }

        return result;

    },

    getPrimaryValueID: function () {
        return this.get_definition().get_options().data.primaryValue;
    },
    
    _getPaddingY: function (container) {
        return parseFloat(container.css("paddingTop"))
            + parseFloat(container.css("paddingBottom"));
    },

    _getPaddingX: function (container) {
        return parseFloat(container.css("paddingRight"))
            + parseFloat(container.css("paddingLeft"));
    }

});