Ext.ns("Trv.layout");

Trv.layout.TableFormLayout = Ext.extend(Ext.layout.TableLayout, {
	renderAll : function (ct, target) {
	    var items = ct.items.items;
	    for (var i = 0, len = items.length; i < len; i++) {
	        var c = items[i];
	        if (c && (!c.rendered || !this.isValidParent(c, target))) {
	            this.renderItem(c, i, target);
	        }
	    }
	},
    monitorResize: true,
    setContainer: function() {
        Ext.layout.FormLayout.prototype.setContainer.apply(this, arguments);
        this.currentRow = 0;
        this.currentColumn = 0;
        this.cells = [];
    },
    renderItem : function(c, position, target) {
        if (c && !c.rendered) {
            var cell = Ext.get(this.getNextCell(c));
            cell.addClass("x-table-layout-column-" + this.currentColumn);
            Ext.layout.FormLayout.prototype.renderItem.call(this, c, 0, cell);
        }
    },
    getAnchorViewSize : Ext.layout.AnchorLayout.prototype.getAnchorViewSize,
    getTemplateArgs : Ext.layout.FormLayout.prototype.getTemplateArgs,
    onLayout : function(ct, target) {
        Trv.layout.TableFormLayout.superclass.onLayout.call(this, ct, target);

        if (!target.hasClass("x-table-form-layout-ct")) {
            target.addClass("x-table-form-layout-ct");
        };

        var viewSize = this.getAnchorViewSize(ct, target);

        var aw, ah;

        if (ct.anchorSize) {
            if (typeof ct.anchorSize == "number") {
                aw = ct.anchorSize;
            } else {
                aw = ct.anchorSize.width;
                ah = ct.anchorSize.height;
            }
        } else {
            aw = ct.initialConfig.width;
            ah = ct.initialConfig.height;
        }

        var cs = ct.items.items, len = cs.length, i, j, c, a, cw, ch;

        var x, w, h, col, colWidth, offset;

        for (i = 0; i < len; i++) {

            c = cs[i];

            x = c.getEl().parent(".x-table-layout-cell");

            if (this.columnWidths) {
                col = parseInt(x.dom.className.replace(/.*x-table-layout-column-([d]+).*/, "$1"));
                colWidth = 0, offset = 0;
                for (j = col; j < (col + (c.colspan || 1)); j++) {
                    colWidth += this.columnWidths[j];
                    offset += 10;
                }

                w = (viewSize.width * colWidth) - offset;
            } else {
                w = (viewSize.width / this.columns) * (c.colspan || 1);
            }

            x.setWidth(w);
            w = w - 10;
            h = x.getHeight();

            if (c.anchor) {
                a = c.anchorSpec;
                if (!a) {
                    var vs = c.anchor.split(" ");
                    c.anchorSpec = a = {
                        right: this.parseAnchor(vs[0], c.initialConfig.width, aw),
                        bottom: this.parseAnchor(vs[1], c.initialConfig.height, ah)
                    };
                }

                cw = a.right ? this.adjustWidthAnchor(a.right(w), c) : undefined;
                ch = a.bottom ? this.adjustHeightAnchor(a.bottom(h), c) : undefined;

                if (cw || ch) {
                    c.setSize(cw || undefined, ch || undefined);
                }
            }
        }
    },

    parseAnchor : function(a, start, cstart) {
        if (a && a != "none") {
            var last;
            if (/^(r|right|b|bottom)$/i.test(a)) {
                var diff = cstart - start;
                return function(v) {
                    if (v !== last) {
                        last = v;
                        return v - diff;
                    }
                };
            } else if (a.indexOf("%") != -1) {
                var ratio = parseFloat(a.replace("%", "")) * .01;

                return function(v) {
                    if (v !== last) {
                        last = v;
                        return Math.floor(v * ratio);
                    }
                };
            } else {
                a = parseInt(a, 10);

                if (!isNaN(a)) {
                    return function(v) {
                        if (v !== last) {
                            last = v;
                            return v + a;
                        }
                    };
                }
            }
        }
        return false;
    },
    adjustWidthAnchor : function(value, comp) {
        return value - (comp.isFormField ? (comp.hideLabel ? 0 : this.labelAdjust) : 0);
    },
    adjustHeightAnchor : function(value, comp) {
        return value;
    },
    getLabelStyle : Ext.layout.FormLayout.prototype.getLabelStyle
});

Ext.Container.LAYOUTS["tableform"] = Trv.layout.TableFormLayout;
