function Graph() {
	this.index = Graph.count;
	Graph.count++;
}

Graph.count = 0;
Graph.prototype.y_axis = 0;
Graph.prototype.y_axis_per = 1;
Graph.prototype.y_label_per = 1;
Graph.prototype.y_axis_min = 0000;
Graph.prototype.y_axis_max = 10000;
Graph.prototype.y_axis_unit = 1000;
Graph.prototype.y_label_callback = null;

Graph.prototype.x_axis = 35;
Graph.prototype.x_axis_per = 5;
Graph.prototype.x_label_per = 5;

Graph.prototype.plot_area_width = 35 * 12;
Graph.prototype.plot_area_height = 150;

Graph.prototype.build = function(parent) {

//	alert(this.y_axis_unit);

	this.y_axis = (this.y_axis_max - this.y_axis_min) / this.y_axis_unit * this.y_axis_per;

	var pane = document.createElement("DIV");
	pane.className = "g";
	parent.appendChild(pane);

	this._makeGPlot(pane);
	this._makeAxis(pane);
	this._makeYLabel(pane);
	this._makeYAxis(pane);
	this._makeXLabel(pane);
	this._makeXAxis(pane);
}

Graph.prototype._makeGPlot = function(parent) {
	var div = document.createElement("DIV");
	div.className = "g_plot";
	div.id = "__graph_plot_area" + this.index;
	div.style.width = this.plot_area_width - 2;
	div.style.height = this.plot_area_height - 1;
	parent.appendChild(div);
}

Graph.prototype._makeAxis = function(parent) {

	var table = document.createElement("TABLE");
	table.className = "axis";
	table.style.width = this.plot_area_width;
	table.style.height = this.plot_area_height + 1;
	parent.appendChild(table);

	var w = this.plot_area_width / this.x_axis;
	var h = this.plot_area_height / this.y_axis;

	var tbody = document.createElement("TBODY");
	table.appendChild(tbody)

	for (var row = 0, rows = this.y_axis; row < rows; row++) {
		var tr = document.createElement("TR");
		tbody.appendChild(tr);
		for (var col = 0, cols = this.x_axis; col < cols; col++) {
			var td = document.createElement("TD");
			td.className = "axis";
			td.style.width = w;
			td.style.height = h;
			tr.appendChild(td);
		}
	}
}

Graph.prototype._makeYLabel = function(parent) {

	var table = document.createElement("TABLE");
	table.className = "y_label";
	table.style.height = this.plot_area_height + 1;
	parent.appendChild(table);

	var tbody = document.createElement("TBODY");
	table.appendChild(tbody)

	var h = this.plot_area_height / this.y_axis * this.y_label_per;
/*
	alert(this.plot_area_height);
	alert(this.y_axis);
	alert(this.y_label_per);
	alert(h);
*/
	for (var i = this.y_axis_max; i >= this.y_axis_min; i -= this.y_axis_unit) {
		var tr = document.createElement("TR");
		tbody.appendChild(tr);

		var td = document.createElement("TD");
		td.className = "y_label";
		td.style.height = h;
		tr.appendChild(td);

		var label = i;
		if (this.y_label_callback != null) {
			label = this.y_label_callback(i);
		}
		td.appendChild(document.createTextNode(label));
	}

}

Graph.prototype._makeYAxis = function(parent) {

	var table = document.createElement("TABLE");
	table.className = "y_axis";
	table.style.height = this.plot_area_height + 1;
	parent.appendChild(table);

	var tbody = document.createElement("TBODY");
	table.appendChild(tbody)

	var h = this.plot_area_height / (this.y_axis / this.y_axis_per);

	for (var i = 0, n = this.y_axis / this.y_axis_per; i < n; i++) {
		var tr = document.createElement("TR");
		tbody.appendChild(tr);

		var td = document.createElement("TD");
		td.className = "y_axis";
		td.style.height = h;
		tr.appendChild(td);
	}
}

Graph.prototype._makeXLabel = function(parent) {

	var table = document.createElement("TABLE");
	table.className = "x_label";
	table.style.width = this.plot_area_width;
	parent.appendChild(table);

	var tbody = document.createElement("TBODY");
	table.appendChild(tbody)

	var tr = document.createElement("TR");
	tbody.appendChild(tr);

	var w = this.plot_area_width / this.x_label_per;

	for (var i = 0, n = this.x_axis / this.x_label_per; i < n; i++) {
		var td = document.createElement("TD");
		td.className = "x_label";
		td.width = w;
		tr.appendChild(td);

		td.appendChild(document.createTextNode((i + 1) * this.x_label_per));
	}
}

Graph.prototype._makeXAxis = function(parent) {

	var table = document.createElement("TABLE");
	table.className = "x_axis";
	table.style.width = this.plot_area_width;
	parent.appendChild(table);

	var tbody = document.createElement("TBODY");
	table.appendChild(tbody)

	var tr = document.createElement("TR");
	tbody.appendChild(tr);

	var w = this.plot_area_width / this.x_axis_per;

	for (var i = 0, n = this.x_axis / this.x_axis_per; i < n; i++) {
		var td = document.createElement("TD");
		td.className = "x_axis";
		td.style.width = w;
		tr.appendChild(td);
	}
}

Graph.prototype.getPlotArea = function() {
	return document.getElementById("__graph_plot_area" + this.index);
}

Graph.prototype.plot = function(x) {
	var div = this.getPlotArea();
	var height = this.plot_area_height - 1
	for (var j = 1, m = arguments.length; j < m; j++) {
		var value = arguments[j];
		if ((value == null) || (!(value instanceof Array))) {
			var a = new Array();
			a.push(value);
			value = a;
		}

		var v = 0;
		var hh = 0;
		for (var i = value.length - 1; i >= 0; i--) {
			var h = value[i];
			if (h < this.y_axis_min) {
				continue;
			}
			if (this.y_axis_min > 0) {
				h -= this.y_axis_min;
			}

			v += value[i];
			if (i == 0) {
				h = v * this.plot_area_height / (this.y_axis_max - this.y_axis_min);
				h = Math.floor(h);
				h -= hh;
			} else {
				h = h * this.plot_area_height / (this.y_axis_max - this.y_axis_min);
				h = Math.floor(h);
			}

			if (h > 0) {
				height -= h;
				hh += h;
				var bar = document.createElement("DIV");
				if (m > 2) {
					bar.className = "bar bar" + (j) + "_" + (i + 1);
				} else {
					bar.className = "bar bar" + (i + 1);
				}
				bar.style.top = height;
				bar.style.left = x - 1;
				bar.style.height = h;
				bar.style.width = 1;
				div.appendChild(bar);
			}
		}
	}
}
