bhajs.defineClass("BHA.Web.UI", "VisualControl", 
    function (id) {
        this.$id = id;
        this.$classInfo = bhajs.getClassInfo("BHA.Web.UI.VisualControl");
        this.$classInfo.registerObject(this.$id, this);
        this.$element = null;
        
        this.$onParentResizeDelegate = null;
        
        this.$initialize = function() {
            this.$addOnParentResizeHandler_IE();
            delete this.$initialize;
        };
        
        
        this.$addOnParentResizeHandler_IE = function() {
            this.$onParentResizeDelegate = this.$classInfo.createInstanceMethodCall(this.$id, '$onParentResizeHandler', ["e"]);
            var element = this.$getElement();
            var parent = element.parentNode;
            if (parent.attachEvent) {
                parent.attachEvent("onresize", this.$onParentResizeDelegate);
                this.$fixSize();
            }
            delete this.$addOnParentResizeHandler_IE;
        };
        
        
        this.$getElement = function() {
            if (!this.$element) {
                this.$element = document.getElementById(this.$id);
            }
            return this.$element;
        };

        this.$onParentResizeHandler = function(e) {
            this.$fixSize();
        };
        
        this.$fixSize = function () {
            var element = this.$getElement();
            var style = element.currentStyle;
            var parent = element.parentNode;
            var width = null;
            var height = null;
            if ((style.position == 'absolute') && (style.left != 'auto') && (style.right != 'auto')) {
//                 alert(this.$id 
//                        + '\n' + style.left + ' ' + style.right + ' ' + style.width
//                        + '\n' + style.top + ' ' + style.bottom + ' ' + style.height
//                    );
                 width = this.$calcWidth(parent.clientWidth, style);
                 width = String(width) + 'px';
//                 alert(width);
                 element.style.width = width;
                 
            }
            if ((style.position == 'absolute') && (style.top != 'auto') && (style.bottom != 'auto')) {
                height = this.$calcHeight(parent.clientHeight, style);
                height = String(height) + 'px';
//                alert(height);
                element.style.height = height;
            }
           
        }
        
        this.$calcWidth = function(parentClientWidth, currentStyle) {
            var left = this.$calcUnit(parentClientWidth, currentStyle.left);
            var right = this.$calcUnit(parentClientWidth, currentStyle.right);
            var marginLeft = this.$calcUnit(parentClientWidth, currentStyle.marginLeft);
            var marginRight = this.$calcUnit(parentClientWidth, currentStyle.marginRight);
            var borderLeftWidth = this.$calcUnit(parentClientWidth, currentStyle.borderLeftWidth);
            var borderRightWidth = this.$calcUnit(parentClientWidth, currentStyle.borderRightWidth);
            var paddingLeft = this.$calcUnit(parentClientWidth, currentStyle.paddingLeft);
            var paddingRight = this.$calcUnit(parentClientWidth, currentStyle.paddingRight);
            
            if ((currentStyle.borderLeftStyle == "none") || (currentStyle.borderLeftStyle == ""))
                borderLeftWidth = 0;
            if ((currentStyle.borderRightStyle == "none") || (currentStyle.borderRightStyle == ""))
                borderRightWidth = 0;
            
            var result = parentClientWidth - (left + right + marginLeft + marginRight + borderLeftWidth + borderRightWidth + paddingLeft + paddingRight);
           
            if (result < 0)
                return 0;
            return result;
        }
        
        this.$calcHeight = function(parentClientHeight, currentStyle) {
            var top = this.$calcUnit(parentClientHeight, currentStyle.top);
            var bottom = this.$calcUnit(parentClientHeight, currentStyle.bottom);
            var marginTop = this.$calcUnit(parentClientHeight, currentStyle.marginTop);
            var marginBottom = this.$calcUnit(parentClientHeight, currentStyle.marginBottom);
            var borderTopWidth = this.$calcUnit(parentClientHeight, currentStyle.borderTopWidth);
            var borderBottomWidth = this.$calcUnit(parentClientHeight, currentStyle.borderBottomWidth);
            var paddingTop = this.$calcUnit(parentClientHeight, currentStyle.paddingTop);
            var paddingBottom = this.$calcUnit(parentClientHeight, currentStyle.paddingBottom);

            if ((currentStyle.borderTopStyle == "none") || (currentStyle.borderTopStyle == ""))
                borderTopWidth = 0;
            if ((currentStyle.borderBottomStyle == "none") || (currentStyle.borderBottomStyle == ""))
                borderBottomWidth = 0;

            var result = parentClientHeight - (top + bottom + marginTop + marginBottom + borderTopWidth + borderBottomWidth + paddingTop + paddingBottom);
            if (result < 0)
                return 0;
            return result;
        
        }
                
        this.$calcUnit = function(parentSize, value) {
            try {
                switch (value) {
                    case "thick":
                        return 6;
                    case "medium":
                        return 4;
                    case "thin":
                        return 2;
                    default:
                        if (value.indexOf("px") > 0)
                            return parseFloat(value);
                            
                        if (value.indexOf("%") > 0)
                            return (parseFloat(value) / 100.0) * parentSize;
                }
            }catch(ex){
            }
            return 0;
        
        }
      
        
        this.$initialize();
    }
);