var LayoutSpring = {
    init: function () {
        this.initView();
        this.initLayout();
        this.initForm();
    },
    initView: function () {
        var gView = z.gv.GView({
            appendTo: "#gvDiv",
            grid_line_visible:true,
            data: this._createDemoData()
        });

        pro.GVUtil.initToolbar(gView);
        this.gView = gView;
    },
    _createDemoData: function () {
        var dataArr = [];
        var centerNode = this._createNode();
        dataArr.push(centerNode);
        var starNodes = this._createStarNodes(dataArr, centerNode, 10);
        this._createStarNodes(dataArr, starNodes[0], 8);
        this._createStarNodes(dataArr, starNodes[1], 6);
        this._createStarNodes(dataArr, starNodes[2], 6);
        var bStartNodes = this._createStarNodes(dataArr, starNodes[3], 9);
        this._createStarNodes(dataArr, bStartNodes[0], 8);
        this._createStarNodes(dataArr, bStartNodes[1], 7);
        this._createStarNodes(dataArr, bStartNodes[2], 6);
        this._createStarNodes(dataArr, bStartNodes[3], 5);
        this._createStarNodes(dataArr, bStartNodes[4], 4);
        // this._createStarNodes(dataArr, bStartNodes[5], 20/3);
        return dataArr;
    },
    _createStarNodes: function (arr, centerNode, count) {
        var nodes = [];
        for (var i = 0; i < count; i++) {
            var node = this._createNode();
            nodes.push(node);
            arr.push(node);
            arr.push(new z.gv.Link(centerNode, node));
        }
        return nodes;
    },
    _createNode: function (props) {
        return new z.gv.Node(z.util.mergeObject({
            stroke: null,
            "fill": "radial_gradient",
            "fill_gradient_colors": {
                "0": "#fff",
                "0.7": "#e30613"
            }
        }, props))
    },

    initForm: function () {
        this.controlFrom = z.form.Form("#controlForm");
        this.controlFrom.onFormChange(this.handleFormChange, this);
        this.controlFrom.setValue(this.forceLayout.gets(["node_repulsion", "link_repulsion"]));
        z.dom.event.onclick("#startBtn", function () {
            this.forceLayout.start();
        }, this);
        z.dom.event.onclick("#stopBtn", function () {
            this.forceLayout.stop();
        }, this);
        z.dom.event.onclick("#randomBtn", function () {
            this.gView.eachData(function (data) {
                if (z.type.isNode(data)) {
                    data.setPosition(Math.random() * 600 | 0, Math.random() * 400 | 0)
                }
            })
        }, this);
        z.dom.event.onclick("#completeBtn", function () {
            this.forceLayout.layout();
        }, this);
    },
    handleFormChange: function () {
        var value = this.controlFrom.getValue();

        this.forceLayout.set(value);
        this.forceLayout.start();
    },
    initLayout: function () {
        var gView = this.gView;
        this.forceLayout = new z.gv.layout.ForceLayout(gView, {
            onComplete: function () {
                // gView.zoomFit();
            }
        });
        this.forceLayout.start();
    }
};
z.ready(function () {
    LayoutSpring.init();
});