package("jp.co.takarakujinet");

jp.co.takarakujinet.TransitionGraph = function (lines, items) {
    this.lines = lines;
    this.items = items;
    this.drawed = {};
};

jp.co.takarakujinet.TransitionGraph.prototype = {
    show: function (id) {
		if (!this.drawed[id]) {
            this.draw(id);
            this.drawed[id] = true;
		}

        this.showLegend(id);
        this.showTab(id);
    },

    draw: function(id) {
        var lg = new html5jp.graph.line(id);
        if (!lg) return;
        lg.draw(
            this.createItems(id),
            {
              x: ["当せん数字"].concat(this.createXLabels()),
              y: ["数字", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
              yMax: 10,
              yMin: 0,
              dLabel: false,
              legend: false,
              xScaleFontSize: "7pt"
            }
        );
    },

	showLegend: function (id) {
        for (var i = 0; i < this.lines.length; i++) {
            this.setVisibility(
                "legend_" + this.lines[i],
                "all" == id || this.lines[i] == id);
        }
    },

    showTab: function (id) {
        var tab_ids = ["all"].concat(this.lines);

        for (var i = 0; i < tab_ids.length; i++) {
            this.setVisibility(
                "tab_" + tab_ids[i],
                tab_ids[i] == id);
        }
    },

    setVisibility: function(id, condition) {
        var visibility;

        if (condition) {
            visibility = "visible"
        } else {
            visibility = "hidden";
        }

        document.getElementById(id).style.visibility = visibility;
    },

    createXLabels: function () {
        var labels = [];

        for (var i = 0; i < this.items.length; i++) {
            var label = "";
            for (var j = 0; j < this.items[i].length; j++) {
                label += this.items[i][j];
            }

            labels.push(label);
        }

        return labels;
    },

    createItems: function (id) {
        var result = [];

        for (var i = 0; i < this.lines.length; i++) {
            var lineItems = [this.lines[i]];

            if ("all" == id || this.lines[i] == id) {
                lineItems = lineItems.concat(this.extractItems(i));
            }

            result.push(lineItems);
        }

        return result;
    },

    extractItems: function (index) {
        var result = [];

        for (var i = 0; i < this.items.length; i++) {
            result.push(this.items[i][index]);
        }

        return result;
    }
};
